php - 我怎样才能获得当前类别的第一篇文章并回显链接?
问题描述
我尝试创建一个代码,该代码将从一个类别中检索第一个帖子并回显链接。
但是,代码总是获得当前帖子的永久链接。谁能帮我解决它?
<?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); ?>
解决方案
$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);
推荐阅读
- javascript - 为什么 react-router 将先前的路由与新的路由结合起来?
- c# - 如何在 .NET Core 中使用 IUserIdProvider?
- function - Dart:函数的参数表示法
- angular - Angular 和 Firebase:如何设置 Cloud Firestore 安全规则?
- c# - 持有对实体框架对象的字符串属性的引用会阻止它被 GC 收集吗?
- c - 为什么增量操作 ++a++ 不起作用,至少在 C 中?
- javascript - 打印选定的属性表单 json 对象
- java - 长时间延迟 Runnable
- javascript - 伊斯坦布尔 + 代码覆盖 + 摩卡咖啡
- jquery - 使用 jQuery 检查 href 是否包含 DOT