Vadivel Murugan M Vadivel Murugan M - 7 months ago 53
PHP Question

How to display custom post category name list in wordpress

I have two custom post types(gallery and activities) in my wordpress site. I want to display the categories in gallery post type. But my code is showing all the categories in both post types(gallery and activities). I'm using advanced custom fields(ACF) and CPT-UI to create custom posts. How can I display the categories only in gallery. here is my code.

<?php $catargs = array(
'post_type' => 'gallery',

'orderby' => 'name',

'order' => 'ASC',

);

$categories = get_categories( $catargs );

foreach ($categories as $category) {?>
<h1 class="entry-title" style="padding-top:30px;">
<?php echo $category->name;// Category title ?></h1>

<?php

// WP_Query arguments

$args = array (

'post_type' => 'gallery',

'cat' => $category->cat_ID,


'order' => 'ASC',

'orderby' => 'title',

);



// The Query

$query = new WP_Query( $args );



// The Loop

if ( $query->have_posts() ) {

while ( $query->have_posts() ) {

$query->the_post();

?>
<div class="col-md-4 col-sm-4 col-xs-12" id="all-post">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($query->ID) ); ?>
<a href="<?php the_permalink(); ?>"><img src="<?php echo $url ?>" class="img-responsive thumb-image-cust thumbnail" alt=""></a>

</div>
<?php

// You can all phone/ email here

}

} else {

// no posts found

}

// Restore original Post Data

wp_reset_postdata();

}
?>

Answer

try this way this will display categories from specific post and retrieve the terms for a post.

<?php
$postArg = array('post_type'=>'post',
                        'posts_per_page'=>-1,
                        'order'=>'desc',
                      );

        $getPost = new wp_query($postArg);
        global $post;
        if($getPost->have_posts()){
            echo '<ul>';
                while ( $getPost->have_posts()):$getPost->the_post();

                    echo "<h2>".$post->post_title."</h2>";
                    $terms = get_the_terms($post->ID, 'category' );
                    foreach ($terms as $term) {
                        echo "<li>".$term_name = $term->name.'</li>';
                    }
                endwhile;
            echo '</ul>';
        }

?>

You can do by this way to display post under category name.

 <?php

     $cat = get_terms('category'); // you can put your custom taxonomy name as place of category.
        foreach ($cat as $catVal) {
            echo '<h2>'.$catVal->name.'</h2>';
            $postArg = array('post_type'=>'post','posts_per_page'=>-1,'order'=>'desc',
                              'tax_query' => array(
                                                    array(
                                                        'taxonomy' => 'category',
                                                        'field' => 'term_id',
                                                        'terms' => $catVal->term_id
                                                    )
                            ));

            $getPost = new wp_query($postArg);
            global $post;

            if($getPost->have_posts()){
                echo '<ul>';
                    while ( $getPost->have_posts()):$getPost->the_post();
                        echo "<li>".$post->post_title."</li>";
                    endwhile;
                echo '</ul>';
            }

        }
    ?>

output enter image description here

Comments