$ _POST Değişkeni referans kaybetmek?

3 Cevap php

Aşağıdaki kodda, üst yankı döndürür true, ancak altta yankı hiçbir şey döndürür. Görünüşe arasındaki kod bana $ _POST değişkeni bir başvuru kaybetmek neden oluyor?

<?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

3 Cevap

Geçerli mesajı içeren özel değişken denir $post, değil $_post. Bunun için varsayılan değer beri Ama in_category() Neyse, bunu bu ikinci parametre geçmesi gerekmez.

Ama you need to add a call to setup_postdata($post) içerde foreach döngü to, well, setup the post data. Without it the "magic" functions like the_title() sadece orijinal yazı için yazılan verileri dönmeye devam edecektir. Bu değişken must çağrılabilir unutmayın $post.

PHP Değişkenler harfe duyarlıdır. Bu $_POST (a predefined variable) $_post ile aynı olduğu anlamına gelir.

Eğer gerçekten ortalama yaptım sonradan şeyleri karıştırmayın olabilir $_post, bu korkunç bir değişken adı bulunuyor.

Sizin foreach $ myrecentposts yeni bir değişken $ yazı bildirir. $ Orada yazı için farklı bir ad kullanın.