wordpress所有文章怎么显示

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

wordpress所有文章怎么显示

如何显示 WordPress 中的所有文章

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

方法 1:使用页面模板

  • 创建一个新的页面或编辑现有页面。
  • 在页面编辑器中,在编辑区域添加以下代码:
<?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 文件中添加以下代码:
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' );
  • 在您想要显示文章的位置添加以下短代码:
[display_all_posts]

方法 4:使用自定义查询

  • 在主题的文件中添加以下代码:
// 查询所有文章 $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();

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

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享