wordpress最新文章调用代码

wordpress最新文章调用代码

方案一:普通文章+发布时间排

<!-- 最新文章 -->
<div class="sidebar-widget">
    <h3>🆕 最新资讯</h3>
    <ul>
        <?php
        $latest_posts = new WP_Query(array(
            'post_type'      => 'post',
            'posts_per_page' => 5,
            'post__not_in'   => array($pid),
            'ignore_sticky_posts' => 1,
            'no_found_rows'  => true
        ));
        if ($latest_posts->have_posts()) :
            while ($latest_posts->have_posts()) : $latest_posts->the_post();
        ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <span class="post-date"><?php echo get_the_date('Y/m/d'); ?></span>
        </li>
        <?php endwhile; wp_reset_postdata(); else : ?>
        <li style="color:#7b8ba3;font-size:13px;">暂无最新文章</li>
        <?php endif; ?>
    </ul>
</div>

优化完整版(文字列表版,直接替换)

<!-- 最新文章 -->
<div class="sidebar-widget">
    <h3>🆕 最新资讯</h3>
    <ul>
        <?php
        $latest_posts = new WP_Query(array(
            'post_type'      => 'post',
            'posts_per_page' => 5,
            'post__not_in'   => array(get_the_ID()), // 统一用 get_the_ID()
            'ignore_sticky_posts' => 1,
            'no_found_rows'  => true,
            'update_post_meta_cache' => false,
            'update_post_term_cache' => false
        ));
        if ($latest_posts->have_posts()) :
            while ($latest_posts->have_posts()) : $latest_posts->the_post();
                // 标题截取,防止溢出
                $title = get_the_title();
                $short_title = mb_strlen($title) > 28 ? mb_substr($title, 0, 28) . '…' : $title;
        ?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr($title); ?>">
                <?php echo esc_html($short_title); ?>
            </a>
            <span class="post-date"><?php echo esc_html(get_the_date('Y/m/d')); ?></span>
        </li>
        <?php endwhile; wp_reset_postdata(); else : ?>
        <li style="color:#7b8ba3;font-size:13px;">暂无最新文章</li>
        <?php endif; ?>
    </ul>
</div>

【缩略图 + 标题】图文样式(常用侧边栏样式)

<!-- 最新文章 -->
<div class="sidebar-widget">
    <h3>🆕 最新资讯</h3>
    <ul class="sidebar-latest-list">
        <?php
        $latest_posts = new WP_Query(array(
            'post_type'      => 'post',
            'posts_per_page' => 5,
            'post__not_in'   => array(get_the_ID()),
            'ignore_sticky_posts' => 1,
            'no_found_rows'  => true,
            'update_post_meta_cache' => false,
            'update_post_term_cache' => false
        ));
        if ($latest_posts->have_posts()) :
            while ($latest_posts->have_posts()) : $latest_posts->the_post();
                $title = get_the_title();
                $short_title = mb_strlen($title) > 24 ? mb_substr($title, 0, 24) . '…' : $title;
        ?>
        <li class="latest-item">
            <?php if(has_post_thumbnail()): ?>
            <div class="latest-thumb">
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('thumbnail', ['alt' => esc_attr($title)]); ?>
                </a>
            </div>
            <?php endif; ?>
            <div class="latest-info">
                <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr($title); ?>">
                    <?php echo esc_html($short_title); ?>
                </a>
                <span class="post-date"><?php echo esc_html(get_the_date('Y/m/d')); ?></span>
            </div>
        </li>
        <?php endwhile; wp_reset_postdata(); else : ?>
        <li style="color:#7b8ba3;font-size:13px;">暂无最新文章</li>
        <?php endif; ?>
    </ul>
</div>

配套基础 CSS(加到主题自定义 CSS)

.sidebar-widget {
    margin-bottom: 30px;
}
.sidebar-widget h3 {
    font-size: 16px;
    padding-bottom: 10px;
    border-bottom: 1px solid #eee;
    margin-bottom: 12px;
}
.sidebar-widget ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
.sidebar-widget ul li {
    padding: 8px 0;
    border-bottom: 1px dashed #efefef;
    display: flex;
    flex-direction: column;
}
.sidebar-widget ul li a {
    color: #333;
    text-decoration: none;
}
.sidebar-widget ul li a:hover {
    color: #007cba;
}
.post-date {
    font-size: 12px;
    color: #999;
    margin-top: 4px;
}

方案二:单个分类 ID(不含子分类)

示例:只调取分类 ID=6,不包含它的子分类

<!-- 最新文章 - 单个分类ID【不含子分类】 -->
<div class="sidebar-widget">
    <h3>🆕 最新资讯</h3>
    <ul>
        <?php
        $current_id = get_the_ID();
        $latest_posts = new WP_Query(array(
            'post_type'      => 'post',
            'posts_per_page' => 5,
            'cat'            => 6, // 单个分类ID
            'post__not_in'   => array($current_id),
            'ignore_sticky_posts' => 1,
            'no_found_rows'  => true,
            'update_post_meta_cache' => false,
            'update_post_term_cache' => false
        ));
        if ($latest_posts->have_posts()) :
            while ($latest_posts->have_posts()) : $latest_posts->the_post();
                $title = get_the_title();
                $short_title = mb_strlen($title) > 28 ? mb_substr($title, 0, 28) . '…' : $title;
        ?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr($title); ?>">
                <?php echo esc_html($short_title); ?>
            </a>
            <span class="post-date"><?php echo esc_html(get_the_date('Y/m/d')); ?></span>
        </li>
        <?php endwhile; wp_reset_postdata(); else : ?>
        <li style="color:#7b8ba3;font-size:13px;">暂无最新文章</li>
        <?php endif; ?>
    </ul>
</div>

方案三:多个分类 ID(不含子分类)

示例:调取分类 ID 6,12,18,不含各自子分类

<!-- 最新文章 - 多个分类ID【不含子分类】 -->
<div class="sidebar-widget">
    <h3>🆕 最新资讯</h3>
    <ul>
        <?php
        $current_id = get_the_ID();
        $latest_posts = new WP_Query(array(
            'post_type'      => 'post',
            'posts_per_page' => 5,
            'category__in'   => array(6,12,18), // 多个分类ID
            'post__not_in'   => array($current_id),
            'ignore_sticky_posts' => 1,
            'no_found_rows'  => true,
            'update_post_meta_cache' => false,
            'update_post_term_cache' => false
        ));
        if ($latest_posts->have_posts()) :
            while ($latest_posts->have_posts()) : $latest_posts->the_post();
                $title = get_the_title();
                $short_title = mb_strlen($title) > 28 ? mb_substr($title, 0, 28) . '…' : $title;
        ?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr($title); ?>">
                <?php echo esc_html($short_title); ?>
            </a>
            <span class="post-date"><?php echo esc_html(get_the_date('Y/m/d')); ?></span>
        </li>
        <?php endwhile; wp_reset_postdata(); else : ?>
        <li style="color:#7b8ba3;font-size:13px;">暂无最新文章</li>
        <?php endif; ?>
    </ul>
</div>

方案四:单个分类 ID【包含所有子分类】

示例:父分类 6,自动包含 6 下面所有层级子分类

必须先引入上方 get_all_category_child_ids() 函数

<!-- 最新文章 - 单个分类ID【包含全部子分类】 -->
<div class="sidebar-widget">
    <h3>🆕 最新资讯</h3>
    <ul>
        <?php
        $current_id = get_the_ID();
        $parent_cat_id = 6;
        // 获取父分类+所有子分类ID数组
        $cat_ids = get_all_category_child_ids($parent_cat_id);

        $latest_posts = new WP_Query(array(
            'post_type'      => 'post',
            'posts_per_page' => 5,
            'category__in'   => $cat_ids,
            'post__not_in'   => array($current_id),
            'ignore_sticky_posts' => 1,
            'no_found_rows'  => true,
            'update_post_meta_cache' => false,
            'update_post_term_cache' => false
        ));
        if ($latest_posts->have_posts()) :
            while ($latest_posts->have_posts()) : $latest_posts->the_post();
                $title = get_the_title();
                $short_title = mb_strlen($title) > 28 ? mb_substr($title, 0, 28) . '…' : $title;
        ?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr($title); ?>">
                <?php echo esc_html($short_title); ?>
            </a>
            <span class="post-date"><?php echo esc_html(get_the_date('Y/m/d')); ?></span>
        </li>
        <?php endwhile; wp_reset_postdata(); else : ?>
        <li style="color:#7b8ba3;font-size:13px;">暂无最新文章</li>
        <?php endif; ?>
    </ul>
</div>

方案四:多个分类 ID【各自包含自身子分类】

示例:父分类 6、12,同时读取 6 所有子分类 + 12 所有子分类

必须先引入上方 get_all_category_child_ids() 函数

<!-- 最新文章 - 多个分类ID【各自包含全部子分类】 -->
<div class="sidebar-widget">
    <h3>🆕 最新资讯</h3>
    <ul>
        <?php
        $current_id = get_the_ID();
        $parent_cat_list = array(6,12); // 多个父分类ID

        $cat_ids = array();
        foreach($parent_cat_list as $pid){
            $cat_ids = array_merge($cat_ids, get_all_category_child_ids($pid));
        }
        // 去重ID,防止重复
        $cat_ids = array_unique($cat_ids);

        $latest_posts = new WP_Query(array(
            'post_type'      => 'post',
            'posts_per_page' => 5,
            'category__in'   => $cat_ids,
            'post__not_in'   => array($current_id),
            'ignore_sticky_posts' => 1,
            'no_found_rows'  => true,
            'update_post_meta_cache' => false,
            'update_post_term_cache' => false
        ));
        if ($latest_posts->have_posts()) :
            while ($latest_posts->have_posts()) : $latest_posts->the_post();
                $title = get_the_title();
                $short_title = mb_strlen($title) > 28 ? mb_substr($title, 0, 28) . '…' : $title;
        ?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr($title); ?>">
                <?php echo esc_html($short_title); ?>
            </a>
            <span class="post-date"><?php echo esc_html(get_the_date('Y/m/d')); ?></span>
        </li>
        <?php endwhile; wp_reset_postdata(); else : ?>
        <li style="color:#7b8ba3;font-size:13px;">暂无最新文章</li>
        <?php endif; ?>
    </ul>
</div>

优化版

<!-- 最新文章 - 多个分类ID【各自包含全部子分类】 -->
<div class="sidebar-widget">
    <h3>🆕 最新资讯</h3>
    <ul>
        <?php
        // ==========内置递归函数(复制整个代码块就生效,不用放头部)==========
        if (!function_exists('get_all_category_child_ids')) {
            function get_all_category_child_ids($cat_id){
                $ids = array((int)$cat_id);
                $children = get_term_children($cat_id, 'category');
                // 容错:判断是否为错误对象
                if(is_wp_error($children) || empty($children)){
                    return $ids;
                }
                foreach($children as $cid){
                    $ids = array_merge($ids, get_all_category_child_ids($cid));
                }
                return $ids;
            }
        }
        // =================================================================

        $current_id = get_the_ID();
        $parent_cat_list = array(926,1); // 修改成你的父分类ID

        $cat_ids = array();
        foreach($parent_cat_list as $pid){
            $cat_ids = array_merge($cat_ids, get_all_category_child_ids((int)$pid));
        }
        // 去重 + 过滤空值
        $cat_ids = array_unique(array_filter($cat_ids));

        $latest_posts = new WP_Query(array(
            'post_type'      => 'post',
            'posts_per_page' => 5,
            'category__in'   => $cat_ids,
            'post__not_in'   => array($current_id),
            'ignore_sticky_posts' => 1,
            'no_found_rows'  => true,
            'update_post_meta_cache' => false,
            'update_post_term_cache' => false
        ));
        if ($latest_posts->have_posts()) :
            while ($latest_posts->have_posts()) : $latest_posts->the_post();
                $title = get_the_title();
                $short_title = mb_strlen($title) > 28 ? mb_substr($title, 0, 28) . '…' : $title;
        ?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr($title); ?>">
                <?php echo esc_html($short_title); ?>
            </a>
            <span class="post-date"><?php echo esc_html(get_the_date('Y/m/d')); ?></span>
        </li>
        <?php endwhile; wp_reset_postdata(); else : ?>
        <li style="color:#7b8ba3;font-size:13px;">暂无最新文章</li>
        <?php endif; ?>
    </ul>
</div>

使用注意事项

  1. 递归函数 get_all_category_child_ids() 只需要在页面顶部写一次,不要复制多份;
  2. 修改数字 6,12,18 换成你自己的分类 ID;
  3. 如果想要【图文带缩略图版本】,我可以把上面 4 套全部替换成带缩略图结构;
  4. category__in = 文章属于任意一个分类就显示(OR 关系) 如果你需要「文章必须同时属于多个分类(AND)」,参数要用 category__and