无法在前端显示元框数据

时间:2019-03-16 作者:Rahul

我可以在编辑器中显示特色图像和文本,但我制作了一些额外的自定义帖子类型和元框来显示一些重要数据,但我无法显示(highlighted in red in this image)

以下是我编写的全部自定义帖子和自定义元框代码:

// 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\' );

1 个回复
SO网友:Qaisar Feroz

您应该能够显示问题中提到的数据。

// 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();
我希望这有帮助。

EDIT
在自定义帖子类型的模板中(archive-car-listing.phpsingle-car-listing.php ), 代码应该看起来像

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;
     }

相关推荐

Publish metabox postition?

在我的自定义帖子类型上,当在移动设备上浏览网站时,发布元数据库将位于其他元数据库之上。为了解决这个问题,我改变了几乎所有元数据库的位置和优先级。但有一个元盒我无法使用优先级和上下文移动:https://developer.wordpress.org/reference/functions/add_meta_box/查看移动设备时,是否可以将发布元数据库移动到始终位于底部?这是我想要更改的默认位置。