WordPress.org

Forums

How to combine two php (2 posts)

  1. navikk
    Member
    Posted 1 year ago #

    Hello,
    I'd like to combine to php. but I don't know how. The result should be that on the homepage will be displaying 5 posts and one category will be exluded.

    <?php
    if (is_home()) {
    query_posts("showposts=5");
    }
    ?>
    <?php
    if (is_home()) {
    query_posts("cat=-160");
    }
    ?>

    How do I combine it? Thank you!

  2. Dave Naylor
    Member
    Posted 1 year ago #

    You simply add all the arguments you want. This is adapted from the WP_Query codex page:

    <?php
    
    // The Query
    $the_query = new WP_Query( 'cat=-160&posts_per_page=5' );
    
    // The Loop
    if ( $the_query->have_posts() ) {
            echo '<ul>';
    	while ( $the_query->have_posts() ) {
    		$the_query->the_post();
    		echo '<li>' . get_the_title() . '</li>';
    	}
            echo '</ul>';
    } else {
    	// no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.