0

I want to display category name as a collapsible headers and inside the collapsible body I want to display post titles linking back to original post.

I am using custom post type 'University' and custom taxonomy 'country'. So far I managed to get custom taxonomies names as header but unable to get posts inside the custom taxonomy.

Here is my code:

<div class="container">
<div class="row">   
<ul class="collapsible" data-collapsible="accordion">

<?php
$args = array(
'post_type'              => array( 'university' ),
'post_status'            => array( 'publish' ),
'taxonomy'               => 'country',
'posts_per_page'         => '1',

);
// The Query
$query = new WP_Query( $args );

$categories = get_categories($args);

// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
    $query->the_post(); 
    // do something

    foreach ($categories as $category){ ?>

    <li class="home-list hoverable col m6 z-depth-1">
    <div class="collapsible-header"><h5><i class="material-icons teal-text">add_circle_outline</i>
        <?php
        //echo var_dump($category);         
        $cat_name = $category->name;
        $cat_count = $category->count;

        echo 'Medical Universities in '.$cat_name;
        //echo $category->count;
        echo '</h5></div>';?>

    <div class="collapsible-body"><span>
        <?php
         echo $cat_count;
            $custom_terms = get_terms('country');
            //echo var_dump($custom_terms);

        ?>

    </span></div>   
        <?php


    }
        ?>
    </li>


    <?php }
} else {
// no posts found
}

// Restore original Post Data
wp_reset_postdata();
?>

Thank you all for your help in advance.

0

Check this:

$categories = get_categories( 'country' );

foreach ( $categories as $category ) {
  $posts_array = get_posts ( array (
    'posts_per_page' => -1,
    'post_type' => 'university',
    'tax_query' => array (
      array(
      'taxonomy' => 'country',
        'field' => 'term_id',
        'terms' => $category->term_id,
      )
    )
  ) );

  var_dump( $posts_array );
}

UPDATE:

<div class="container">
  <div class="row">   
    <ul class="collapsible" data-collapsible="accordion">

    <?php
    $categories = get_terms( 'country' );

    foreach ( $categories as $category ) : ?>

      <li class="home-list hoverable col m6 z-depth-1">

        <div class="collapsible-header">
          <h5>
            <i class="material-icons teal-text">add_circle_outline</i>
            <?php echo 'Medical Universities in ' . esc_html( $category->name ) . ' (' . $category->count . ')'; ?>
          </h5>
        </div>

        <div class="collapsible-body">

          <?php
          $posts_array = get_posts ( array (
            'posts_per_page' => -1,
            'post_type' => 'university',
            'tax_query' => array (
              array(
              'taxonomy' => 'country',
                'field' => 'term_id',
                'terms' => $category->term_id,
              )
            )
          ) );

          if ( $posts_array ) :

            foreach ( $posts_array as $university ) : ?>

              <h3><a href="<?php echo esc_url( get_permalink( $university->ID ) ); ?>"></a></h3>

            <?php endforeach;

          else : ?>

            <p>No posts found</p>

          <?php endif; ?>

        </div>

      </li>

    <?php endforeach; ?>

    </ul>

  </div>
</div>
| improve this answer | |
  • Thanks Minh for time but I am getting : array (size=0) empty I am using this code on front-page.php – Pankaj Kumar Apr 26 '18 at 16:06
  • Can you make sure you have posts under each categories? Can you show me a screenshot of your posts list from wp backend? – Minh Apr 26 '18 at 16:21
  • Hi Minh please check the screenshot here snag.gy/DsKJre.jpg I have 15 posts in 5 custom taxonomies – Pankaj Kumar Apr 27 '18 at 4:24
  • Can you check with $categories = get_terms( 'country' );? – Minh Apr 27 '18 at 6:04
  • Yes Im getting this result snag.gy/0YqU2B.jpg array (size=6) and can see all country created – Pankaj Kumar Apr 27 '18 at 7:17

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.