WordPress 文章置顶循环
像素鱼丸
2023-07-04
1027
0

WordPress文章编辑页,设置文章置顶,还需要修改文章调用函数。

<?php
$sticky = get_option('sticky_posts');
rsort( $sticky ); // 文章数组逆向排序
$sticky = array_slice( $sticky, 0, 3); // 限制只有3个置顶文章
$args = array(
    "cat"=>$product_id,
    "posts_per_page" => 8,
    'post__in' => $sticky
);
query_posts($args); while(have_posts()): the_post(); 
?>
    <a class="card" href="<?php the_permalink(); ?>"><?php the_title() ?></a>
<?php endwhile; wp_reset_query(); ?>

显示置顶文章以后,需要循环调用非置顶文章。

<?php
$the_query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>
收藏
打赏
WordPress 评论表单函数 comment_form()
上一篇
WordPress 自定义文章排序
下一篇

发表评论

像素鱼丸
147 文章
0 评论
4 喜欢
最新文章

详解 WordPress 的评论设置

好的,我们来详细梳理并总结 WordPress 中关于文章评论的两个核心控制层级:全局设置和单篇设置。理解这两者的关系(优先级)是管理网站评论的关键。 1. 全局设置 (Global Settings) —— 网站的“默认规则” 这是整个网站评论系统的总开关和默认行为准则。它决定了新发布的文章默认是什么样子的。 位置:WordPress 后台仪表盘 -> 设置 (Settings) -> […]

wp_handle_comment_submission 函数

wp_handle_comment_submission() 是 WordPress 中用于处理评论提交的核心函数之一。它通常在用户提交评论时被调用,负责验证和处理评论数据,并最终将评论插入到数据库中。 ✅ 函数作用 wp_handle_comment_submission() 的主要功能是: 验证用户提交的评论数据(如评论内容、用户名、邮箱等) 检查是否为垃圾评论(通过 Akismet 或其他过 […]

Mirage 主题 v2.89.0 发布

Mirage 主题 v2.89.0 发布 feat 懒加载图片增加灰色背景颜色 refactor 移除一些无用的js文件 fix 优化卡片列表样式 feat 如果设置里删除logo,就直接显示网站名称 fix 修复pc下拉菜单宽度的一个样式bug fix 修复未登录用户取消喜欢时产生的一个错误 fix 优化 wp_vt_star 表结构 下载地址:https://gitee.com/vthemec […]

WordPress set_transient()使用方法和实现

set_transient() 是 WordPress 中用于设置临时数据的函数,它允许你将数据存储在缓存中,这些数据会在指定的时间后自动过期。它是 WordPress 提供的 transient API 的一部分,常用于缓存数据库查询、API 响应或其他计算密集型操作的结果。 一、基本概念 1. 什么是 Transient? Transient 是 WordPress 中的一种缓存机制,类似于 […]
生成中...
扫描二维码
扫描二维码
用户登录