Search code examples
phpcustom-post-typewordpresswp-list-categories

Custom Post Type displays post belong on the category


I have a custom post type advice and taxonomy adcat. I want to display all post belong to that category.

Lets say I have 4 categories namely : 'games', 'tours', 'dishes', 'hotels' and also this four category is a menu. If I click one of the category for example: hotels all post belong to the hotels should display.

By the way this code I used to show wordpress default categories:

<?php $catname = wp_title('', false); ?>
<?php $posts = get_posts("category_name=$catname&numberposts=8&offset=0");
foreach ($posts as $post) : start_wp(); ?>

//html output
<h1><?php the_title(); ?></h1>

<?php endforeach; ?>

this is not working in custom post 'taxonomies' any suggestion would be helpful thank's


Solution

  • try this maybe it work... I write a note so you can see whats going on.. hope it help

    <?php
        // Get the term/category of the post
        $terms = get_the_terms( $post->ID , 'advice-cat' );
         foreach ( $terms as $term ) {
         $term_link = get_term_link( $term, 'advice cat' );
        }
        //WordPress loop for custom post type
        $terms = get_the_terms( $post->ID , 'advice-cat' );
         $my_query = new WP_Query('post_type=advice&advice-cat=' . $term->name . '&posts_per_page=-1');
              while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
         // output content
    
         <?php the_title(); ?>           
    
    
        <?php endwhile;  wp_reset_query(); ?>