I’m having some problems trying to format an If/Then condition in a WordPress widget. In this case I want to say, “display all the recent posts titles in the sidebar, except for post ID 122. I’m not even sure exactly what should be the if/then condition should be, then there are other if/then statement already in the code, so I’m just confused as to where it should go.
Here’s the ORIGINAL code found in the ‘widget.php’ file, where all the conditions of the sidebar widgets are stored:
$r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' =>true,
'post_status' => 'publish', 'ignore_sticky_posts' => true));
if ($r->have_posts()) :
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul>
<?php while ($r->have_posts()) : $r->the_post();?>
<li>
<a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a>
</li>
<?php endwhile; ?>
</ul>
Try replacing the query at the top with this:
edit as requested, a brief explanation:
WP_Query is a function used to grab a lists of posts in WordPress. it takes an array of parameters, that allow you to specify extra requirements for stuff int hat list, among other things. For example, your original query had
'post_status' => 'publish'in its array, so it should only retrieve posts that are published. I added the option suggested by Anand,'post__not_in' => array( 122 ), which specifies that posts with the id(s) specified should not be in the returned list.It is a very powerful and complicated function, you can read more about hto to use it here: http://codex.wordpress.org/Class_Reference/WP_Query