wordpress怎么调用分类的文章列表
时间 : 2023-11-26 22:06:01声明: : 文章内容来自网络,不保证准确性,请自行甄别信息有效性

最佳答案

要在WordPress中调用分类的文章列表,可以使用以下步骤:

1. 首先,登录到WordPress后台。

2. 在左侧导航菜单中,找到并点击“外观”选项。

3. 在“外观”菜单下,选择“编辑器”或“主题编辑器”选项,以打开主题文件编辑器。

4. 在主题文件编辑器中,找到并点击“category.php”或“archive.php”,这取决于你的主题文件结构。如果没有这些文件,则可以创建一个新的模板文件。

5. 在打开的模板文件中,找到文章循环的代码。它通常是由`<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>`开始,以及结束于`<?php endwhile; endif; ?>`的结束。

6. 在文章循环的代码中,添加以下代码来调用分类文章列表:

```php

<?php

$args = array(

'cat' => 'your-category-slug',

'posts_per_page' => -1

);

$query = new WP_Query($args);

if($query->have_posts()) : while($query->have_posts()) : $query->the_post();

?>

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br>

<?php endwhile; endif; ?>

注意:需要将`your-category-slug`替换为你想要调用的分类的别名或ID。

7. 保存文件并关闭编辑器。

通过以上步骤,你就可以在WordPress中调用特定分类的文章列表了。请确保你对主题文件的修改非常熟悉,并备份文件以防止意外情况。同时也可以考虑使用子主题来进行修改,以避免在主题更新时丢失你的修改。

其他答案

在WordPress中,可以使用以下代码调用分类的文章列表:

```php

<?php

// 获取当前页面的分类ID

$category_id = get_query_var('cat');

// 设置查询参数

$args = array(

'post_type' => 'post', // 文章类型,默认为post

'cat' => $category_id, // 分类ID

'posts_per_page' => -1, // 每页显示的数量,-1表示显示所有文章

);

// 查询文章

$query = new WP_Query($args);

// 输出文章列表

if ($query->have_posts()) {

while ($query->have_posts()) {

$query->the_post();

?>

<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

<p><?php the_excerpt(); ?>

<?php

}

} else {

echo "没有找到相关文章。";

}

// 重置查询

wp_reset_query();

?>

将以上代码插入到你希望显示分类文章列表的页面模板文件中,保存并更新页面,就可以看到该分类下的文章列表了。注意,该代码将显示文章标题和摘要,你可以根据需要自定义输出内容的方式。