Returning value into the Post Type in Wordpress

2

I need to return values to be displayed in columns of the post type, but I can not return anything, how should I do it?

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:

    
asked by anonymous 08.04.2014 / 20:51

2 answers

2

It all depends on how you are going to save and work with options.

Here's a practical example of everything I've 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;
    }
}
    
09.04.2014 / 21:07
1

A answer from Claudio Sanches is exactly what you need, but I'll do a Code Review:

  • You do not need to have two add_action( 'init', ... ); , put everything together into one.

  • You also do not need to put zero priority: add_action( 'init', ..., 0 ); , this is for special cases.

  • The hook manage_posts_custom_column has two parameters: column and post_id . Then, with priority ten default ), it looks 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 must access global $post to get post_content . But if you did not need this content , you did not need to call the global variable, because the ID that is used in several parts is already passed in hook .

  • If you happen to be using this code in the functions.php of theme file, switch to a plugin . See why at Where to put my code: plugin or functions.php?

12.04.2014 / 23:50