Forums

[resolved] Help with excluding category (15 posts)

  1. sclausen
    Member
    Posted 1 year ago #

    Right now this code gets the most recent post from the loop,
    and display the title and thumbnail..

    Is it possible to exclude a category, from this function?

    Heres my code:

    <a href="<?php the_permalink(); ?>">
    	<?php get_the_post_thumbnail($post->ID); ?>
    	<div id="newestpost" class="bg50 absolute">
    	<h1 href="<?php the_permalink(); ?>"><?php the_title(); ?></h1>
    	</div>
    </a>
  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

  3. sclausen
    Member
    Posted 1 year ago #

    @esmi
    I've tried that already, but i could'nt make it work..

    Perhaps you could give me an example?

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    See this Codex page for an example using query_posts.

  5. sclausen
    Member
    Posted 1 year ago #

    It doesnt work..

    The code is placed outside the loop..
    Wich makes it a bit more difficult..

  6. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Sorry? The code snippet you posted above clearly shows part of the Loop.

  7. sclausen
    Member
    Posted 1 year ago #

    It is actually in the header.php..

    It displays the most recent post in the topbanner.
    And the most recent post is then removed from the loop with:
    <?php query_posts('posts_per_page=6&offset=1'); ?>

  8. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    In that case, you have multiple Loops on the 1 page, so you would be better off using get_posts or WP_Query for the secondary queries. Same arguments but get_posts uses a foreach loop.

    http://codex.wordpress.org/Function_Reference/get_posts

  9. sclausen
    Member
    Posted 1 year ago #

    Ok, now it looks like this:

    <?php
    $args = array( 'numberposts' => 1, 'orderby' => 'recent' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
    	<li><a href="<?php the_permalink(); ?>">
    	<?php the_post_thumbnail(); ?>
    	<div id="newestpost" class="bg50 absolute">
    	<h1 href="<?php the_permalink(); ?>"><?php the_title(); ?></h1>
    	</div></a></li>
    <?php endforeach; ?>

    Now, what do i do to exclude a category?
    It doesnt seem to work if i use:
    <?php query_posts( '&cat=-7,-8,-9'); ?>

  10. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    $args = array( 'numberposts' => 1, 'orderby' => 'recent', 'cat => -7,-8,-9' );

  11. sclausen
    Member
    Posted 1 year ago #

    Wow! that simple..

    Thanks for the help, i really appreciate it! :)

  12. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Glad it's working now. :-)

  13. sclausen
    Member
    Posted 1 year ago #

    Just found out that the new code causes the next_post_link and previous_post_link to not work properly..

    If i change the "$rand_posts as $post"-part to for example, "$posts as $post" the tags work again, but then the code doesnt work.. :(

  14. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try adding <?php wp_reset_query();?> after <?php endforeach; ?>

  15. sclausen
    Member
    Posted 1 year ago #

    Its works now :) Thank you :)

Topic Closed

This topic has been closed to new replies.

About this Topic