【发布时间】:2022-12-02 01:19:05
【问题描述】:
I want to display myfaq post (custom type post)onmain post (single.php). I am using category to match the posts. If any category fromfaq post (custom type post)matches category ofmain post (single.php)then display FAQ post content below main post. Category need not match all but atleast one.
<?php while (have_posts()):
the_post(); ?>
<h1 class="page-title"><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php get_template_part("widgets/cta"); ?>
<?php
$cat = the_category();
echo $cat[0]->cat_name;
?>
<?php
$args_faq = ["post_type" => "faq", "posts_per_page" => 2];
$faq_loop = new WP_Query($args_faq);
while ($faq_loop->have_posts()):
$faq_loop->the_post();
$category_faq = the_category();
$cat_slug_faq = $category_faq[0]->cat_name;
echo $cat_slug_faq[0]->cat_name;
if ($cat_slug_faq == $cat_slug) {
echo "<h4>" . get_the_title() . "</h4>";
echo the_content();
}
endwhile;
?>
<?php
endwhile; ?>
【问题讨论】:
-
For clarity: does your main post have one category? Or multiple? If your main post has multiple, what category term would you want to match with your faq posts?
-
@disinfor My main post has multiple categories and also faq posts has multiple categories. Therefore, I need the statement to do search of any term that is the same and that is available between both posts even if it one category. I have tried using this
if (count(array_intersect($array1, $array2)) === 0) { // No values from array1 are in array 2 } else { // There is at least one value from array1 present in array2 }but it is not working also -
Do your main posts (I'm assuming the default Post type) and faq post type share the default category taxonomy? Or does FAQs have a registered taxonomy that's different? This will help.