简体   繁体   English

在Wordpress中显示相关文章

[英]Show related posts in Wordpress

I want to show related posts in Wordpress. 我想在Wordpress中显示相关帖子。 I need to select these posts manually, not choose from a category or tags automatically. 我需要手动选择这些帖子,而不是自动从类别或标签中选择。 I have previously used this code: 我以前使用过此代码:

<?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(); ?>

This selects nicely from the related tags but I need to call my related posts manually. 这可以从相关标签中很好地选择,但是我需要手动调用相关帖子。

I have put the post ids that I want to call in the custom field 'myrelatedposts' as a comma separated list, eg: 103, 104, 105, 122 我已将要调用的帖子ID放在逗号分隔的自定义字段“ myrelatedposts”中,例如:103、104、105、122

Now I need to call them in the above script. 现在,我需要在上面的脚本中调用它们。

How can I explode this list of posts into the array (still limiting to 5 posts) then call each post thumbnail and title? 如何将帖子列表分解为数组(仍然限制为5个帖子),然后调用每个帖子的缩略图和标题?

Thanks for any suggestions. 感谢您的任何建议。

You should try with these arguments : 您应该尝试使用以下参数:

$args=array(
  'post__in' => explode(',', get_post_meta($post->ID, 'myrelatedposts')),
  'ignore_sticky_posts'=>1 // caller_get_posts is deprecated
);

To add post thumbnails in your loop, you simply have to use post thumbnails functions , eg from codex : 要在循环中添加帖子缩略图,您只需要使用帖子缩略图功能 ,例如从codex中:

// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
    the_post_thumbnail();
} 

There is plugin called Similar Posts. 有一个名为“类似帖子”的插件。

http://wordpress.org/extend/plugins/similar-posts/ http://wordpress.org/extend/plugins/similar-posts/

it searches and match post contents, not only tags. 它不仅搜索并匹配帖子内容,还包括标签。

i think one of the disadvantages is that it runs many slow SQL queries. 我认为缺点之一是它运行许多缓慢的SQL查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM