0

我正在按自定义字段值的降序对帖子进行排序,我想知道是否有办法按降序查找第 n 个帖子。

例如,顺序是:

1st from top: id = 9
2nd from top: id = 5
3rd from top: id = 6

现在,我get_template_part()用来显示帖子。

我想知道是否有get_template_part_of_post(3rd-from-top).

<div class="onethird">

                    <?php


                    $count_posts = wp_count_posts("ott_products", "");
                    $published_posts_orig = $count_posts->publish;
                    $published_posts = $published_posts_orig +  (3 - ($published_posts_orig % 3));


                    $i = 0;

                    if ( have_posts()) : while($query->have_posts()) :

                        echo $i . " " . $published_posts;
                        $i = $i + 3;
                        $query->the_post();

                        get_template_part( 'content', 'category' );

                        if ( $i % 3 === 2 ) :
                            if ( ($i - 2 == $published_posts) ) :
                                $i = 3;
                        endif; endif;

                        if ( $i % 3 === 1 ) :
                            if ( ($i - 1 == $published_posts) ) :
                                echo "</div><div class='onethird last'>";
                                $i = 2;
                        endif; endif;

                        if ( $i % 3 === 0 ) :
                            if ( ($i == $published_posts) ) :
                                echo "</div><div class='onethird'>";
                                $i = 1;
                        endif; endif; 


                    endwhile;

                    else :

                        get_template_part( 'no-results', 'archive' );

                    endif;  

                    ?>


            </div>

这是我目前正在使用的。这将帖子分为三列。

该变量i将本来应该在三列中从上到下的内容从左到右转换。

以前,我的帖子显示如下:

(Total 9 posts)
1  4  7
2  5  8
3  6  9

有了它,我可以i

(Total n posts)
1  2  3
4  5  6
...

现在,问题是我无法i显示帖子。这些帖子仍然排在第一位。

4

3 回答 3

1

获取nth帖子的最简单方法是执行以下操作:

global $posts;

// This gets your nth level post object.
if( isset( $posts[ $nth_post ] ) )
    echo $posts[ $nth_post ]->post_title;

我希望这有帮助。:)

于 2013-02-08T16:14:57.377 回答
0

您可以先使用 total_posts = wp_count_posts() 来统计帖子的数量。

然后,您必须运行“循环”并为每个帖子保留一个计数器,当该计数器达到 total_posts - N 时,执行您想要的操作:

伪代码:

total_posts = wp_count_posts();
count = 0;
while(have_posts()) {
   count++;
   if (count = total_posts - N) {
       // ACTION       
   }
   the_post();
}
于 2013-02-08T15:58:14.703 回答
0

get_template_part()正如它所说的那样,它会获得一个位于您的主题文件夹中的模板。它接受的唯一参数是 slug 和名称(参见WordPress codex

如果我理解正确,您想每次都获取第三个帖子吗?最简单的方法是在模板文件中设置计数器和条件,可能是loop-something.php.

$i = 0;

if ( have_posts()): 

while (have_posts()) : the_post();

  if ($i % 3 == 0):
    // Do something different, this is the first column.
    // I propose:
    $column = 1;

  elseif ($i % 3 == 1):
    // Do something different, this is the second column.
    $column = 2;

  elseif ($i % 3 == 2):
    // Do something different, this is the third column.
    $column = 3;
  endif;

  echo '<div class="column-'.$column.'">';
  // the post
  echo '</div>';

  $i++;

endwhile;

else:

  get_template_part( 'no-results', 'archive' );

endif;
于 2013-02-08T16:01:16.293 回答