簡體   English   中英

如果與帖子類別匹配,則顯示自定義帖子類型的內容

[英]Display content from custom post type if match to post category

我使用此解決方案解決了在帖子中顯示自定義帖子類型的問題,但是,我想過濾更多內容,只顯示與主要帖子類別相匹配的自定義帖子類型的帖子(或更准確地說,子彈,但解決方案沒有區別)。

我使用以下方法獲得主要帖子的內容:

$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

我以相同的方式從自定義帖子類型中獲得了子彈,但它位於循環遍歷自定義帖子類型的循環中。

$category_course = get_the_category();
$cat_slug_course = $category_course[0]->slug;
echo $cat_slug_course;

因此,我現在想要的是僅顯示與原始帖子的子句匹配的來自自定義類型的帖子。

用偽代碼可以是:

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 if ($cat_slug_course == $cat_slug): ?>
    <div class="landing_title">
        <?php the_title(); ?>
    </div>
        <?php the_content();?>
    </div>
<?php endif; ?>

解決它。 沒想到,但是確實如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM