在Wordpress中显示相关文章
问题描述:
我想在WordPress中显示相关文章。我需要手动选择这些帖子,而不是从一个类别或标签中自动选择。我以前使用过这个代码:在Wordpress中显示相关文章
<?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'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if($my_query->have_posts()) {
echo '<div id="relatedposts"><h3>Related Posts</h3><ul>';
while($my_query->have_posts()) {
$my_query->the_post(); ?>
<li><div class="relatedthumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
<div class="relatedcontent">
<h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<?php the_time('M j, Y') ?>
</div>
</li>
<? }
echo '</ul></div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>
这很好地从相关标签中选择,但我需要手动调用我的相关帖子。
我已经把帖子的ID,我想在自定义字段“myrelatedposts”调用以逗号分隔的列表,如: 103,104,105,122
现在我需要给他们打电话上面的脚本。
如何将此列表中的帖子分解到数组中(仍然限制为5个帖子),然后调用每个帖子的缩略图和标题?
感谢您的任何建议。
答
你应该尝试用这些参数:
$args=array(
'post__in' => explode(',', get_post_meta($post->ID, 'myrelatedposts')),
'ignore_sticky_posts'=>1 // caller_get_posts is deprecated
);
要在循环中添加后的缩略图,你只需要使用post thumbnails functions,例如来自电码:
// check if the post has a Post Thumbnail assigned to it.
if (has_post_thumbnail()) {
the_post_thumbnail();
}
答
有一个名为Similar Posts的插件。
http://wordpress.org/extend/plugins/similar-posts/
它搜索和赛后的内容,不仅是标签。
我认为其中一个缺点是它运行许多慢的SQL查询。
非常感谢,我会尽力处理 – Sara44