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.

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:

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') ?>

3 Answers 3


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

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.

  • This is what I put and it isn't working: snippi.com/s/1vcv3n5 Any suggestions? Jun 20, 2012 at 23:07
  • You need to add 'global $shortname' into the function, right at the top. If it's outside the function, then it is assumed that it's a local variable.
    – shabushabu
    Jun 21, 2012 at 4:01
  • Now I have this snippi.com/s/w8ggm07 , which also is not working. Jun 21, 2012 at 15:41
  • What exactly isn't working? Does nothing get displayed? Check that $shortname is actually a global and that the get_option() call actually returns anything. Just dumping the values on the screen should point you in the right direction. Also, you still haven't added the code block for the next/prev navigation, so if the option is set to that, then you won't see anything.
    – shabushabu
    Jun 22, 2012 at 10:00
  • Next/Previous links were showing up. So, I've updated my code snippi.com/s/ncz7urn and get this error: themeforward.com/demo2/?cat=50 Jun 25, 2012 at 15:19

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


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

    case 'previous_next':
      //handle previous/next pagination selection

    case 'full_pagination':
      //handle full pagination selection




  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" ;)

  • get_pagination_option() is really obsolete. You could just use get_option( 'your_option_name_here', 'no_selection' ); or default:
    – shabushabu
    Jun 20, 2012 at 4:29
  • Obsolete is ... debatable at best. He's making a plug-in so who knows where else he may need to get the pagination option and for what reason. Maybe a dashboard in the plug-in to show settings? Either way, I feel its a lot more effective to keep it simple than trying to make it "compact".
    – akamaozu
    Jun 20, 2012 at 9:37
  • It can't get any simpler than using get_option() with the default parameter set. It's exactly what your function does. That's what I meant with obsolete. Maybe that's the wrong word. You just don't need an extra function, is all I'm saying.
    – shabushabu
    Jun 20, 2012 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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