0

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>
2
  • I'm not exactly sure what your asking, but I'm pretty certain its not a syntax issue. Commented Jan 12, 2012 at 16:04
  • Hi MrGlass, I posted the original code, so the code has not been changed yet to reflect my desired change. I just don't know what the if/then condition should be in order to prevent Wordpress from displaying post #122 Commented Jan 12, 2012 at 16:09

3 Answers 3

4

http://codex.wordpress.org/Class_Reference/WP_Query

//Display all posts but NOT the specified ones:

$query = new WP_Query( array( 'post__not_in' => array( 122 ) ) );
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Anand, would I add on this piece of code, and if so, where should I add this? Sorry if I'm confused.
It would replace the wpquery at the top of the cod e you posted. You would need to modify it first, to include the parameters from your wpquery
So you are saying that I would first replace the code, and then make additional modifications elsewhere? Or would replacing it simply do the trick? I apologize for the confusion once again.
See my answer, which combines the wpquery from your code with the query here.
Next time add more explanation
3

Try replacing the query at the top with this:

$r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' =>true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'post__not_in' => array( 122 )));

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

4 Comments

MrGlass, fantastic answer! It not only worked, but I added a few more posts to that array in order to prevent them from displaying, which is what I wanted in the end anyway.
MrGlass, can you tell me in lamens terms what this piece of code was telling the server? I don't understand how the array played its role....
Yes, I knew that actually, and Anand does deserve credit for the initial suggestion. How do I give two people credit? :) haha
I added a short explanation, hope it helps.
0

i guess something like this:

<?php  while ($r->have_posts()) : $r->the_post();?>
    $id = the_ID();
    if ($id != 122){
        <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; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.