WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding specific category into an existing query (4 posts)

  1. JMoe
    Member
    Posted 1 year ago #

    Here is the code to show the "Latest News" posts on my home page.

    <?php if ( of_get_option('gg_headlines') ) {

    $query = new WP_Query( );
    $post_number = of_get_option('gg_headlines');
    $query->query('posts_per_page=' . $post_number);
    while ($query->have_posts()) : $query->the_post();
    ?>

    I would like to have my home page only show category 4 (Latest News) and not all categories on my blog. How do I go about adding cat=4 in this code?

    Thanks in advance!

  2. Andrew Bartel
    Member
    Posted 1 year ago #

    Give this a shot.

    $query->query('posts_per_page=' . $post_number . '&cat=4');
  3. JMoe
    Member
    Posted 1 year ago #

    Yes! This works perfectly - thank you so much, Andrew. Such a simple solution but I've searched on this for hours and couldn't figure it out :o

  4. Andrew Bartel
    Member
    Posted 1 year ago #

    Sure thing, check out the documentation for WP_Query too, I still refer to it constantly.

    Please mark as resolved and happy coding!

Topic Closed

This topic has been closed to new replies.

About this Topic