WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to display posts under one catagory (3 posts)

  1. dubbinz
    Member
    Posted 1 year ago #

    Hi, I am currently using this snippet which I found on the net in a template

    <?php
             $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
             $args = array (
                'posts_per_page' => $posts_per_page,
                'post_type' => 'post',
                'orderby' => 'title',
                'order' => 'ASC',
                'paged' => $paged
             );
             query_posts($args);
             if ( have_posts() ) {
                $in_this_row = 0;
                while ( have_posts() ) {
                   the_post();
                   $first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));
                   if ($first_letter != $curr_letter) {
                      if (++$post_count > 1) {
                         end_prev_letter();
                      }
                      start_new_letter($first_letter);
                      $curr_letter = $first_letter;
                   }
                   if (++$in_this_row > $posts_per_row) {
                      end_prev_row();
                      start_new_row();
                      ++$in_this_row;  // Account for this first post
                   } ?>
                   <div class="title-cell"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
                <?php }
                end_prev_letter();
                ?>
                <div class="navigation">
                   <div class="alignleft"><?php next_posts_link('&laquo; Higher Letters') ?></div>
                   <div class="alignright"><?php previous_posts_link('Lower Letters &raquo;') ?></div>
                </div>
             <?php } else {
                echo "<h2>Sorry, no posts were found!</h2>";
             }
             ?>
    
          </div><!-- End id='a-z' -->

    Can anyone tell me how to edit it to show only posts from 1 category?

  2. x500.net
    Member
    Posted 1 year ago #

    add 'category_name' => 'your-desired-category-name' to your $args array.
    more info here

  3. dubbinz
    Member
    Posted 1 year ago #

    Thanks for the quick response, this has fixed the issue. Just to clarify if someone needs this for future reference I changed the array to this and it has done the trick.

    $args = array (
                'posts_per_page' => $posts_per_page,
                'post_type' => 'post',
    	    'category_name' => 'category-slug-here',
                'orderby' => 'title',
                'order' => 'ASC',
                'paged' => $paged

Topic Closed

This topic has been closed to new replies.

About this Topic