获取自定义帖子类型的后置元数据

问题描述 投票:0回答:2

我创建自定义帖子类型“组合”并使用值键“_portfolio_name_value_key”创建自定义元框。但是当我尝试在前端检索保存的值时,它没有显示。我在single-portfolio.php页面中使用了这段代码:<?php get_post_meta( $post->ID, '_portfolio_name_value_key' ); ?>

但是标题,特色图片,自定义分类和内容都没有问题显示。这是我用过的完整代码:

<?php 
                    if ( have_posts() ) :
                    while ( have_posts() ) : the_post(); ?>


         <div class=row>
            <div class="col-xs-12 col-md-8 col-sm-8">
               <div class="owl-carousel owl-theme project-detail-carousel"id=project_detail>
                  <div class=project-detail-item><img alt=p-detail src="<?php the_post_thumbnail(''); ?>"></div>
                </div>
            </div>
            <div class="col-xs-12 col-sm-4 col-md-4">
               <div class=project-detail-1>
                  <h2><?php the_title(); ?></h2>
                  <ul>
                     <li><span>Client:</span><?php get_post_meta( $post->ID, '_portfolio_name_value_key' ); ?>
                     <li><span>Category:</span> <?php the_terms( $post->ID, 'field' ); ?>
                     <li><span>Status:</span> <?php get_post_meta( $post->ID, '_portfolio_status_value_key' ); ?>
                     <li><span>Tags:</span> <?php the_terms( $post->ID, 'ptag' ); ?>
                     <li><span>Date:</span> <?php get_post_meta( $post->ID, '_portfolio_date_value_key' ); ?>
                  </ul>
               </div>
            </div>
         </div>
         <div class="row mt-50">
            <div class=col-md-12>
               <div class=project-detail-1-info>
                  <h3>A little About This Project</h3>
                  <div class=text-content>
                    <?php the_content(); ?>
                  </div>
               </div>
            </div>
        </div>

        <?php 
        endwhile;
    endif;      

     ?>
wordpress custom-post-type meta-boxes
2个回答
0
投票

因为看起来你正在使用一个循环。在循环中,您应该使用get_the_ID()函数。

从:

<?php get_post_meta( $post->ID, '_portfolio_name_value_key' ); ?>

至:

<?php get_post_meta(get_the_ID(), '_portfolio_name_value_key' ); ?>

0
投票

试试这个:-

<?php 

$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));
                    if ( $wpb_all_query->have_posts() ) { ?>
	<div class=row>
            <div class="col-xs-12 col-md-8 col-sm-8">
               <div class="owl-carousel owl-theme project-detail-carousel"id=project_detail>
                  <div class=project-detail-item><img alt=p-detail src="<?php the_post_thumbnail(''); ?>"></div>
                </div>
            </div>
            <div class="col-xs-12 col-sm-4 col-md-4">
               <div class=project-detail-1>
                  <h2><?php the_title(); ?></h2>
                  <ul>
                     <li><span>Client:</span><?php get_post_meta( get_the_ID(), '_portfolio_name_value_key', true ); ?>
                     <li><span>Category:</span> <?php the_terms( get_the_ID(), 'field' ,true ); ?>
                     <li><span>Status:</span> <?php get_post_meta( get_the_ID(), '_portfolio_status_value_key' ,true ); ?>
                     <li><span>Tags:</span> <?php the_terms( get_the_ID(), 'ptag' ,true ); ?>
                     <li><span>Date:</span> <?php get_post_meta( get_the_ID(), '_portfolio_date_value_key' ,true ); ?>
                  </ul>
               </div>
            </div>
         </div>
         <div class="row mt-50">
            <div class=col-md-12>
               <div class=project-detail-1-info>
                  <h3>A little About This Project</h3>
                  <div class=text-content>
                    <?php the_content(); ?>
                  </div>
               </div>
            </div>
        </div>
		<?php wp_reset_postdata(); }?>
<ul>
 	<li><a href="#">Office Painting – the Easy Way</a></li>
 	<li><a href="#">12 Questions to Ask – <i>Before</i> You Hire a Painter</a></li>
 	<li><a href="http://route1paintingandwindows.com/price-service-sketchy-dudes/">Price, Service &amp; Sketchy Dudes</a></li>
 	<li><a href="http://route1paintingandwindows.com/top-3-must-haves-when-hiring-a-paint-contractor/">Top 3 Must-Haves When Hiring A Paint Contractor</a></li>
 	<li><a href="#">Top 5 Uninsured Contractor What-if’s</a></li>
</ul>
© www.soinside.com 2019 - 2024. All rights reserved.