【问题标题】:WordPress : printing custom fields content in pagesWordPress:在页面中打印自定义字段内容
【发布时间】:2015-10-24 20:44:58
【问题描述】:
我正在使用自定义帖子类型。帖子类型是使用类型插件创建的。自定义帖子类型名称为partners,具有标题、特色图片和自定义字段描述,这就是我获取图片和标题的方式
<?php
$args=array('post_type' => 'partners');
$query= new WP_Query($args);
while ($query-> have_posts() ) : $query->the_post()?>
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-12">
<?php the_title;?>
<?php the_post_thumbnail( 'full', array( 'class' => 'innerimages')
);?>
</div>
<?php endwhile;?>
现在如何在标题后打印自定义字段内容?请帮助
【问题讨论】:
标签:
php
wordpress
custom-post-type
【解决方案1】:
请注意“类型”插件的文档:
...类型自定义字段使用标准的 WordPress 后元表,使其与任何主题或插件交叉兼容....
因此,可以使用“get_post_meta”函数获取自定义字段的值:
get_post_meta ( int $post_id, string $key = '', bool $single = false )
如果你知道数据库中自定义字段的名称,例如:description,就可以把它的值带入循环,用代码的sn-p:
get_post_meta ( get_the_ID(), 'description' )
包括你之前的代码:
<?php
$args=array('post_type' => 'partners');
$query= new WP_Query($args);
while ($query-> have_posts() ) : $query->the_post()?>
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-12">
<?php the_title;?>
<?php the_post_thumbnail( 'full', array( 'class' => 'innerimages') );?>
<?php print get_post_meta ( get_the_ID(), 'description' ); ?>
</div>
<?php endwhile;?>
仅此而已。
最好的问候。