Php 如果与文章类别匹配,则显示自定义文章类型中的内容

Php 如果与文章类别匹配,则显示自定义文章类型中的内容,php,wordpress,plugins,Php,Wordpress,Plugins,我解决了在文章中显示自定义文章类型的问题,但是,我想过滤更多,只显示自定义文章类型中与主文章类别匹配的文章(或者更准确地说是slug,但解决方案没有区别) 我通过使用以下命令获得主帖子的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,但解决方案没有区别)

我通过使用以下命令获得主帖子的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
我以同样的方式从自定义post类型中获取slug,但是它在一个循环中,循环通过自定义post类型

$category_course = get_the_category();
$cat_slug_course = $category_course[0]->slug;
echo $cat_slug_course;
所以,我现在想要的是,只显示自定义类型中与原始post的slug匹配的post

在伪代码中,这类似于:

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; ?>
$args=array('post\u type'=>'Course','posts\u per\u page'=>2);
$loop=新的WP_查询($args);
而($loop->have_posts()):$loop->the_post();
$category_course=get_the_category();
$cat_slug_课程=$category_课程[0]->slug;
echo$cat_slug_课程;//这只是为了看看我是否得到了正确的输出
回声“
”; _title(); 回声'; _内容(); 回声'; endwhile;?>
好吧,这比预期的要简单

<?php if ($cat_slug_course == $cat_slug): ?>
    <div class="landing_title">
        <?php the_title(); ?>
    </div>
        <?php the_content();?>
    </div>
<?php endif; ?>

解决它。我没料到,但确实如此