简体   繁体   English

显示当前类别的最早的WordPress帖子

[英]Show earliest wordpress post of current category

I'm trying to insert a link to a first post of the current category in "single" template, outside of the post query. 我正在尝试在帖子查询之外的“单个”模板中插入指向当前类别的第一篇文章的链接。

Found this piece of code which originally would list all posts in the current category, which I figured would later be cropped. 找到了这段代码,该代码最初将列出当前类别中的所有帖子,我认为稍后会删除。

It works fine but I can't list posts in ascending order. 它工作正常,但我无法按升序列出帖子。

$category = get_the_category();
foreach ($category as $cat)
{
    query_posts( array ( 'cat' => $cat->cat_ID, '&order=ASC', 'posts_per_page' => 1  ) );
    echo '<div class="post">';
    echo '<h2>'.$cat->cat_name.'</h2>';
    echo '<ul>';
    while (have_posts())
    {
        the_post();
        echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    }
    echo '</ul>';
    $category_id = get_cat_ID($cat->cat_name);
    $category_link = get_category_link($category_id);
    echo '<div class="paging">';
    echo '<a href="'.$category_link.'" title="'.$cat->cat_name.'">More Post from '.$cat->cat_name.'</a>';
    echo '</div>';
    echo '</div>';
}

See this example from the docs for query_posts() : 请参阅文档中的query_posts()示例:

query_posts( array( 'category__and' => array(1,3), 'posts_per_page' => 2, 'orderby' => 'title', 'order' => 'DESC' ) );

Try changing your query_posts() call to this: 尝试将您的query_posts()调用更改为此:

query_posts( array ( 'cat' => $cat->cat_ID, 'order' => 'ASC', 'posts_per_page' => 1  ) );

You may also have to add the orderby attribute. 您可能还必须添加orderby属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM