带有缩略图的上一篇/下一篇帖子

时间:2013-07-16 作者:rwzdoorn

关于下一篇/上一篇文章,我还有一个问题。

目前,我正在处理我们的投资组合中的几个项目。每个项目都是一篇带有标题、类别和;特色图片。查看项目时,有4篇下一篇/上一篇文章。

如何显示下一个2和前一个2?如果下一篇或上一篇不存在,我如何只显示4篇之前的文章或4篇接下来的文章(在第一篇和最后一篇文章)?

目前我正在使用twitter引导CSS/HTML:

<div class="row-fluid tab-footer">

        <div class="span3">



                      <div class="row-fluid none">
                           <div class="span3">THUMBNAIL</div>

                           <div class="span9" ><h2>TITLE</h2>

                           <p><?php
                                foreach((get_the_category()) as $childcat) {
                                if (cat_is_ancestor_of(3, $childcat)) {
                                echo \'<a href="\'.get_category_link($childcat->cat_ID).\'">\';
                                 echo $childcat->cat_name . \'</a> \';
                                }}
                                ?>
                            </p> 
                      </div>

        </div> 

        <div class="span3 hidden-phone">

                  <div class="row-fluid none">
                       <div class="span3">THUMBNAIL</div>

                       <div class="span9" ><h2>TITLE</h2>

                       <p>CATEGORY</p> 
                  </div>
        </div>

        <div class="span3 hidden-phone">
                  <div class="row-fluid none">
                       <div class="span3">THUMBNAIL</div>

                       <div class="span9" ><h2>TITLE</h2>

                       <p>CATEGORY</p> 
                  </div>
        </div>

        <div class="span3">   
                  <div class="row-fluid none">
                       <div class="span3">THUMBNAIL</div>

                       <div class="span9" ><h2>TITLE</h2>

                       <p>CATEGORY</p> 
                  </div>
        </div>

</div>

1 个回复
最合适的回答,由SO网友:Hannes 整理而成

您需要发送自定义查询以获取帖子列表。在本例中,我们使用一个自定义的post类型“project”和一个自定义的分类法“sphere”,并在当前sphere中得到一个下一个项目。在您的用例中,您可以将posts\\u per\\u page增加到4。

<?php 
//remember the id and menu_order from current post 
$id=get_the_ID(); 
global $haet_post_order; 
$haet_post_order=$post->menu_order;

//There is only one sphere per project in this example
$custom_terms = get_the_terms($id, \'sphere\');
foreach ($custom_terms AS $term) {
    $sphere=$term->slug;
}

function haet_filter_next_post( $where = \'\' ) {
    global $haet_post_order;
    $where .= \' AND menu_order>\'.$haet_post_order;
    return $where;
}

add_filter( \'posts_where\', \'haet_filter_next_post\' );
$loop = new WP_Query( array( \'post_type\' => \'project\', \'posts_per_page\' => 1,\'orderby\' => \'menu_order\',\'order\' => \'ASC\',\'sphere\'=>$sphere ) );
remove_filter( \'posts_where\', \'haet_filter_next_post\' );

//if there is no next post use the first one
if( !$loop->have_posts() ){
    $loop = new WP_Query( array( \'post_type\' => \'project\', \'posts_per_page\' => 1,\'orderby\' => \'menu_order\',\'order\' => \'ASC\',\'sphere\'=>$sphere ) );
}

while ( $loop->have_posts() ) : $loop->the_post();
// place you code here
?>

<?php  endwhile;  ?>
在while循环中,您可以使用get_the_post_thumbnail(). 您可以在中找到更多描述行this blog post

结束

相关推荐