我正试图在我的单曲上创建一个相关的帖子部分。php文件,我将此作为起点(http://www.wpbeginner.com/wp-tutorials/how-to-display-related-posts-in-wordpress/) 我的问题是,我不认为这个脚本是从自定义帖子类型调用的。只要主岗位区域有什么,没有岗位。他们都在CPT中。我该如何调整才能使其发挥作用?这里有一件事我已经试过了,但还是出现了错误。
<div class="relatedposts">
<h3>Related posts</h3>
<?php
$orig_post = $post array( \'food\', \'film\', \'music\', \'art\', \'fashion\', \'photography\' );
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
\'tag__in\' => $tag_ids,
\'post__not_in\' => array($post->ID),
\'posts_per_page\'=>4, // Number of related posts to display.
\'caller_get_posts\'=>1
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="relatedthumb">
<a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
<?php the_title(); ?>
</a>
</div>
<? }
}
$post = $orig_post;
wp_reset_query();
?>
</div>
最合适的回答,由SO网友:Stephen Harris 整理而成
默认情况下WP_Query()
将查询帖子(除非tax_query
已设置)。您可以通过设置post_type
输入您的$args
大堆
要搜索与当前查看的帖子类型相同的帖子,可以执行以下操作:
$args=array(
\'post_type\' => get_post_type(), //gets the post type of the current \'post\'
\'tag__in\' => $tag_ids,
\'post__not_in\' => array($post->ID),
\'posts_per_page\' => 4, // Number of related posts to display.
\'caller_get_posts\' => 1
);