Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to have pagination on multiple tabs?

I have a page the has 5 tabs, each tab has filtered content from the same posts. For example:

Tab 1 - All Posts. Tab 2 - Posts with Keyword One. Tab 3 - Posts with Keyword Two.

etc...

My problem is that I can't figure out how to use pagination on each tab and have the user be able to select the page on that tab and stay on that tab - it wants to go back to the first tab.

share|improve this question
    
anyone have a suggestion? –  user7481 Sep 4 '11 at 22:35
add comment

2 Answers

up vote 0 down vote accepted

Use the following extension to the WP_Query class.

The extension only takes your arguments as an array, rather than a string format.

You'll to add individual values for "page_link" for each of your tabs. This should just be a string ("tab1", "tab2" etc etc)

Then you'll need to use the $my_query->next() and $my_query->prev() to add page turning links to each tab.

Code Also Here

  <?php
class MF_Query extends WP_Query{
    private $page_link = "page";

    function __construct(array $args){
        if(!array_key_exists('posts_per_page',$args)) $args['posts_per_page'] = 10;
        if(array_key_exists('page_link',$args)) $this->page_link['page_link'];

        $args['offset'] = (isset($_GET[$this->page_link])?($_GET[$this->page_link]-1)*$args['posts_per_page']:0);
        parent::query($args);
    }

    function next($link_text = "Next"){

            $curPage = intval((isset($_GET['page'])?$_GET['page']:1));//Use 1 if $_GET['page'] not set

            $link = "<a href='".remove_post_vars(curPageURL());

            if($curPage<$this->max_num_pages){
                echo $link.$this->constructQuery($this->merge(array($this->page_link=>$curPage+1),$_GET))."'>".$link_text."</a>";
            } else {
                return false;
            }


    }
    function prev($link_text = "Previous"){

            $curPage = (isset($_GET['page'])?$_GET['page']:1);//Use 1 if $_GET['page'] not set
            $link = "<a href='".remove_post_vars(curPageURL());
            if($curPage>1){
                echo $link.$this->constructQuery($this->merge(array($this->page_link=>$curPage-1),$_GET))."'>$link_text</a>";
            } else {
                return false;
            }


    }
    private function constructQuery(array $query){


        $url_ext = "?";
        foreach($query as $k => $v){
            $url_ext .=$k."=".$v."&amp;";
        }
        $url_ext = substr($url_ext, 0, -5);//chop last ampersand off


        return $url_ext;

    }
    private function merge($get, $put){
        //Get values from one array, and put them in another (overriding existing values if appropriate)
        foreach ($get as $k => $v){
                $put[$k]=$v;
        }
        return $put;

    }
}
?>
share|improve this answer
    
I decided to use links with different templates instead of tabs. But I might use this in the future. Thanks! –  user7481 Oct 29 '11 at 18:08
add comment

You'd have to return an array from paginate_links(), check the url for a fragment identifier then append that to your array before output.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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