$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom post types - Unable to show meta box data in frontend|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - Unable to show meta box data in frontend

matteradmin8PV0评论

I am able to show the featured image and the text in the editor but I made some additional custom post types and meta box to show some important data but I am unable to show(highlighted in red in this image)

Here's the entire custom post and custom meta box code that I wrote:

// enqueue the child theme stylesheet

Function qode_child_theme_enqueue_scripts() {
    wp_register_style( 'childstyle', get_stylesheet_directory_uri() . '/style.css'  );
    wp_enqueue_style( 'childstyle' );
}
add_action( 'wp_enqueue_scripts', 'qode_child_theme_enqueue_scripts', 11);

// create_post_car_listing function is created to add Car Listings from the admin area
function create_post_car_listing() {
$supports = array(
'title', 
'editor', 
'thumbnail', 
'excerpt', 
'revisions', 
);
$labels = array(
'name' => _x('Car Listings', 'plural'),
'singular_name' => _x('All Listings', 'singular'),
'menu_name' => _x('Car Listings', 'admin menu'),
'name_admin_bar' => _x('Car Listings', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Car Listing'),
'new_item' => __('New Car Listing'),
'edit_item' => __('Edit Car Listing'),
'view_item' => __('View Car Listing'),
'all_items' => __('All Listings'),
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'car-listing'),
'has_archive' => false,
'hierarchical' => false,
//'taxonomies' => array( 'category'),
);

register_post_type('car_listing', $args);
}
add_action('init', 'create_post_car_listing');

//Adding taxonomies for car listing 

function fuel_type() {
    $labels = array(
        'name' => _x( 'Fuel Type', 'taxonomy general name' ),
        'singular_name' => _x( 'Fuel Type', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Fuel Types' ),
        'all_items' => __( 'All Fuel Types' ),
        'parent_item' => __( 'Parent Fuel Type' ),
        'parent_item_colon' => __( 'Parent Fuel Type:' ),
        'edit_item' => __( 'Edit Fuel Type' ), 
        'update_item' => __( 'Update Fuel Type' ),
        'add_new_item' => __( 'Add New Fuel Type' ),
        'new_item_name' => __( 'New Fuel Type' ),
        'menu_name' => __( 'Fuel Type' ),
    ); 
    register_taxonomy(
        'fuel_type',
        'car_listing',
        array(
            'labels' => $labels,
            'rewrite' => array( 'slug' => 'fuel-type' ),
            'hierarchical' => true,
        )
    );

}
add_action( 'init', 'fuel_type' );

//Listing Categories function is used to add the type of fuel a car uses 

function listing_categories() {
    $labels = array(
        'name' => _x( 'Listing Category', 'taxonomy general name' ),
        'singular_name' => _x( 'Listing Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Listing Categories' ),
        'all_items' => __( 'All Listing Categories' ),
        'parent_item' => __( 'Parent Listing Category' ),
        'parent_item_colon' => __( 'Parent Listing Category:' ),
        'edit_item' => __( 'Edit Listing Category' ), 
        'update_item' => __( 'Update Listing Category' ),
        'add_new_item' => __( 'Add New Listing Category' ),
        'new_item_name' => __( 'New Listing Category Name' ),
        'menu_name' => __( 'Listing Categories' ),
    ); 
    register_taxonomy(
        'listing_category',
        'car_listing',
        array(
            'labels' => $labels,
            'rewrite' => array( 'slug' => 'listing-categories' ),
            'hierarchical' => true,
        )
    );

}
add_action( 'init', 'listing_categories' );


// Adding custom meta boxes for car listing
function listing_overview_meta_box(){
    add_meta_box( 'listing_overview_box', 'Listing Overview', 'overview_box_content_callback', 'car_listing' );
}
function overview_box_content_callback($post) {
    wp_nonce_field('overview_box_content_savedata', 'listing_overview_meta_box_nonce' );

    $value = get_post_meta( $post -> ID, '_listing_overview_content_key', true );

    echo '<textarea style ="width:100%;" rows="4" cols="50" id="listing_overview_box_content_field" name="listing_overview_box_content_field">' .esc_attr($value). '</textarea>';
}

add_action('add_meta_boxes', 'listing_overview_meta_box');

function overview_box_content_savedata ($post_id) {

    if ( ! isset ( $_POST['listing_overview_meta_box_nonce'])){
        return;
    }

    if ( ! wp_verify_nonce( $_POST['listing_overview_meta_box_nonce'], 'overview_box_content_savedata') ){
        return;
    }

    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
        return;
    }

    if( ! current_user_can('edit_post', $post_id) ){
        return;
    }

    if( ! isset( $_POST['listing_overview_box_content_field'] ) ) {
        return;
    }

    $overview_data = sanitize_text_field( $_POST['listing_overview_box_content_field'] );
    update_post_meta( $post_id, '_listing_overview_content_key', $overview_data );
}

add_action('save_post', 'overview_box_content_savedata' );

I am able to show the featured image and the text in the editor but I made some additional custom post types and meta box to show some important data but I am unable to show(highlighted in red in this image)

Here's the entire custom post and custom meta box code that I wrote:

// enqueue the child theme stylesheet

Function qode_child_theme_enqueue_scripts() {
    wp_register_style( 'childstyle', get_stylesheet_directory_uri() . '/style.css'  );
    wp_enqueue_style( 'childstyle' );
}
add_action( 'wp_enqueue_scripts', 'qode_child_theme_enqueue_scripts', 11);

// create_post_car_listing function is created to add Car Listings from the admin area
function create_post_car_listing() {
$supports = array(
'title', 
'editor', 
'thumbnail', 
'excerpt', 
'revisions', 
);
$labels = array(
'name' => _x('Car Listings', 'plural'),
'singular_name' => _x('All Listings', 'singular'),
'menu_name' => _x('Car Listings', 'admin menu'),
'name_admin_bar' => _x('Car Listings', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Car Listing'),
'new_item' => __('New Car Listing'),
'edit_item' => __('Edit Car Listing'),
'view_item' => __('View Car Listing'),
'all_items' => __('All Listings'),
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'car-listing'),
'has_archive' => false,
'hierarchical' => false,
//'taxonomies' => array( 'category'),
);

register_post_type('car_listing', $args);
}
add_action('init', 'create_post_car_listing');

//Adding taxonomies for car listing 

function fuel_type() {
    $labels = array(
        'name' => _x( 'Fuel Type', 'taxonomy general name' ),
        'singular_name' => _x( 'Fuel Type', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Fuel Types' ),
        'all_items' => __( 'All Fuel Types' ),
        'parent_item' => __( 'Parent Fuel Type' ),
        'parent_item_colon' => __( 'Parent Fuel Type:' ),
        'edit_item' => __( 'Edit Fuel Type' ), 
        'update_item' => __( 'Update Fuel Type' ),
        'add_new_item' => __( 'Add New Fuel Type' ),
        'new_item_name' => __( 'New Fuel Type' ),
        'menu_name' => __( 'Fuel Type' ),
    ); 
    register_taxonomy(
        'fuel_type',
        'car_listing',
        array(
            'labels' => $labels,
            'rewrite' => array( 'slug' => 'fuel-type' ),
            'hierarchical' => true,
        )
    );

}
add_action( 'init', 'fuel_type' );

//Listing Categories function is used to add the type of fuel a car uses 

function listing_categories() {
    $labels = array(
        'name' => _x( 'Listing Category', 'taxonomy general name' ),
        'singular_name' => _x( 'Listing Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Listing Categories' ),
        'all_items' => __( 'All Listing Categories' ),
        'parent_item' => __( 'Parent Listing Category' ),
        'parent_item_colon' => __( 'Parent Listing Category:' ),
        'edit_item' => __( 'Edit Listing Category' ), 
        'update_item' => __( 'Update Listing Category' ),
        'add_new_item' => __( 'Add New Listing Category' ),
        'new_item_name' => __( 'New Listing Category Name' ),
        'menu_name' => __( 'Listing Categories' ),
    ); 
    register_taxonomy(
        'listing_category',
        'car_listing',
        array(
            'labels' => $labels,
            'rewrite' => array( 'slug' => 'listing-categories' ),
            'hierarchical' => true,
        )
    );

}
add_action( 'init', 'listing_categories' );


// Adding custom meta boxes for car listing
function listing_overview_meta_box(){
    add_meta_box( 'listing_overview_box', 'Listing Overview', 'overview_box_content_callback', 'car_listing' );
}
function overview_box_content_callback($post) {
    wp_nonce_field('overview_box_content_savedata', 'listing_overview_meta_box_nonce' );

    $value = get_post_meta( $post -> ID, '_listing_overview_content_key', true );

    echo '<textarea style ="width:100%;" rows="4" cols="50" id="listing_overview_box_content_field" name="listing_overview_box_content_field">' .esc_attr($value). '</textarea>';
}

add_action('add_meta_boxes', 'listing_overview_meta_box');

function overview_box_content_savedata ($post_id) {

    if ( ! isset ( $_POST['listing_overview_meta_box_nonce'])){
        return;
    }

    if ( ! wp_verify_nonce( $_POST['listing_overview_meta_box_nonce'], 'overview_box_content_savedata') ){
        return;
    }

    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
        return;
    }

    if( ! current_user_can('edit_post', $post_id) ){
        return;
    }

    if( ! isset( $_POST['listing_overview_box_content_field'] ) ) {
        return;
    }

    $overview_data = sanitize_text_field( $_POST['listing_overview_box_content_field'] );
    update_post_meta( $post_id, '_listing_overview_content_key', $overview_data );
}

add_action('save_post', 'overview_box_content_savedata' );
Share Improve this question edited Mar 16, 2019 at 14:56 Rahul asked Mar 16, 2019 at 8:39 RahulRahul 2291 gold badge4 silver badges12 bronze badges 3
  • Image you shared has no red highlighted area. Share the code which fails to display data on front-end. – Qaisar Feroz Commented Mar 16, 2019 at 8:56
  • I don't see any connection between the code and image you provide. Also, please remove everything does not belonging to meta data: post type and taxonomy registration, scripts. – Max Yudin Commented Mar 16, 2019 at 9:31
  • Updated the image – Rahul Commented Mar 16, 2019 at 14:58
Add a comment  | 

1 Answer 1

Reset to default 2

You should be able to display the data mentioned in your question.

// use within loop 
echo get_post_meta( $post -> ID, '_listing_overview_content_key', true );

Example:

/**
* Setup query to show the 'car_listing' post type with '8' posts.
* Output is title with excerpt, Listing Overview, Fuel Type an Listing Category.   
*/
   $args = array(  
       'post_type' => 'car_listing',
       'post_status' => 'publish',
       'posts_per_page' => 8,
       'orderby' => 'title',
       'order' => 'ASC',
   );

   // get posts
   $loop = new WP_Query( $args );
   if ( $loop->have_posts() )  {    
       while ( $loop->have_posts() ) : $loop->the_post(); ?>
           <article>
               <h2><?php   the_title();  ?></h2>
               <p><?php    the_excerpt();  ?></p>
               <h3>Listing Overview:</h3>
               <p>
                   <?php  echo get_post_meta( $post -> ID, '_listing_overview_content_key', true ); ?>
               </p>
               <?php the_terms( $post->ID, 'fuel_type', '<br>Fuel Type: ', ' , ' ); ?>
               <?php the_terms( $post->ID, 'listing_category', '<br>Listing Category: ', ' , ' ); ?>
           </article>
   <?php
       endwhile;
 }

   wp_reset_postdata();

I hope this helps.

EDIT
In templates for custom post type ( archive-car-listing.php and single-car-listing.php ), the code should look like something

if ( have_posts() )  {    
           while ( have_posts() ) : the_post(); ?>
               <article>
                   <h2><?php   the_title();  ?></h2>
                   <p><?php    the_excerpt();  ?></p>
                   <h3>Listing Overview:</h3>
                   <p>
                       <?php  echo get_post_meta( $post->ID, '_listing_overview_content_key', true ); ?>
                   </p>
                   <?php the_terms( $post->ID, 'fuel_type', '<br>Fuel Type: ', ' , ' ); ?>
                   <?php the_terms( $post->ID, 'listing_category', '<br>Listing Category: ', ' , ' ); ?>
               </article>
       <?php
           endwhile;
     }
Post a comment

comment list (0)

  1. No comments so far