WordPress 文章置顶循环
WordPress 开发文档
收录了这篇文章

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

<?php
$sticky_arr = get_option( 'sticky_posts' );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// rsort( $sticky ); // 文章数组逆向排序
// $sticky = array_slice( $sticky, 0, 3); // 限制只有3个置顶文章

$args = array(
    'cat' => $cat,
    'post__in' => $sticky_arr,
    'paged' => $paged
);
// 置顶文章
$query_posts = new WP_Query( $args );
?>

<?php if ( $sticky_arr && $query_posts->have_posts() ) : ?>
    <?php while ( $query_posts->have_posts() ) : ?>
        <?php
        $query_posts->the_post();
        $rt_post_type = get_post_meta( $post->ID, 'rt_post_type', true );
        get_template_part( 'templates/media/media' );
        ?>
    <?php endwhile; ?>
<?php endif; ?>

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

<?php
$args = array(
    'cat' => $cat,
    'posts_per_page' => get_option('posts_per_page'),
    'ignore_sticky_posts' => true,
    'post__not_in' => $sticky_arr,
    'paged' => $paged
);
// 正常文章
$query_posts = new WP_Query( $args );
?>

<?php if ( $query_posts->have_posts() ) : ?>
    <?php while ( $query_posts->have_posts() ) : ?>
        <?php
        $query_posts->the_post();
        $rt_post_type = get_post_meta( $post->ID, 'rt_post_type', true );
        get_template_part( 'templates/media/media' );
        ?>
    <?php endwhile; ?>
<?php endif; ?>  

 

修改时间 2024-06-06

声明:本站所有文章和图片,如无特殊说明,均为原创发布。商业转载请联系作者获得授权,非商业转载请注明出处。
随机推荐
WordPress 的用户角色和权限
WordPress 一键从HTTP转换到HTTPS
如何使用命令修改 MySQL 数据库名称
JavaScript audio 教程
HTML select 下拉列表
WordPress 中如何在主题和插件中使用
Node.js 使用 Jest 和 supertest 做接口测试
JavaScript 窗口/文档事件