WordPress.org

Forums

[resolved] How to add pagination without plugin on twenty eleven child (6 posts)

  1. kostisgrde
    Member
    Posted 2 years ago #

    hello,
    I want to add pagination on my post page following this tutorial:
    http://wp-ultra.com/2011/07/adding-pagination-to-the-twenty-eleven-theme-without-a-plugin/

    I did everything saying there but couldn't get it to work!
    I think it's because I'm using a child of twenty eleven and that it has to do with the functions.php that I did wrong.

    I didn't edit the twenty eleven functions.php because it would be changed again with the new update. So I created a functions.php empty file in my child and add the code there. I copied the index.php from twenty eleven and paste it on my child and then edit the code the tutorial said, and then edited the style.css of my child as the tutorial said.

    does anyone know what I did wrong and the pagination doesn't work?
    I'm getting the default "<-older posts" "newer posts->"

  2. Michael
    Forum Moderator
    Posted 2 years ago #

    please post a link to your site to illustrate the result of your edits.

  3. kostisgrde
    Member
    Posted 2 years ago #

    I am running the site on local host xampp. is not ready yet it is under construction! thx!

  4. kostisgrde
    Member
    Posted 2 years ago #

    I think I did something wrong with the creation of functions.php for my child.
    this is the way I did it:
    Create a new .txt file on my child folder, renamed it to functions.php,
    then I opened the file with Notepad++ and add the code

    <?php
    	function sonar_pagination($pages = '', $range = 3) {   /* handle pagination for post pages*/
        $showitems = ($range * 2)+1;  
    
        global $paged;
        if(empty($paged)) $paged = 1;
    
            if($pages == '')
            {
                global $wp_query;
                $pages = $wp_query->max_num_pages;
                if(!$pages)
                {
                $pages = 1;
                }
            }   
    
            if(1 != $pages)
            {
                echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
                if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
                if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
    
                for ($i=1; $i <= $pages; $i++)
                {
                    if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
                    {
                         echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
                    }
                }
    
                if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
                if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
                echo "</div>\n";
            }
        } //  sonar_pagination
    ?>

    that's all. nothing more. so....is there something wrong with this?

  5. Michael
    Forum Moderator
    Posted 2 years ago #

    Twenty Eleven has a

    <?php twentyeleven_content_nav( 'nav-above' ); ?>

    and

    <?php 				<?php twentyeleven_content_nav( 'nav-below' ); ?>
    ?>

    possibly change both.

    alternatively - even simpler without changing anything in index.php, you could just integrate the new code into functions.php of your child theme, into a copy of the function twentyeleven_content_nav( $html_id );

    example:

    function twentyeleven_content_nav( $html_id ) {
    	global $wp_query; //sonar pagination
    
    $pages = ''; $range = 3;   /* handle pagination for post pages*/
        $showitems = ($range * 2)+1;  
    
        global $paged;
        if(empty($paged)) $paged = 1;
    
            if($pages == '')
            {
                global $wp_query;
                $pages = $wp_query->max_num_pages;
                if(!$pages)
                {
                $pages = 1;
                }
            }   
    
            if(1 != $pages)
            {
                echo "<div id=\"".esc_html($html_id)."\" class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
                if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
                if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
    
                for ($i=1; $i <= $pages; $i++)
                {
                    if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
                    {
                         echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
                    }
                }
    
                if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
                if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
                echo "</div>\n";
            }
    //  sonar_pagination
    }
  6. kostisgrde
    Member
    Posted 2 years ago #

    that worked! THANKS A MILLION! :)

Topic Closed

This topic has been closed to new replies.

About this Topic