wordpress所有文章怎么显示

来源:undefined 2024-12-17 13:12:37 1049

在 wordpress 中显示所有文章的方法包括:使用页面模板创建页面,添加查询所有文章的代码。在侧边栏添加“最新文章”小部件。在主题的 functions.php 文件中添加查询所有文章的 php 代码。在特定位置添加自定义查询的 php 代码。

如何显示 WordPress 中的所有文章

WordPress 是一个强大的内容管理系统,允许用户轻松创建和管理网站。如果您需要在网站上显示所有文章,可以通过以下方法实现:

方法 1:使用页面模板

创建一个新的页面或编辑现有页面。 在页面编辑器中,在编辑区域添加以下代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?php

// 查询所有文章

$args = array(

post_type => post,

posts_per_page => -1,

);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

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

// 显示文章的内容

the_title( <h2>, </h2> );

the_content();

endwhile;

endif;

wp_reset_postdata();

?>

登录后复制
保存或更新页面。

方法 2:使用小部件

转到仪表盘中的“外观”>“小部件”。 拖放“最新文章”小部件到侧边栏或其他小部件区域。 配置小部件,包括要显示的文章数量和其他选项。

方法 3:使用 PHP 代码

在主题的 functions.php 文件中添加以下代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

function display_all_posts() {

// 查询所有文章

$args = array(

post_type => post,

posts_per_page => -1,

);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

echo <ul>;

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

echo <li><a href=" . get_permalink() . "> . get_the_title() . </a></li>;

endwhile;

echo </ul>;

endif;

wp_reset_postdata();

}

add_shortcode( display_all_posts, display_all_posts );

登录后复制
在您想要显示文章的位置添加以下短代码:

1

[display_all_posts]

登录后复制

方法 4:使用自定义查询

在主题的文件中添加以下代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// 查询所有文章

$args = array(

post_type => post,

posts_per_page => -1,

);

$query = new WP_Query( $args );

// 循环文章并显示内容

if ( $query->have_posts() ) :

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

// 显示文章的内容

the_title( <h2>, </h2> );

the_content();

endwhile;

endif;

wp_reset_postdata();

登录后复制

您可以在网站的任何地方插入此代码,例如特定页面、文章或侧边栏。

以上就是wordpress所有文章怎么显示的详细内容,更多请关注php中文网其它相关文章!

最新文章