建站历程 · 2025年2月11日 0

WordPress如何设置首页仅显示某个分类下的最新文章

目前的WordPress都是显示整个站点内最新的文章。而我目前做的站点 Promot Note 除了分享我的Prompt工程师相关笔记之外,我也想把我建站的点点滴滴分享给想做独立站点的站友们。

首先,我百度了在,但是发现都不具备可行性,因为百度中大家所给出的方法,都是去修改主题文件 index.php

即:

但是这就有个问题了,不同的主题,其代码不一样,因此我们无法参照网上的方法去修改。

我将index.php的代码和我的要求直接告知AI,让AI来给我修改,竟然成功了!

index.php的原始代码:

<?php get_header(); ?>
<div class="content">
	
	<?php if ( get_theme_mod('heading-enable','off') == 'on' ) : ?>
		<?php get_template_part('inc/page-title'); ?>
	<?php endif; ?>

	<div class="content-inner group">
		<?php get_template_part('inc/front-widgets-top'); ?>
		
		<?php if ( have_posts() ) : ?>
			<?php while ( have_posts() ): the_post(); ?>
				<?php get_template_part('content'); ?>
			<?php endwhile; ?>
			
			<?php get_template_part('inc/front-widgets-bottom'); ?>
			<?php get_template_part('inc/pagination'); ?>
			
		<?php endif; ?>
	</div>
</div><!--/.content-->

<div id="move-sidebar-content"></div>

<?php get_footer(); ?>

从代码上开,只有<?php while ( have_posts() ): the_post(); ?>这个代码要修改,其余的部分的代码不是渲染模板,就是加载网页头部和尾部等。

我用的是ChatGPT,输入原始的index.php代码和我的要求:

这是wordpress的首页模板代码(index.php):
<?php get_header(); ?>

<div class="content">
	
	<?php if ( get_theme_mod('heading-enable','off') == 'on' ) : ?>
		<?php get_template_part('inc/page-title'); ?>
	<?php endif; ?>

	<div class="content-inner group">
		<?php get_template_part('inc/front-widgets-top'); ?>
		
		<?php if ( have_posts() ) : ?>
			<?php while ( have_posts() ): the_post(); ?>
				<?php get_template_part('content'); ?>
			<?php endwhile; ?>
			
			<?php get_template_part('inc/front-widgets-bottom'); ?>
			<?php get_template_part('inc/pagination'); ?>
			
		<?php endif; ?>
	</div>
</div><!--/.content-->

<div id="move-sidebar-content"></div>

<?php get_footer(); ?>
我需要wordpress的首页显示分类id为1的最新文章,请你修改上述代码

ChatGPT的返回结果如下:


要让首页只显示分类ID为1的最新文章,你可以在 index.php 中修改 have_posts() 查询的部分,使用 WP_Query 来获取特定分类的文章。以下是修改后的代码:

<?php get_header(); ?>

<div class="content">
	
	<?php if ( get_theme_mod('heading-enable','off') == 'on' ) : ?>
		<?php get_template_part('inc/page-title'); ?>
	<?php endif; ?>

	<div class="content-inner group">
		<?php get_template_part('inc/front-widgets-top'); ?>
		
		<?php 
		// WP_Query 用于获取分类ID为1的最新文章
		$args = array(
			'cat' => 1, // 分类ID为1
			'posts_per_page' => 10, // 显示最新的10篇文章,可以根据需要调整
		);
		$query = new WP_Query( $args );

		if ( $query->have_posts() ) : ?>
			<?php while ( $query->have_posts() ) : $query->the_post(); ?>
				<?php get_template_part('content'); ?>
			<?php endwhile; ?>
			
			<?php get_template_part('inc/front-widgets-bottom'); ?>
			<?php get_template_part('inc/pagination'); ?>
		
		<?php else : ?>
			<p><?php esc_html_e( '没有找到相关文章。', 'your-theme-textdomain' ); ?></p>
		<?php endif; ?>

		<?php wp_reset_postdata(); // 重置查询 ?>
	</div>
</div><!--/.content-->

<div id="move-sidebar-content"></div>

<?php get_footer(); ?>

主要更改:

  1. 使用 WP_Query 来查询分类ID为 1 的文章,设置了 posts_per_page10(可以根据需要调整显示数量)。
  2. 使用 wp_reset_postdata() 来重置查询,确保其他地方的默认查询不受影响。
  3. 添加了一个 else 分支来显示如果没有相关文章时的提示。

这样,首页就会只显示分类ID为1的最新文章。


完美运行,输入后前端显示的都是cat=1的文章了,如果你不知道怎么看WordPress的分类id,请看下图:

如果你发现新的首页无法分页,例如向我这样,点击翻页按钮后,每一页内容都是一样的。那么也可以继续让AI给我们写。

首页我们先看下当前wordpress的分页的url

这次我换cursor来修改代码,正好借此机会体验下cursor,新建一个index.php文件,然后将上面的代码复制进去,你可以根据自己的要求做适当调整,例如加一些注释,避免以后自己看不懂,接着打开cursor的chat模式,选中当前文件的代码,然后输入我们的要求以及分页的url机制:

将CURSOR输出的代码结构整合到index.php中,然后在WordPress的主题编辑器中粘贴并保存,经过测试后,就可以正常翻页了。