List all default posts (with proper_pagination) in WordPress

To list all default posts in WordPress with proper pagination, you can use the WordPress Query API and pagination functions. Below is an example code snippet to display all posts with pagination in a WordPress theme:

Setting Up the Query with Pagination

Add this code to a template file, such as index.php, archive.php, or a custom template:

get_header();

// Define the number of posts per page
$posts_per_page = 10;

// Get the current page number
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

// Set up a new query for posts
$args = array(
    'post_type'      => 'post',
    'posts_per_page' => $posts_per_page,
    'paged'          => $paged,
);

$query = new WP_Query($args);

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

    // Display pagination
    echo '';
else :
    echo '

' . __('No posts found.', 'textdomain') . '

'; endif; // Reset post data wp_reset_postdata(); get_footer();

No comments:

Post a Comment