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>
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?
See this Codex page for an example using query_posts.
sclausen
Member
Posted 1 year ago #
It doesnt work..
The code is placed outside the loop..
Wich makes it a bit more difficult..
Sorry? The code snippet you posted above clearly shows part of the Loop.
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'); ?>
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
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'); ?>
$args = array( 'numberposts' => 1, 'orderby' => 'recent', 'cat => -7,-8,-9' );
sclausen
Member
Posted 1 year ago #
Wow! that simple..
Thanks for the help, i really appreciate it! :)
Glad it's working now. :-)
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.. :(
Try adding <?php wp_reset_query();?> after <?php endforeach; ?>
sclausen
Member
Posted 1 year ago #
Its works now :) Thank you :)