标签: 置顶文章

  • 每日随机展示10个wordpress置顶文章

    WordPress 置顶文章是博主根据自己的需要设置的,通常用于展示重要或热门的文章。

    以下是一个示例代码,用于在 WordPress 主题中展示 10 个置顶文章:

    <?php
    // 查询置顶文章
    $sticky = get_option('sticky_posts');
    $args = array(
        'post__in' => $sticky,
        'posts_per_page' => 10, // 显示 10 篇置顶文章
        'ignore_sticky_posts' => 1 // 忽略置顶设置,直接获取置顶文章
    );
    $query = new WP_Query($args);
    
    // 判断是否有置顶文章
    if ($query->have_posts()) {
        echo '<div class="sticky-posts">';
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <div class="sticky-post">
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p><?php the_excerpt(); ?></p>
            </div>
            <?php
        }
        echo '</div>';
    } else {
        echo '没有置顶文章';
    }
    
    // 重置查询
    wp_reset_postdata();
    ?>

    你可以将这段代码添加到你的 WordPress 主题文件中,比如 sidebar.php 或 index.php,根据你的需求调整显示位置。