Returning Value Inside Wordpress Post Type

Asked

Viewed 239 times

2

I need to return values to be displayed in the columns of the post type, but I’m not able to return anything, as should be done?

Code:

add_action( 'init', 'create_eventcategory_taxonomy', 0);
add_filter ("manage_edit-tf_events_columns", "tf_events_edit_columns");
add_action ("manage_posts_custom_column", "tf_events_custom_columns");
add_action('init', 'create_event_postype');

function create_event_postype() {
    $labels = array(
        'name' => _x('Events', 'post type general name'),
        'singular_name' => _x('Event', 'post type singular name'),
        'add_new' => _x('Add New', 'events'),
        'add_new_item' => __('Add New Event'),
        'edit_item' => __('Edit Event'),
        'new_item' => __('New Event'),
        'view_item' => __('View Event'),
        'search_items' => __('Search Events'),
        'not_found' =>  __('No events found'),
        'not_found_in_trash' => __('No events found in Trash'),
        'parent_item_colon' => '',
    );

    $args = array(
        'label' => __('Events'),
        'labels' => $labels,
        'public' => true,
        'can_export' => true,
        'show_ui' => true,
        '_builtin' => false,
        'capability_type' => 'post',
        'menu_icon' => get_bloginfo('template_url').'/functions/images/event_16.png',
        'hierarchical' => false,
        'rewrite' => array( "slug" => "events" ),
        'supports'=> array('title', 'thumbnail', 'excerpt', 'editor') ,
        'show_in_nav_menus' => true,
        'taxonomies' => array( 'tf_eventcategory', 'post_tag')
    );

    register_post_type( 'tf_events', $args);
}

function create_eventcategory_taxonomy() {
    $labels = array(
        'name' => _x( 'Categories', 'taxonomy general name' ),
        'singular_name' => _x( 'Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Categories' ),
        'popular_items' => __( 'Popular Categories' ),
        'all_items' => __( 'All Categories' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Category' ),
        'update_item' => __( 'Update Category' ),
        'add_new_item' => __( 'Add New Category' ),
        'new_item_name' => __( 'New Category Name' ),
        'separate_items_with_commas' => __( 'Separate categories with commas' ),
        'add_or_remove_items' => __( 'Add or remove categories' ),
        'choose_from_most_used' => __( 'Choose from the most used categories' ),
    );

    register_taxonomy('tf_eventcategory','tf_events', array(
        'label' => __('Event Category'),
        'labels' => $labels,
        'hierarchical' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'event-category' ),
    ));
}


function tf_events_edit_columns($columns) {
    $columns = array(
        "cb" => "<input type=\"checkbox\" />",
        "tf_col_ev_cat" => "Category",
        "tf_col_ev_date" => "Dates",
        "tf_col_ev_times" => "Times",
        "tf_col_ev_thumb" => "Thumbnail",
        "title" => "Event",
        "tf_col_ev_desc" => "Description",
    );
    return $columns;
}

function tf_events_custom_columns($column){
    global $post;
    $custom = get_post_custom();

    switch ($column){
        case "tf_col_ev_cat":
            echo 'teste1';
        break;

        case "tf_col_ev_date":
            echo 'teste2';
        break;

        case "tf_col_ev_times":
            echo 'teste3';
        break;

        case "tf_col_ev_thumb":
            echo 'teste4';
        break;

        case "tf_col_ev_desc";
            echo 'teste5';
        break;
    }
}

I want to leave it this way: inserir a descrição da imagem aqui

2 answers

2

It all depends on the way you will save and work with the options.

  • Category is a custom taxonomy and in case you can use get_the_term_list().
  • Dates looks like a custom field and can be recovered with get_post_meta().
  • Times is the same as Dates.
  • Thumbnail if it is the native Wordpress option is possible with get_the_post_thumbnail().
  • Event which is the title just use get_the_title().
  • Description must be the excerpt or the content of the editor... For both cases you have options.

Here’s a practical example of everything I said:

function tf_events_custom_columns( $column ) {
    global $post;

    switch ( $column ) {
        case 'tf_col_ev_cat' :
            echo get_the_term_list( $post->ID, 'tf_eventcategory', '', ', ', '' );
            break;

        case 'tf_col_ev_date' :
            echo get_post_meta( $post->ID, 'your_date_key', true );
            break;

        case 'tf_col_ev_times' :
            echo get_post_meta( $post->ID, 'your_times_key', true );
            break;

        case 'tf_col_ev_thumb' :
            echo get_the_post_thumbnail( $post->ID, 'thumbnail' );
            break;

        case 'tf_col_ev_desc' ;
            echo wp_trim_words( $post->post_content, 20 ); // resumo/descrição com 20 palavras
            break;
    }
}

1

To answer by Claudio Sanches is exactly what you need, but I’ll make a Code Review:

  • It is not necessary to have two add_action( 'init', ... );, put it all together in one.

  • You also don’t need to prioritize zero: add_action( 'init', ..., 0 );, This is for special cases.

  • The hook manage_posts_custom_column has two parameters: column and post_id. So, with priority ten (default), gets like this:

    add_action ( 'manage_posts_custom_column', 'tf_events_custom_columns', 10, 2 );
    function tf_events_custom_columns( $column, $post_id ) { ... }
    

    In your case, you need to access global $post to catch the post_content. But if I didn’t need that content, did not need to call the global variable because the ID that is used in several parts is already passed in the hook.

  • If by chance you are using this code in the file functions.php of Theme, change to a plugin. See why in Where to put my code: plugin or functions.php?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.