【问题标题】:How to display custom type post article on main post (single.php)How to display custom type post article on main post (single.php)
【发布时间】: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.

标签: php wordpress


【解决方案1】:

There are a lot of mistakes in your code.

  1. the_category() doesn't return anything, it echoes the terms links.
  2. the_content() also echoes so you should not add echo before this function.
  3. the_content() shouldn't be wrapped in &lt;p&gt;&lt;/p&gt; tag.
  4. You're not using wp_reset_postdata() in your custom query.
  5. Your logic is not good for your requirements.
  6. Necessary validations are missing in your code.

    There is a simplified code for requirements.

    Note: I have not tested the code, so there could be typos and mistakes which can create errors so make sure you have FTP access to modify the files to fix the error, If you're using the WordPress file editor then test the code in localhost or somewhere else then upload in live websites

    <?php
    while ( have_posts() ) :
        the_post();
        ?>
    
        <h1 class="page-title"><?php the_title(); ?></h1>
    
        <div class="post-content"><?php the_content(); ?></div>
    
        <?php get_template_part( 'widgets/cta' ); ?>
    
        <?php vh_display_post_faqs( get_the_ID() ); ?>
    <?php endwhile; ?>
    

    The above code is your single.php template file, I have added vh_display_post_faqs() which takes the post id and outputs the faqs content after doing the validations.

    The below code will go to functions.php.

    <?php
    /**
     * Display faq lists from matching post categories.
     *
     * @param int $post_id Post ID.
     */
    function vh_display_post_faqs( $post_id = 0 ) {
        // Return if post id is empty.
        if ( empty( $post_id ) ) {
            return;
        }
    
        // Get the categories ids using the passed post id.
        $categories_ids = wp_get_post_terms( $post_id, 'category', array( 'fields' => 'ids' ) );
    
        // Validate for empty ids or wp errors.
        if ( empty( $categories_ids ) || is_wp_error( $categories_ids ) ) {
            return;
        }
    
        // Prepare query args, using tax query and assuming you have "category" taxonomy linked with "faq"
        // custom post type.
        $query_args = array(
            'post_type'      => 'faq',
            'posts_per_page' => 2,
            'tax_query'      => array( // phpcs:ignore WordPress.DB.SlowDBQuery
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => $categories_ids,
            ),
        );
    
        // Run query and fetch faq posts
        $faqs = new WP_Query( $query_args );
    
        // Check if we have posts from the query.
        if ( $faqs->have_posts() ) :
            
            // Loop through the query posts.
            while ( $faqs->have_posts() ) :
                $faqs->the_post();
    
                // Print the content.
                ?>
                <div class="faq-post">
                    <h2 class="faq-title"><?php the_title(); ?></h2>
    
                    <div class="faq-content"><?php the_content(); ?>
                </div>
                <?php
    
            endwhile;
    
            // Reset the post data.
            wp_reset_postdata();
    
        endif;
    }
    

【讨论】:

    猜你喜欢
    • 2023-02-24
    • 2022-12-01
    • 2022-12-01
    • 2014-07-05
    • 1970-01-01
    • 2022-12-01
    • 2022-12-01
    • 2022-12-01
    • 1970-01-01
    相关资源
    最近更新 更多