【问题标题】:Display content from custom post type if match to post category如果与帖子类别匹配,则显示来自自定义帖子类型的内容
【发布时间】: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; ?>

【问题讨论】:

    标签: php wordpress plugins


    【解决方案1】:

    好的,比想象的要简单。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 2014-10-03
      • 1970-01-01
      • 2019-04-29
      • 2017-10-11
      相关资源
      最近更新 更多