• nishagarg

    (@nishagarg)


    Hi,

    I recently started posting on my website and discovered that the category page had full posts instead of excerpts (maybe 251 words or so).

    Can you assist how to have the category pages to display only posts under that category with only excerpts showing up? I also do not want the date, time, author and comments to be displayed on category page.

    Please see image below to know what I mean:
    https://greentealobby.com/wp-content/uploads/2017/02/cat-page.jpg

    Also, I want to display 10-15 posts under each category page.

    Please assist.

    Thank you
    Nisha

Viewing 2 replies - 1 through 2 (of 2 total)
  • Subrata Sarkar

    (@subrataemfluence)

    You have two different questions.
    [A] “how to have the category pages to display only posts under that category”
    [B] “with only excerpts showing up”

    This is how I did it. You may try once if haven’t already

    [A]

    1. Create a page category.php in your theme’s root folder
    2. Get category object from current category title

    
    $cat_name = single_cat_title('', false);
    $term = get_term_by('name', $cat_name, 'category');
    $cat_id = $this_cat->term_id;
    

    Get posts for above category id ($cat_id). Better way is to create a custom function in your functions.php file.

    
    function get_posts_by_category_id($cat_id, $post_type, $records_per_page){
            $currentPage = get_query_var('paged');
            $args = array(
                'category' => $cat_id,
                'post_type' => $post_type,
                'paged' => $currentPage,
                'posts_per_page' => $records_per_page
            );
            $posts = get_posts($args);
    
            return $posts;
        }
    

    [B]:

    Now inside your category.php call the function from functions.php:

    
    $posts = get_posts_by_category_id($cat_id, 'trips', 24);
    <?php foreach($posts as $post) : setup_postdata($post); ?>
      <div>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
        <?php the_excerpt(); ?>
      </div>
    <?php endforeach;?>
    

    Hope this helps!

    • This reply was modified 8 years ago by Subrata Sarkar. Reason: Added code block
    • This reply was modified 8 years ago by Subrata Sarkar. Reason: Removed wrong placement of php tag
    Thread Starter nishagarg

    (@nishagarg)

    Thank you for your response @subrataemfluence. I do not have technical knowledge, so could not understand how to ” Get category object from current category title”.

    I can create a root file category.php, but rest is nightmare for me to do?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Category Page to have excerpts instead of full post’ is closed to new replies.