0

I am creating a wordpress theme in which there is a page named "blog" which will show my all latest blog posts. Here, i am displaying only 5 posts per page and i want in first page it will only show "Newer" link, in second page it will show both "Older" and "Newer" link and also "Older" link on last page like this. Here, m using following code for my task and its not working as expected.

This is the function I am using in functions.php to alter custom post type "blog". But this is also not working, its showing no posts instead of 5 which i've set here.

// ----------------------- Set the number of a custom post type posts per page
add_filter( 'pre_get_posts', 'be_archive_query' );
function be_archive_query( $query ) {
    if( $query->is_post_type_archive('blog') ) {
        $query->set( 'posts_per_page', 5 );
    }
}

This is my page-blog.php

<div class="post-content">

    <?php
    if(have_posts()) {
        while(have_posts()) {
            the_post();
            $link = get_permalink();
            echo "<div id='bl-post'>";

            echo "<div class='thumb'>";
            echo "<a href=$link>";
            if(has_post_thumbnail()) {
                    the_post_thumbnail();
                }
            echo "</a>";
            echo "</div>";

                echo "<div class='cont'>";
                echo "<h2 class='bl-post-title'><a href=$link>";
                the_title();
                echo "</a></h2>";
                echo "<p>";
                     $excerpt = get_the_excerpt();
                     echo string_limit_words($excerpt,30);                                  
                echo "</p>";
                echo "<a href=$link id='cont-read'>";
                _e('Read More','nss-framework');
                echo "</a>";
                echo "</div>";

            echo "</div>";
        }
            ?>
            <div class="clr"></div>
            <ul class="sl-navi">
                    <li>
                        <a data-hover="Previous" href="#">
                            <span><?php next_posts_link(__('Older','nss-framework')); ?></span>
                        </a>
                    </li>
                    <li>
                        <a data-hover="Next" href="#">
                            <span><?php previous_posts_link(__('Newer','nss-framework')); ?></span>
                        </a>
                    </li>
                </ul>
            <?php
    }

    ?>

</div>

This is my single-blog.html

<div class="clr"></div>


<ul class="sl-navi">
        <li>
            <a data-hover="Previous" href="#">
                <span>Previous</span>
            </a>
        </li>
        <li>
            <a data-hover="Next" href="#">
                <span>Next</span>
            </a>
        </li>
    </ul>

Expected previews

1st page

enter image description here

2nd page

enter image description here

Last page

enter image description here

But, in wp i am getting ds

enter image description here

I've also checked html part after using next_posts_link() and previous_posts_link() functions and I get this (nothing displaying between span tags)

What changes should I make?

enter image description here

  • Take a look at this question: wordpress.stackexchange.com/questions/77661/… Also note that next_posts_link print all the link, including <a> tags, in your code you have nested anchor, that I think is html illegal. – jep Dec 31 '14 at 16:03
  • Two questions, why? are you using a single page as a blog page, this is surely not why single pages are meant for, and that is why your asetup won't work. Secondly, why are you running a custom query? – Pieter Goosen Jan 1 '15 at 9:58
  • next_posts_link(); & previous_posts_link(); are also not working on my index page of other themes wch i am developing... – Raunak Hajela Jan 1 '15 at 10:57
  • 2
    Those links will never work if you are not using the main query. Drop your custom queries, use the default loop and use pre_get_posts to alter the main query – Pieter Goosen Jan 1 '15 at 12:25
1

I did this and its working as expected. Please close this question

<div class="post-content">

    <?php
    // set the "paged" parameter (use 'page' if the query is on a static front page)
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

    $the_query = new WP_Query( 'posts_per_page=5&post_type=blog&paged=' . $paged );
    if($the_query->have_posts()) {
        while($the_query->have_posts()) {
            $the_query->the_post();
            $link = get_permalink();
            echo "<div id='bl-post'>";

            echo "<div class='thumb'>";
            echo "<a href=$link>";
            if(has_post_thumbnail()) {
                    the_post_thumbnail();
                }
            echo "</a>";
            echo "</div>";

                echo "<div class='cont'>";
                echo "<h2 class='bl-post-title'><a href=$link>";
                the_title();
                echo "</a></h2>";
                echo "<p>";
                     $excerpt = get_the_excerpt();
                     echo string_limit_words($excerpt,30);                                  
                echo "</p>";
                echo "<a href=$link id='cont-read'>";
                _e('Read More','nss-framework');
                echo "</a>";
                echo "</div>";

            echo "</div>";
        }
            ?>
            <div class="clr"></div>
            <ul class="sl-navi">
                    <li>
                        <span><?php next_posts_link('Older', $the_query->max_num_pages); ?></span>
                    </li>
                    <li>
                        <span><?php previous_posts_link('Newer'); ?></span>
                    </li>
                </ul>
                <?php wp_reset_postdata(); ?>
            <?php
    }

    ?>

</div>
|improve this answer|||||
  • Please accept your own answer so this can show as solved ;-) – Pieter Goosen Feb 11 '16 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.