• Resolved devonanne

    (@devonanne)


    I’m using this code on a custom template to display a list of post titles:

    <ul class="category-list">
    <?php
    // we add this, to show all posts in our
    // Glossary sorted alphabetically
    $args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' );
    $glossaryposts = get_posts( $args );
    // here comes The Loop!
    foreach( $glossaryposts as $post ) :	setup_postdata($post);  ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    This works nicely, however I want to limit my list to a certain category. I’ve found other solutions for doing this, but not one that works with the code I currently have. How can I modify this to limit my list to a specific category?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi, to get a list of posts in a specific category you can specify the category ID in your $args array, try this

    $args = array( 'posts_per_page' => -1, 'category'=> 1, 'orderby'=> 'title', 'order' => 'ASC' );

    where you should replace
    ‘category’=> 1
    with
    ‘category’=> ID_of_your_category

    Here’s an example with the complete list of parameters that you can use in the $args array

    <?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 find more examples here
    https://codex.www.ads-software.com/Template_Tags/get_posts

    Hope it helps:)

    Thread Starter devonanne

    (@devonanne)

    This is exactly what I needed, thank you! Got it working perfectly now ??

    Thread Starter devonanne

    (@devonanne)

    Marked as resolved

    Glad to hear that:)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Limit list of post titles to single catgegory?’ is closed to new replies.