【问题标题】:How to display custom posts from a related custom taxonomy如何显示来自相关自定义分类的自定义帖子
【发布时间】:2012-09-04 05:04:30
【问题描述】:

我创建了两种不同的自定义帖子类型:“视频”和“位置”。
然后,我创建了一个名为“Video_Categories”的自定义分类。
我已将此自定义分类法分配给两种自定义帖子类型。

我想要做的是在位置上显示彼此具有相同术语的视频。

例如:

视频帖子:

  • 名称:视频 1; Video_Category:昆士兰布里斯班;
  • 名称:视频 2; Video_Category:昆士兰黄金海岸;
  • 名称:视频 3; Video_Category:昆士兰州阳光海岸;

位置信息:

  • 名称:布里斯班; Video_Category:布里斯班;

我想从位置页面创建一个查询,查看此帖子的分类并返回具有相同分类的视频帖子。

在上面的示例中,“视频 1”视频帖子将被返回并显示在位置页面上。

【问题讨论】:

    标签: wordpress taxonomy custom-post-type custom-taxonomy


    【解决方案1】:

    好问题,这与获取相关类别或标签有点不同,但仍使用类似的前提。有几种方法可以做到这一点,但最简单的方法之一可能是使用使用WP_Query 的自定义函数。将以下代码添加到您的 functions.php 文件中。

    // Create a query for the custom taxonomy
    function related_posts_by_taxonomy( $post_id, $taxonomy, $args=array() ) {
        $query = new WP_Query();
        $terms = wp_get_object_terms( $post_id, $taxonomy );
    
        // Make sure we have terms from the current post
        if ( count( $terms ) ) {
            $post_ids = get_objects_in_term( $terms[0]->term_id, $taxonomy );
            $post = get_post( $post_id );
            $post_type = get_post_type( $post );
    
            // Only search for the custom taxonomy on whichever post_type
            // we AREN'T currently on
            // This refers to the custom post_types you created so
            // make sure they are spelled/capitalized correctly
            if ( strcasecmp($post_type, 'locations') == 0 ) {
                $type = 'videos';
            } else {
                $type = 'locations';
            }
    
            $args = wp_parse_args( $args, array(
                    'post_type' => $type,
                    'post__in' => $post_ids,
                    'taxonomy' => $taxonomy,
                    'term' => $terms[0]->slug,
                ) );
            $query = new WP_Query( $args );
        }
    
        // Return our results in query form
        return $query;
    }
    

    显然,您可以更改此函数中的任何内容以获得您正在寻找的确切结果。请查看http://codex.wordpress.org/Class_Reference/WP_Query 以供进一步参考。

    有了这个,您现在可以访问related_posts_by_taxonomy() 函数,您可以在其中传递您想要查找相关帖子的任何分类。因此,在您的 single.php 或用于自定义帖子类型的任何模板中,您可以执行以下操作:

    <h4>Related Posts</h3>
    <ul>
    <?php $related =  related_posts_by_taxonomy( $post->ID, 'Video_Categories' );
        while ( $related->have_posts() ): $related->the_post(); ?>
            <li><?php the_title(); ?></li>
        <?php endwhile; ?>
    </ul>
    

    【讨论】:

    • 嗨@dpcasady,我已经添加了这个函数并从single.php页面调用它,它返回:“致命错误:不能使用WP_Error类型的对象作为数组”。并将这一行指定为问题:"$post_ids = get_objects_in_term( $terms[0]->term_id, $taxonomy );"
    • 这意味着您正在尝试使用不存在的分类法。你确定你注册的自定义分类是Video_Categories吗?也许是Video_Category
    • 你说得对,我必须删除“Video_Category”中的大写字符才能正常工作。看起来像我想要的那样工作。谢谢你的帮助! :)
    • 嗨@dpcasady,不错的功能。我在结果中添加了链接和日期:&lt;?php $related = related_posts_by_taxonomy( $post-&gt;ID, 'Video_Categories' ); while ( $related-&gt;have_posts() ): $related-&gt;the_post(); ?&gt; &lt;li&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="Link to &lt;?php the_title_attribute(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;span&gt;&lt;?php the_time(get_option('date_format')); ?&gt;&lt;/span&gt;&lt;/li&gt; &lt;?php endwhile; ?&gt;
    猜你喜欢
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多