WordPress - 自定义分类问题的相关帖子

发布于 2024-09-05 01:56:40 字数 1535 浏览 10 评论 0原文

我正在尝试根据自定义分类法显示相关帖子。我在 wordpress.org 上发现了一个类似的查询。然而,原始帖子在结果中多次重复。 (单词是我使用的自定义分类法的名称)似乎发生的情况是,根据设置的显示数量,单个帖子会被重复。知道什么可能导致这种情况吗?

代码:

<?php
//for in the loop, display all "content", regardless of post_type,
//that have the same custom taxonomy (e.g. words) terms as the current post
$backup = $post;  // backup the current object
$found_none = '<h2>No related posts found!</h2>';
$taxonomy = 'words';//  e.g. post_tag, category, custom taxonomy
$param_type = 'words'; //  e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array('public' => true), 'names' );
$tax_args=array('orderby' => 'none');
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
if ($tags) {
  foreach ($tags as $tag) {
    $args=array(
      "$param_type" => $tag->slug,
      'post__not_in' => array($post->ID),
      'post_type' => $post_types,
      'showposts'=>5,
      'caller_get_posts'=>1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
        <?php $found_none = '';
      endwhile;
    }
  }
}
if ($found_none) {
echo $found_none;
}
$post = $backup;  // copy it back
wp_reset_query(); // to use the original query again
?>

I'm trying to display related posts based on a custum taxonomy. I found a query at wordpress.org that kind of works. However the original post gets duplicated in the results multiple times. (words is the name of the custom taxonomy I use) What seems to happen is that the single post gets duplicated according to what amount showpost is set. Any idea's what could cause this?

The code:

<?php
//for in the loop, display all "content", regardless of post_type,
//that have the same custom taxonomy (e.g. words) terms as the current post
$backup = $post;  // backup the current object
$found_none = '<h2>No related posts found!</h2>';
$taxonomy = 'words';//  e.g. post_tag, category, custom taxonomy
$param_type = 'words'; //  e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array('public' => true), 'names' );
$tax_args=array('orderby' => 'none');
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
if ($tags) {
  foreach ($tags as $tag) {
    $args=array(
      "$param_type" => $tag->slug,
      'post__not_in' => array($post->ID),
      'post_type' => $post_types,
      'showposts'=>5,
      'caller_get_posts'=>1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
        <?php $found_none = '';
      endwhile;
    }
  }
}
if ($found_none) {
echo $found_none;
}
$post = $backup;  // copy it back
wp_reset_query(); // to use the original query again
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熊抱啵儿 2024-09-12 01:56:40

您在 foreach 循环内得到了重复项。该代码实际上是在说;

  1. 获取分类类型的所有术语 $param_type
  2. 对于每个术语,获取使用该术语标记的 5 篇帖子

因此,如果您有一篇标记有多个术语的帖子属于同一分类,很可能会出现不止一次。

您可以迭代地将查询的帖子添加到 post__not_in 数组中,以确保它们不会再次出现;

  1. if ($tags) {

    上方添加 $post_not_in = array($post->ID);

  2. 然后替换行 post__not_in' =>; array($post->ID), with post__not_in' =>; $post_not_in,.

  3. 最后,将 $post_not_in[] = get_the_ID(); 放入 while 循环中,位于 $found_none = '';

It's inside the foreach loop that you're getting duplications. That code is effectively saying;

  1. Get all the terms for taxonomy type $param_type
  2. For each term, get 5 posts that are tagged with that term

So if you have a post that is tagged with more than one term of the same taxonomy, it's likely it will appear more than once.

You can iteratively add queried posts into the post__not_in array to ensure they don't appear again;

  1. Add $post_not_in = array($post->ID); just above if ($tags) {

  2. Then replace the line post__not_in' => array($post->ID), with post__not_in' => $post_not_in,.

  3. Finally, drop $post_not_in[] = get_the_ID(); inside your while loop, after $found_none = '';

小霸王臭丫头 2024-09-12 01:56:40

对于我来说,我使用 这个插件自定义分类法相关帖子。我希望该插件能够帮助您解决问题。

As for me i use this plugin for custom taxonomy relate post. I hope that plugin will help your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文