我如何获取当前类别的第一篇文章并回显链接?

时间:2018-08-08 08:52:10

标签: php wordpress

我尝试创建一个代码,该代码将从类别中检索第一篇文章并回显链接。

但是,代码始终会成为当前帖子的永久链接。谁能帮我修复它?

    <?php
    global $post;
    $category = get_the_category($post->ID);
    $category = $category[0]->cat_ID;
        $array = new WP_Query(array(
            'category__in' => array($category),
            'post_per_page' => 1,
            'order' => 'asc',
            'orderby' => 'id'

    ));?>
    <?php the_permalink($post->ID); ?>

1 个答案:

答案 0 :(得分:0)

$args = array(
            'numberposts' => 1, 
             'orderby' => 'post_date',
            'order' => 'ASC', 
            'fields'          => 'ids',
            'category' => 8 //or use further logic to check in list
        );
     $post_cus = get_posts($args);
     $first_post_id = $post_cus[0];
     $post_url = get_the_permalink ($first_post_id);