tags:

views:

71

answers:

3

In the code below, the echo at the top returns true, but the echo at the bottom returns nothing. Apparently the code in between is causing me to lose a reference to the $_post variable?

<?php
echo "in category: ".in_category('is-sidebar'); //RETURNS TRUE
if (!get_option('my_hide_recent'))
{
    $cat=get_cat_ID('top-menu'); 
    $catHidden=get_cat_ID('hidden');
    $myquery = new WP_Query();
    $myquery->query(array(
        'cat' => "-$cat,-$catHidden",
        'post_not_in' => get_option('sticky_posts')
    ));
    $myrecentpostscount = $myquery->found_posts;
    if ($myrecentpostscount > 0) 
    { ?>
    <div class="menu"><h4><?php if ($my_sidebar_heading_recent !=="") { echo $my_sidebar_heading_recent; } else { echo "Recent Posts";} ?></h4><ul>
    <?php 
    global $post; 
    $current_page_recent = get_post( $current_page );
    $myrecentposts = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$catHidden",'showposts' => $my_recent_count));
    foreach($myrecentposts as  $idxrecent=>$post) {
        if($post->ID == $current_page_recent->ID)
            {
                $home_menu_recent = ' class="current_page_item';
            } 
            else 
            {
                $home_menu_recent = ' class="page_item';
            }
            $myclassrecent = ($idxrecent == count($myrecentposts) - 1 ? $home_menu_recent.' last"' : $home_menu_recent.'"'); 
        ?>
    <li<?php echo $myclassrecent ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php 
    } ; if (($myrecentpostscount > $my_recent_count) && $my_recent_count > -1){ ?><li><a href="<?php bloginfo('url'); ?>/site-map">View all</a></li><?php } ?></ul></div>
<?php 
}
} 
global $sitemap;
echo "in category: ".in_category('is-sidebar'); //RETURNS NOTHING
+1  A: 

Your foreach $myrecentposts declares a new variable $post. Use a different name for $post there.

camomileCase
when I change the code I just get the first element repeated over and over until the end of the loop.foreach($myrecentposts as $idxrecent=>$mypost) { if($mypost->ID == $current_page_recent->ID)
Scott B
+2  A: 

Variables in PHP are case-sensitive. This means that $_POST (a predefined variable) is not the same as $_post.

If you really did mean $_post, it's a terrible variable name, as it may confuse things later on.

R. Bemrose
Actually, I get the same output with our without the $_post variable. I've removed it to avoid confusion, but yes, it should have been $_POST.
Scott B
A: 

The special variable that contains the current post is called $post, not $_post. But since that's the default value for in_category() anyway, you don't need to pass it that second parameter.

But you need to add a call to setup_postdata($post) inside that foreach loop to, well, setup the post data. Without it the "magic" functions like the_title() will just keep returning the post data for the original post. Note that that variable must be called $post.

mercator
It works just fine without setup_postdata(). Not sure why, but I've never needed to use that.
Scott B
@Scott, have you solved the problem you commented on camomileCase's answer then? I think that's because of this...
mercator
No, when I change foreach($myrecentposts as $idxrecent=>$post) to... foreach($myrecentposts as $idxrecent=>$myrenamedpost){setup_postdata($myrenamedpost); I still get repeat of the first item in the loop. btw, is there a better way to format code in comments?
Scott B
@Scott, You can surround your code with backticks (\`) in your comments... Your example indeed wouldn't work because the variable passed in to `setup_postdata()` has to be called `$post`.
mercator