wordpress首页获取全站置顶帖子按发布时间排序

wordpress首页获取全站置顶帖子按发布时间排序

2023.05.11 05:48 62 0
主页 / 前端 / wordpress首页获取全站置顶帖子按发布时间排序

wordpress 首页获取全站置顶帖子按发布时间排序

这段代码是首页 5 篇推荐帖子,获取置顶帖子

<section class="container py-5 my-lg-5">
    <div class="row mb-5">
        <div class="col">
            <span class="text-uppercase">Blog recommend</span>
            <h2><span class="iconfont icon-fenlei me-1"></span>推荐</h2>
        </div>
        <div class="col-4 d-flex align-items-center justify-content-end">
            <a class="text-muted"  href="https://98zx.net/new-article">
                <small class="text-body-secondary">查看更多<span class="iconfont icon-chakangengduo ms-1"></span></small>
            </a>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12 col-lg-5 mb-4 mb-lg-0 d-none d-lg-block">
            <?php
            $query_args = array(
                'no_found_rows' => true,
                'posts_per_page' => 1,
                'post__in' => get_option('sticky_posts'),
                'ignore_sticky_posts' => 1
            );
            $query = new WP_Query($query_args);
            
            if ($query->have_posts()) :
                $query->the_post();
                ?>
                <a href="<?php the_permalink(); ?>">
                    <div class="card text-bg-dark h-100">
                        <img src="<?php echo catch_the_image(); ?>" class="card-img h-100 object-fit-cover">
                        <div class="card-img-overlay h-100 d-flex flex-column justify-content-end p-4">
                            <h5 class="h-2x"><?php the_title();?></h5>
                            <p class="h-2x"><?php echo wp_trim_words(get_the_content(), 40); ?></p>
                            <small class="text-secondary">
                                <span class="iconfont icon-shijian me-1"></span><?php echo timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))); ?>
                                <span class="iconfont icon-liulan ms-2 me-1"></span><?php post_views('', ''); ?>
                            </small>
                        </div>
                    </div>
                </a>
                <?php
            endif;
            wp_reset_postdata();
            ?>
        </div>
        <div class="col-md-12 col-lg-7">
            <div class="row row-cols-1 row-cols-md-2 row-cols-lg-2 g-4">
                <?php
                $query_args = array(
                    'no_found_rows' => true,
                    'posts_per_page' => 4,
                    'post__in' => get_option('sticky_posts'),
                    'ignore_sticky_posts' => 1,
                    'offset' => 1
                );
                $query = new WP_Query($query_args);
                
                if ($query->have_posts()) :
                    while ($query->have_posts()) :
                        $query->the_post();
                        get_template_part('mokuai/fangxingfengmian');
                    endwhile;
                endif;
                wp_reset_postdata();
                ?>
            </div>
        </div>
    </div>
</section>
评论(0) 评论本文