• Resolved ventus85

    (@ventus85)


    Hi all.

    The version of my WordPress is 3.5.1

    I modified my front-page file.
    This code gets the most recent post from the loop.
    I want to exclude a category.
    The id of the category is ‘6’.

    <?php
    $args = array( 'numberposts' => 1, 'exclude' => '6' );
    $lastposts = get_posts( $args );
    foreach($lastposts as $post) : setup_postdata($post); ?>
    	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    	<?php the_content(); ?>
    <?php endforeach; ?>

    I also tried
    $args = array( 'numberposts' => 1, 'cat => -6' );

    But it does not work: in the home page show all, including the post of the excluded category.

    I read the documentation and this old topic, but these pages didn’t help me

    I wonder where I am going wrong.

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • I also tried
    $args = array( 'numberposts' => 1, 'cat => -6' );

    might be a simple syntax error; change the single quotes like so:

    $args = array( 'numberposts' => 1, 'cat' => -6 );

    Thread Starter ventus85

    (@ventus85)

    Hi all.

    Thank you for your answer.

    Ops, the code is wrong in the post, but in the file it is correct, I tried

    $args = array( 'numberposts' => 1, 'cat' => -6 );

    and

    $args = array( 'numberposts' => 1, 'cat' => '-6' );

    but these solutions don’t work

    🙁

    Thread Starter ventus85

    (@ventus85)

    $args = array( 'numberposts' => 1, 'cat' => -6 );

    The code is correct.
    Yesterday when I wrote this code, maybe I must have been sleeping 😛

    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘get_posts with excluding category’ is closed to new replies.