簡體   English   中英

WordPress:標簽相關的帖子,但在相同的類別

[英]WordPress: Related Posts by Tags, but in the Same Categories

我使用下面的腳本來獲取相關的標簽職位,但注意到,如果我有一只鳥的圖片標記表也標記的白色 ,該表將在鳥的相關職位部分顯示出來。 有沒有辦法只獲得至少與其中一個類別相匹配的帖子,因此鳥類和表格不會相關,除非它們共享一個類別?

我嘗試在$ args中設置一個category__in數組,但沒有運氣!

<?php
  $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),
    'showposts'=>6, // Number of related posts that will be shown.
    'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) {
        $my_query->the_post();
    ?>

    <?php if ( get_post_meta($post->ID, 'Image', true) ) { ?>
        <a style="text-decoration: none;" href="<?php the_permalink(); ?>">
            <img style="max-height: 125px; margin: 15px 10px; vertical-align: middle;" src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "Image", $single = true); ?>&w=125&zc=1" alt="" />
        </a>
    <?php } ?>      

    <?php
    }
}
  }
?>

似乎Wordpress不會創建wp_term_taxonomywp_term_relationships表的多個連接,這很奇怪!
無論如何,快速入侵(效率不高)是在根據標簽獲取帖子后,將原始帖子類別與返回的帖子進行比較:

$tags = wp_get_post_tags($post->ID);
$cats = wp_get_post_categories($post->ID);

...

$my_query->the_post();
$resCats = wp_get_post_categories($post->ID);
$resArr = array_diff($resCats, $cats);
if(count($resArr) != count($resCats)) {

我將深入研究query_posts方法,並在有時間的時候編寫插件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM