根据标签显示相关帖子
问题描述:
我正在使用大量标签和每个帖子分类的音乐网站。例如,在艺术家的页面上,相关的帖子显示基于标签的艺术家的发布。我曾尝试添加使用Wordpress' post_type到的$ args =阵列(标签 -根据标签显示相关帖子
'post_type' => 'releases'
,但并未奏效
例如,下面是完整的代码 -
<div class="relatedposts">
<h3>Releases by Artist</h3>
<?php
$orig_post = $post;
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(
'post_type' => 'releases',
'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>
我跟着其他很多帖子#1的,似乎无法得到正确的结果。我在哪里去了?!
答
<section class="row related">
<div class="row related-articles">
<h2 class="related-title">Related Content</h2>
<div class="relatedposts">
<?php
$orig_post = $post;
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'=>3, // 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="large-4 medium-4 small-12 columns img-wrap"> <a rel="external" href="<? the_permalink()?>">
<?php $url = wp_get_attachment_url(get_post_thumbnail_id($post->ID, 'thumbnail')); ?>
<img src="<?php echo $url ?>" />
<div class="story-title-sub">
<h2>
<?php the_title(); ?>
</h2>
</div>
</div>
<?php }
}
$post = $orig_post;
wp_reset_query();
?>
</div>
</div>
</section>
答
你可以尝试让这个职位代码由标签
<?php // related posts based on first tag of current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo '<h3>Related Posts</h3>';
$first_tag = $tags[0]->term_id;
$args = array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts' => 7, // how many posts?
'caller_get_posts' => 1
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) { ?>
<ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php } ?>
<?php } ?>
没有,似乎并没有被它的相关... – 2015-04-02 12:54:23
我编辑了以上我的网站上使用的代码,它看起来像相同的脚本,它在我的网站上工作。看起来你唯一真正不同的地方是你正在调用帖子类型。看看,也许你可以在上面找到答案 – 2015-04-02 13:12:31
是的,这是我使用的代码。它调用所有相关的标签。我已经在$ args = array中尝试了很多变体(section。嗯。令人费解的。 – 2015-04-02 13:18:51