• Hello, I am trying to get pagination to appear so that people can click on different page numbers to see all posts in the query.

    This is my current code, what am I missing?

    <?php

    global $post;
    $args = array( ‘post_count’ => 12, ‘posts_per_page’ => 4, ‘category_name’ => ‘home-feed’ );

    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post );
    ?>

    <div class=”row post-stream”>
    <div class=”col-md-3 col-sm-4 col-xs-12″>
    “><?php echo get_the_post_thumbnail( $post_id, ‘large’ ); ?>
    </div>
    <div class=”col-md-9 col-sm-8 col-xs-12″>
    <div style=”min-height: 135px;”>
    <h4 style=”margin-top: 0;”>
    “><?php the_title(); ?>
    </h4>
    <div class=”feed-date-tags”>
    <?php the_date(); ?>
    </div>
    <p>
    <?php echo excerpt(45); ?>
    </p>
    </div>
    “>Read More
    </div>
    </div>
    <hr>
    <?php endforeach;
    wp_reset_postdata();?>

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter fsucfasupport

    (@fsucfasupport)

    This looks very promising but I must be putting it in the wrong place in my code.

    Does it go after the foreach?

    Thread Starter fsucfasupport

    (@fsucfasupport)

    Here is what it looks like now…

    <?php
    /**
    Template Name: Development Test
    */
    get_header(); ?>

    <style type=”text/css”>
    .post-image img {
    height: 185px;
    width: 200px;
    object-fit: cover;
    }
    </style>

    <?php
    $page_id = get_the_ID();
    $key_1_value = get_post_meta($page_id,’section_1′,true);
    $key_2_value = get_post_meta($page_id,’section_2′,true);
    $key_3_value = get_post_meta($page_id,’section_3′,true);
    $key_4_value = get_post_meta($page_id,’section_4′,true);
    ?>

    <div class=”row”>
    <div class=”container”>
    <h1 class=”mobile-center” style=”font-size:40px; color:black; margin-top: 5px; font-weight:500;”>In the News</h1>

    <?php

    global $post;
    $args = array( ‘post_count’ => 12, ‘posts_per_page’ => 4, ‘category_name’ => ‘home-feed’, );

    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post );
    ?>

    <div class=”row post-stream”>
    <div class=”col-md-3 col-sm-4 col-xs-12″>
    “><?php echo get_the_post_thumbnail( $post_id, ‘large’ ); ?>
    </div>
    <div class=”col-md-9 col-sm-8 col-xs-12″>
    <div style=”min-height: 135px;”>
    <h4 style=”margin-top: 0;”>
    “><?php the_title(); ?>
    </h4>
    <div class=”feed-date-tags”>
    <?php the_date(); ?>
    </div>
    <p>
    <?php echo excerpt(45); ?>
    </p>
    </div>
    “>Read More
    </div>
    </div>
    <hr>
    <?php endforeach; ?>

    <div class=”row”>
    <?php the_posts_pagination( array(
    ‘mid_size’ => 2,
    ‘prev_text’ => __( ‘Back’, ‘textdomain’ ),
    ‘next_text’ => __( ‘Onward’, ‘textdomain’ ),
    ) ); ?>
    </div>

    <?php wp_reset_postdata();?>

    </div>
    </div>

    <?php get_footer(); ?>

    Moderator bcworkz

    (@bcworkz)

    Core pagination functions will not work with custom queries such as used with get_posts(). The core pagination function has no idea what the query is for. You also need to setup your get_posts() call to take pagination requests into account. Please see Making Custom Queries using Offset and Pagination.

    Thread Starter fsucfasupport

    (@fsucfasupport)

    Interesting, okay – thanks!

    I am looking into it now, I am far from a pro with PHP and Queries.

    It looks like I need to add an action? Going to keep reading up on it.

    Any more information helps, thank you guys…

    Moderator bcworkz

    (@bcworkz)

    Yes, that’s right. But there are differences in your case. You don’t need to ensure your code is modifying the right query in the callback because you can do so right on your page template. Just add your hooks on your page template immediately before calling get_posts(). By adding here, the query passed to your callback can only be due to your get_posts() call because there are no other queries active.

    When your callback has done its work, it’s good practice to have your callback remove itself from the hook stack. This prevents any subsequent queries from being improperly altered. Do so even if there are no further queries. In the future there may be, and the cause of errors from errant callbacks being in place could be very hard to find.

    There’s another important difference. You cannot rely on the query var ‘paged’ to have the right value, which is why you need your own pagination function. It’s simplest to have the target page be passed as a custom URL parameter. Then instead of relying on ‘paged’, use the passed URL parameter that’s available in $_GET.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘JQuery Pagination’ is closed to new replies.