0

What I am trying to achieve is displaying all Post Category Titles as thumbnail on first page. But when user clicks on the specific Category, I want to display all posts for that specific category. I am using Custom Post type and Custom Fields Plugin so I have created different custom post and fields too, but stuck how I can configure the permalink function or create the custom query that could help me achieve this.

Any help will be highly appreciated. Thank you.

1

1 Answer 1

1

How to get category wise all post in WordPress?

Add below code in taxonomy-"yourtaxonomyname".php

for ex : file name : taxonomy-event-category.php & Post type : events & taxonomy name : event-category

<?php
$category_object        = get_queried_object();
$category_taxonomy      = $category_object->taxonomy;
$category_term_id       = $category_object->term_id;
$category_name          = $category_object->name;
$category_description   = $category_object->description;
$category_slug          = $category_object->slug;

$post_type = 'events';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_args=array(
    'type'                     => $post_type,
    'post_status'              => 'publish',
    'posts_per_page'           => 15,
    'paged'                    => $paged, 
    'caller_get_posts'         => -1,
    'child_of'                 => 0,
    'parent'                   => 0,
    'orderby'                  => 'name', 
    'order'                    => 'ASC',
    'hide_empty'               => 0,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'tax_query'                => array(
                                        array(
                                            'taxonomy' => $category_taxonomy,
                                            'field' => 'id',
                                            'terms' => $category_term_id
                                        )
                                    ),
    'pad_counts'               => false, 
);
$post_my_query = null;
$post_my_query = new WP_Query($post_args);

if( $post_my_query->have_posts() ) :
?>

    <ul>
        <?php
            while ($post_my_query->have_posts()) : $post_my_query->the_post(); 
            ?>
            <li>
                <a href="<?php echo get_permalink($post->ID);?>">
                <?php
                    if ( has_post_thumbnail() ) {
                        //get_the_post_thumbnail( $post->ID, array( 100, 100) ); 
                       ?>
                        <?php echo  get_the_post_thumbnail($post->ID,'medium'); //thumbnail,medium,large,full,array(100,100)?>
                       <?php
                    }
                ?>
                    <p><?php echo get_the_title( $post->ID );?></p>
                </a>
            </li>
            <?php
           endwhile;
       ?>
    </ul>

<?php
else :      
    echo '<p class="red-error">No Post Found!</p>'; 
endif;

wp_reset_query($post_my_query);
?>
<?php //if(function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $post_my_query ) ); }else{ echo '<p class="red-error">No Event Post Found!</p>'; }
?>
0

Your Answer

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

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