0

I'm trying to display a page with related posts (FAQ) within the same category ID. Currently it displays all custom posts, but with different categories.

As an example: - Page has a category ID '4'. - Page has content - Under content it has a FAQ that needs to show the same category of the page (4)

My code:

          <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

      <div class="content">
        <div class="white-block white-bg shadow">

          <div class="col-md-12">
            <div class="row">

              <div class="col-md-9 order-md-last">
                <div class="col-md-12 text-block light-grey">


                <h1><?php the_title(); ?><br />
                  <small><?php edit_post_link(); ?></small>
                  <?php the_category(); ?>
                </h1>

                <?php the_content(); ?>

    <?php
                        $query = new WP_Query( array( 'post_type' => 'faq', 'cat' => $cat_id, 'posts_per_page' => '3', 'post_status'=>published,) );
                          while ( $query->have_posts() ) : $query->the_post();
                       ?>

                       <a href="<? the_permalink(); ?>" class="list-group-item list-group-item-action ">
                         <div class="col-12">
                           <div class="row">

                        <h5><?php the_title(); ?></h5>

                      </div>
                      <div class="row">
                          <small><?php the_date(); ?></small>

                      </div>
                      </div>
                        <p class="mb-1">
                          <?php echo(get_the_excerpt()); ?></p>
                      </a>


                     <?php endwhile; ?>


                <?php wp_link_pages(); ?>


            <h3> FAQ </h3>


            <?php wp_reset_query(); ?>

<?php
                    $query = new WP_Query( array( 'post_type' => 'faq', 'cat' => $cat_id, 'posts_per_page' => '3', 'post_status'=>published,) );
                      while ( $query->have_posts() ) : $query->the_post();
                   ?>

                   <a href="<? the_permalink(); ?>" class="list-group-item list-group-item-action ">
                     <div class="col-12">
                       <div class="row">

                    <h5><?php the_title(); ?></h5>

                  </div>
                  <div class="row">
                      <small><?php the_date(); ?></small>

                  </div>
                  </div>
                    <p class="mb-1">
                      <?php echo(get_the_excerpt()); ?></p>
                  </a>


                 <?php endwhile; ?>

What am I doing wrong?

1 Answer 1

0

Fixed it as followed:

Within the loop I fetch the cat_ID:

 <?php
     foreach((get_the_category()) as $category) {
     $cat_id= $category->cat_ID;
   }
 ?>

Outside the loop I use the cat_ID:

'cat' => $cat_id,

I guess not the best solution but it works..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.