Home > CMS Tutorial > WordPress > body text

How to display all articles in wordpress

下次还敢
Release: 2024-04-15 16:48:19
Original
510 people have browsed it

Methods to display all articles in WordPress include: using a page template to create a page and adding code to query all articles. Add a "Latest Articles" widget to your sidebar. Add PHP code to query all posts in your theme’s functions.php file. Add PHP code for custom queries at specific locations.

How to display all articles in wordpress

How to display all posts in WordPress

WordPress is a powerful content management system that allows users to easily create and administer the website. If you need to display all articles on the website, you can achieve it by the following methods:

Method 1: Use the page template

  • Create a new page or edit existing page.
  • In the page editor, add the following code in the editing area:
<code class="php"><?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();
?></code>
Copy after login
  • Save or update the page.

Method 2: Using Widgets

  • Go to Appearance >Widgets in your dashboard.
  • Drag and drop the "Latest Articles" widget to the sidebar or other widget area.
  • Configure the widget, including the number of articles to display and other options.

Method 3: Using PHP Code

  • Add the following code in your theme’s functions.php file:
<code class="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' );</code>
Copy after login
  • Add the following shortcode where you want the article to appear:
<code class="php">[display_all_posts]</code>
Copy after login

Method 4: Use a custom query

  • Add the following code to your theme’s files:
<code class="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();</code>
Copy after login

You can insert this code anywhere on your website, such as on a specific page, post, or sidebar.

The above is the detailed content of How to display all articles in wordpress. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!