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 分享