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

声明:本站所有文章和图片,如无特殊说明,均为原创发布,转载请注明出处。
随机推荐
JavaScript 数据类型和变量
Node.js 数据库模块 mysql2
WordPress 函数 get_categories() 获取所有分类
JavaScript navigator对象
WordPress 使用 shortcode() 增加编辑器功能
如何使主题支持 Woocommerce
MySQL 删除逗号分隔字段中的某一个值
WordPress 的用户角色和权限