0

I have created a custom post type. Within that I have assigned two custom taxonomies. One (computer) is what I use to drive the template files and slug of page (taxonomy-computer.php). The second (faq-option) is what will categorize all posts associated under the first taxonomy (computer).

My issue that I am having is creating some sort of loop to list out the faq-option tax with the associated posts to that.

i.e.

taxonomy-computer.php (computer)
-> faq-option
-> post
-> post

Currently it is listing out each term with the associated post

i.e.

-> faq-option
-> post

-> faq-option
-> post

My main objective is to group the under the respective taxonomy. I also was trying to stay with the main loop rather than create a new query. Now I am open to suggestions here.

    $terms = get_the_terms( $post->ID , 'faq-option' );

  foreach( $terms as $term ) :

        foreach( $terms as $term ) : setup_postdata($term);

      echo '<h3>'.$term->name. '</h3>' ;
        echo '<p><a href="#">'.the_title().'</a></p>';

    endforeach;
        wp_reset_postdata();

  endforeach;

The block of code is being added to the taxonomy-computer.php file. I am also using pre_get_posts to change the amount of ppp as well as the order/orderby.

I am stump and open to suggestions.

Thank you!

  • It's hard to understand what you are trying to do. So you want a taxonomy template (computer) to display the posts that are in another taxonomy (faq)? This sounds unusual, to say the least. Are you sure you understood what taxonomies are for? Wordpress by default will query the posts that are within a taxonomy term, ie. faq-options/faq-1, which will give you all the posts that are in faq-1. – Michael Sep 27 '18 at 23:35
  • Actually I don't know what exactly you are trying to do. But seeing your code and your desired output, try putting this line echo '<h3>'.$term->name. '</h3>' ; before second Foreach loop. Hope it helps. – Krishna Joshi Sep 28 '18 at 5:00
0

From what you are describe, I think you have a very simple and common (I'm certainly guilty) mistake. In taxonomy-computer.php, you have put your code after while ( have_posts() ) : the_post();. The result is that it executes not once, but every time a "computer" post is displayed.

To fix the problem, you need to move your code and put it BEFORE while ( have_posts() ) : the_post();.

A taxonomy template is a form of archive template; it is going to display multiple posts. In your case, taxonomy-computer.php is going to display each every post that has been been tagged as "computer". If the goal is to display the FAQ at the top of the page, followed by each of the "computer" posts below, then your FAQ code needs to be inserted before the template starts displaying the "computer" posts (i.e. before while ( have_posts() ) : the_post();).

Just where you put the code depends on how you want to format the template. That part is up to you.

0

Try this

<?php 
  $terms = get_the_terms( $post->ID , 'company' );
  foreach( $terms as $term ) :
    echo '<h3>'.$term->name. '</h3>' ;
    foreach( $terms as $term ) : setup_postdata($term);
      echo '<p><a href="#">'.the_title().'</a></p>';
    endforeach; wp_reset_postdata();
  endforeach;
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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