Home > Net >  How to display custom type post article on main post (single.php)
How to display custom type post article on main post (single.php)

Time:12-03

I want to display my faq post (custom type post) on main post (single.php). I am using category to match the posts. If any category from faq post (custom type post) matches category of main 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 ><?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; ?>

FAQ CUSTOM POST TYPE

add_action( 'init', 'create_faqs' );

function create_faqs() {
    register_post_type( 'faqs',
        array(
            'labels' => array(
                'name' => 'FAQs',
                'singular_name' => 'FAQ',
                'add_new' => 'Add New',
                'add_new_item' => 'Add New Faq',
                'edit' => 'Edit',
                'edit_item' => 'Edit Faq',
                'new_item' => 'New Faq',
                'view' => 'View',
                'view_item' => 'View Faq',
                'search_items' => 'Search FAQs',
                'not_found' => 'No FAQs found',
                'not_found_in_trash' => 'No faqs found in Trash',
                'parent' => 'Parent Faq'
            ),
            
 
            'public' => true,
            'menu_position' => 15,
            // 'menu_icon' => 'dashicons-editor-help',
            'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
            'taxonomies' => array( '' ),
            'menu_icon' => 'dashicons-editor-help',
            'has_archive' => true
        )
    );
}
function reg_cat() {
         register_taxonomy_for_object_type('category','faqs');
    }
add_action('init', 'reg_cat');

CodePudding user response:

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 <p></p> 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 ><?php the_title(); ?></h1>

    <div ><?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.
    $query_args = array(
        'post_type'      => 'faqs',
        '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 >
                <h2 ><?php the_title(); ?></h2>

                <div ><?php the_content(); ?>
            </div>
            <?php

        endwhile;

        // Reset the post data.
        wp_reset_postdata();

    endif;
}
  • Related