【发布时间】:2014-03-03 14:16:12
【问题描述】:
我用this solution 解决了在帖子中显示自定义帖子类型的问题,但是,我想过滤更多,只显示与主帖子类别匹配的自定义帖子类型的帖子(或更多精确的蛞蝓,但解决方案没有区别)。
我通过使用这个来获得主要帖子的 slug:
$category_main = get_the_category();
$cat_slug = $category_main[0]->slug;
echo $cat_slug; // This is just to see if I got the right output
我以同样的方式从自定义帖子类型中获取 slug,但它位于循环自定义帖子类型的循环中。
$category_course = get_the_category();
$cat_slug_course = $category_course[0]->slug;
echo $cat_slug_course;
所以,我现在想要的是只显示与原始帖子的 slug 匹配的自定义类型的帖子。
在伪代码中是这样的:
If $cat_slug_course is equal to $cat_slug, display all custom posts with slug $cat_slug_course and none other
我觉得这不是 Wordpress 特有的,而是 PHP 的,这就是我在这里发布它的原因。
这是用于显示自定义类型帖子的循环。
$args = array( 'post_type' => 'Course', 'posts_per_page' => 2 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$category_course = get_the_category();
$cat_slug_course = $category_course[0]->slug;
echo $cat_slug_course; // This is just to see if I got the right output
echo '<br />';
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile; ?>
【问题讨论】: