WordPress.org

Ready to get started?Download WordPress

Forums

Pagination in custom template (4 posts)

  1. xvegax
    Member
    Posted 1 year ago #

    Hi guys,

    I am totally new to WordPress and made my first custom template with five static pages and one page showing posts, like a mini diary in a static website.

    Now I have set to show four posts per page, but I do not get the next/older posts buttons. I also tried two plug-ins but that did not work.

    Can you show me how to get the "next page/posts" button under all the posts?

    Here is my code:

    <?php get_header(); ?>

    <?php
    global $post;
    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $args = array(
    'posts_per_page' => 4,
    'paged' => $paged
    );

    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :
    setup_postdata($post); ?>
    <p class='day'><?php the_time('d'); ?></p>
    <p class='month'><?php the_time('M'); ?></p>
    <p class='year'><?php the_time('Y'); ?></p>
    <?php the_title(); ?>
    <?php the_content(); ?>
    <?php edit_post_link('-- Eintrag bearbeiten', '<p>', '</p>'); ?>
    <?php endforeach; ?>

    <?php get_footer(); ?>

    Thank you in advance.

  2. xvegax
    Member
    Posted 1 year ago #

    You can have a look here: http://www.studio-scs.de/termine/

  3. keesiemeijer
    moderator
    Posted 1 year ago #

    Try adding one of the pagination functions in your template and use get_query_var( 'page' ) if this is the page template for a static front page

    http://codex.wordpress.org/Function_Reference/next_posts_link
    http://codex.wordpress.org/Function_Reference/previous_posts_link

    <?php get_header(); ?>
    
        <?php
        global $post;
        $paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : 1;
        $args = array(
        'posts_per_page' => 4,
        'paged' => $paged
        );
    
        $the_query = new WP_Query( $args );
        while ( $the_query->have_posts() ) : $the_query->the_post();
         ?>
        <p class='day'><?php the_time('d'); ?></p>
        <p class='month'><?php the_time('M'); ?></p>
        <p class='year'><?php the_time('Y'); ?></p>
        <?php the_title(); ?>
        <?php the_content(); ?>
        <?php edit_post_link('-- Eintrag bearbeiten', '<p>', '</p>'); ?>
        <?php endwhile; ?>
        <?php next_posts_link(); ?>
        <?php previous_posts_link(); ?>
        <?php wp_reset_postdata(); ?>
        <?php get_footer(); ?>
  4. xvegax
    Member
    Posted 1 year ago #

    Thank you, but that did not help. I just added your code and now the page gets a 500 error. :-/

Topic Closed

This topic has been closed to new replies.

About this Topic