Allow Users to Choose Pagination or Next/Previous (Combine)

I’m building a themes option panel and want users to be able to choose between Next/Previous links or pagination in my archives and homepage.

I need help combining the two functions below (pagination and next previous) to create a function which chooses based on the user’s selection.

Read More

I figure the function I add to the options panel will look like this:

array( "name" => "Paginate or next/previous links?",
    "desc" => "Choose your option",
    "id" => $shortname."_next_prev_or_paginate",
    "type" => "select",
    "options" => array("Next/Previous Links", "Pagination"),
    "std" => ""),

Here’s how I’m calling the pagination:

<?php
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );
?>

Here’s how I call next/prev links:

<?php previous_posts_link ('Newer') ?>
<?php next_posts_link('Older') ?>

Related posts

Leave a Reply

3 comments

  1. A simple if statement should do, so something like this:

    function my_theme_navigation() {
        if( get_option( $shortname .'_next_prev_or_paginate' ) == 'Next/Previous Links' ) :
            // the block for next-prev navigation
        else :
            // the code for pagination
        endif;
    }
    

    I have no way of knowing what the array in your first code block exactly does, but I think I got it right. The only thing you’d still have to do is substitute $shortname with whatever that variable holds. If it’s a global, then just add global $shortname; to the top of the function. I am also assuming that the values of the select options are the same as the titles.

  2. I agree with @shabushabu that an if statement would probably do the job just fine as soon as you have both functions ready. I would consider also adding wp_pagenavi as an option, which essentially combines both your options.
    wp_pagenavi

  3. Good work, but I’d do it a bit differently:

    function get_pagination_option() {
    $option = get_option(your_option_name_here);
    if ($option)? return $option : return 'no_selection';
    }
    
    function display_pagination() {
    $option = get_pagination_option();
    
      switch($option) {
        case 'no_selection':
          //handle no pagination selected condition
        break;
    
        case 'previous_next':
          //handle previous/next pagination selection
        break;
    
        case 'full_pagination':
          //handle full pagination selection
        break;
    
      }
    
    }
    

    Why?

    1. switch/case is marginally faster. Not game-breakingly faster, but its nice to be in the habit of saving every microsecond where you can.

    2. handles a situation where there is no selection, for whatever reason.

    3. give your functions 1 thing to do, as a rule. the more it does, the less flexible it is and the harder it is for anyone else to ever debug it.

    I’d admit the solution provided before this does the job, but this is more along the lines of “best practices” 😉