2012-10-08 94 views
0

我想在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個帖子),然後調用每個帖子的縮略圖和標題?

感謝您的任何建議。

回答

1

你應該嘗試用這些參數:

$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(); 
} 
+0

非常感謝,我會盡力處理 – Sara44