从类似的类别显示自定义字段(从类似帖子获取自定义字段)
问题描述:
我有一个single.php显示产品。我有这个产品的分类和子类别,我想显示类似类别的自定义字段图像的底部。从类似的类别显示自定义字段(从类似帖子获取自定义字段)
比如我在红袋1的时候,我想放在底部的缩略图像红袋2和红袋3相似的页面,这是在同一类别“红袋”
其实我有一个代码,已经,但我不知道如何定位的高级自定义字段插件创建的自定义字段:the_field("product_image")
这里是我的代码:
<?php
global $post;
$cat_ID=array();
$categories = get_the_category(); //get all categories for this post
foreach($categories as $category) {
array_push($cat_ID,$category->cat_ID);
}
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'numberposts' => 8,
'post__not_in' => array($post->ID),
'category__in' => $cat_ID
); // post__not_in will exclude the post we are displaying
$cat_posts = get_posts($args);
$out='';
foreach($cat_posts as $cat_post) {
$out .= '<li>';
$out .= '<a href="'.get_permalink($cat_post->ID).'" title="'.wptexturize($cat_post->post_title).'">'.wptexturize($cat_post->post_title).'</a></li>';
}
$out = '<ul class="cat_post">' . $out . '</ul>';
echo $out;
?>
其中.wptexturize($cat_post->post_title).
假设是一个图像,而不是标题。图像源应该取自自定义字段the_field("product_image")
答
如果您使用post_thumbnail_html
这将是一个很多simpeler。相反,您使用的是不是默认的wordpress功能:the_field
。
我不知道这是什么插件。
也许该功能(the_field
)可以在正常loop
内工作。
要做到这一点,您应该将get_posts($args)
更改为WP_Query($args)
,并用循环替换foreach($cat_posts..
。
我建议使用默认的wordpress缩略图/精选图像与post_thumbnail_html
这是有道理的。我使用高级自定义字段插件the_field,是的,我没有看到使用该功能与特色图像相比的点。感谢您的建议,我会尝试这个。 – mark
嗨,如果我使用post_thumbnail代替,我该如何输出它? '.wptexturize($ cat_post-> post_thumb).' does not work – mark
你不需要它。 – janw