在页面中打印自定义字段内容


WordPress : printing custom fields content in pages

我正在使用自定义帖子类型。Post类型是使用types插件创建的。自定义帖子类型名称是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;?>

现在如何打印标题后的自定义字段内容?请帮助

请注意"Types"插件的文档:

…类型自定义字段使用标准的WordPress后元表,使其与任何主题或插件交叉兼容....

因此,可以使用"get_post_meta"函数获取自定义字段的值:

get_post_meta ( int $post_id, string $key = '', bool $single = false )

如果您知道数据库中自定义字段的名称,例如:description,则可以使用以下代码片段将其值放入循环中:

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

就这些。

问好。