Get Value of Post before

时间:2014-04-15 作者:Nepo Znat

我在wordpress中有一个正常的循环,在每个帖子中,我都需要检查帖子的值,然后再将其与当前帖子进行比较。

--帖子1--**什么都不做**

--POST 2--**从POST 1获取值**

--POST 3--**从POST 2获取值**

<?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();

                  $description = get_post_meta( get_the_ID( ), \'metabox_description\', true );       
                  echo $description;

                  // Add: echo $description_from_post_before;

        } // end while
    } // end if
?>

2 个回复
SO网友:Rajeev Vyas
<?php 
$previous_post=null;
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
             global $post;
            if($previous_post!=null)
            {
              /// Do what ever you want to do with previous post $previous_post.
            }


                  $description = get_post_meta( get_the_ID( ), \'metabox_description\', true );       
                  echo $description;

                  // Add: echo $description_from_post_before;
            $previous_post = $post;
        } // end while
    } // end if
?>
SO网友:Sormano

您可以使用该功能get_previous_post() 获取上一个post对象。

$prev = get_previous_post();
if ( !empty( $prev ) ) {
   // Do some comparing
}
希望这有帮助

结束

相关推荐

使用POSTS__NOT_IN时,PRE_GET_POSTS返回非属性对象

我正在显示某个类别中的帖子,并希望使用posts\\uu not\\u in显示除第一个之外的所有帖子。下面的代码返回非对象错误的属性。function modify_onthisday_cat( $query ) { if ($query->is_category(7) && $query->is_main_query() ) { global $post, $wp_query; s