我有来自的自定义Metabox字段组https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Field-Types#group 包含3个字段,显示在产品页面模板上,我想调用客户端添加的最新帖子,将它们显示在我的首页上。以下是自定义字段的代码。
/**
* Repeatable Field Groups
*/
$meta_boxes[\'field_group\'] = array(
\'id\' => \'field_group\',
\'title\' => __( \'Manage your products here\', \'cmb2\' ),
\'show_on\' => array( \'id\' => array( 11, ) ), // Specific post IDs to display this metabox
\'object_types\' => array( \'page\', ),
\'fields\' => array(
array(
\'id\' => $prefix . \'repeat_group\',
\'type\' => \'group\',
\'options\' => array(
\'group_title\' => __( \'Product {#}\', \'cmb2\' ), // {#} gets replaced by row number
\'add_button\' => __( \'Add Another Product\', \'cmb2\' ),
\'remove_button\' => __( \'Remove Product\', \'cmb2\' ),
\'sortable\' => true, // beta
),
\'fields\' => array(
array(
\'name\' => \'Product Name\',
\'id\' => \'product_name\',
\'type\' => \'text\',
),
array(
\'name\' => \'Product Description\',
\'description\' => \'Write a short description for this Product\',
\'id\' => \'product_description\',
\'type\' => \'textarea\',
),
array(
\'name\' => \'Product Image\',
\'id\' => \'product_image\',
\'type\' => \'file\',
),
),
),
),
);
更新:我找到了这个
http://codex.wordpress.org/Function_Reference/wp_get_recent_posts 但我仍然不知道如何让它从自定义字段获取帖子
最合适的回答,由SO网友:Robert hue 整理而成
好的,你可以把这个粘贴到首页模板上。我以为你的$prefix
是prefix
产品页面id为11
您想最后打印3
条目。
<?php $meta_entries_pre = get_post_meta( 11, \'prefix_repeat_group\', true ); $meta_entries_int = array_reverse( $meta_entries_pre ); $meta_entries = array_slice( $meta_entries_int, 0, 3 ); if( ! empty( $meta_entries ) ) { ?>
<ul>
<?php foreach ( (array) $meta_entries as $key => $entry ) {
$product_name = $product_description = $meta_link = \'\';
if ( isset( $entry[\'product_name\'] ) ) $product_name = $entry[\'product_name\'];
if ( isset( $entry[\'product_description\'] ) ) $product_description = $entry[\'product_description\'];
if ( isset( $entry[\'product_image\'] ) ) $product_image = esc_html( $entry[\'product_image\'] ); ?>
<li>
<h3><?php echo $product_name; ?></h3>
<p><?php echo $product_description; ?></p>
<p><img src="<?php echo $product_image; ?>" alt="" /></p>
</li>
<?php } ?>
</ul>
<?php } ?>