dpeo
dpeo

Reputation: 5

How to show WordPress Post and page in category

I’m trying to create many WordPres post and page. I include the post and page to various category. In each category I add post and page both. In this circumstances I need to show post and page under the particular category. And I want to sort the post and page ascending or descending under the Category. I need the PHP Coding this purpose. Please Give me assistance. I have created category.php by the code bellow.

<div class="cate-top ">
                    <h1 class="cat-page-title"><?php printf( __( ' Your are Browsing: %s', 'twentythirteen' ), single_cat_title( '', false ) ); ?></h1>

                    <?php if ( category_description() ) : // Show an optional category description ?>
                    <div class="archive-meta"><?php echo category_description(); ?></div>
                    <?php endif; ?>

                    <?php while(have_posts()): the_post();?>


                </div>
            <div class="category-page">
                <div class="cate-inn ">
                        <h2><a href="<?php the_permalink();?>"> <?php the_title();?></a></h2>

                        <div class="cat-image fix">
                            <a href="<?php the_permalink();?>"> <?php the_post_thumbnail();?></a>
                        </div>
                        <div class="cat-read-more fix">
                            <?php read_more(0);?><a href="<?php the_permalink();?>">Read More</a>
                        </div>
                </div>




                    <?php endwhile;?>

Upvotes: 0

Views: 44

Answers (1)

Mukesh Ram
Mukesh Ram

Reputation: 6328

You can use get_posts or WP_Query to get the page and post with your desired category, For example

<?php $args = array(
'posts_per_page'   => 5,
'offset'           => 0,
'category'         => '',
'category_name'    => '',
'orderby'          => 'date',
'order'            => 'DESC',
'include'          => '',
'exclude'          => '',
'meta_key'         => '',
'meta_value'       => '',
'post_type'        => 'post',
'post_mime_type'   => '',
'post_parent'      => '',
'author'       => '',
'post_status'      => 'publish',
'suppress_filters' => true 
);
$posts_array = get_posts( $args ); ?>

You can just change your category name in args,

If you are using custom taxonomy instead default category, you may use following code

$custom_terms = get_terms('custom_taxonomy');

foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
    'tax_query' => array(
        array(
            'taxonomy' => 'custom_taxonomy',
            'field' => 'slug',
            'terms' => $custom_term->slug,
        ),
    ),
 );

 $loop = new WP_Query($args);
 if($loop->have_posts()) {
    echo '<h2>'.$custom_term->name.'</h2>';

    while($loop->have_posts()) : $loop->the_post();
        echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
    endwhile;
 }
}

For more help you can VISIT, for get_post you may VISIT

Upvotes: 1

Related Questions