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.

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:

<?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') ?>
share|improve this question
add comment

3 Answers

up vote 3 down vote accepted
+25

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.

share|improve this answer
    
This is what I put and it isn't working: snippi.com/s/1vcv3n5 Any suggestions? –  AndrettiMilas Jun 20 '12 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 '12 at 4:01
    
Now I have this snippi.com/s/w8ggm07 , which also is not working. –  AndrettiMilas Jun 21 '12 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 '12 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 –  AndrettiMilas Jun 25 '12 at 15:19
show 3 more comments

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

share|improve this answer
add comment

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

share|improve this answer
    
get_pagination_option() is really obsolete. You could just use get_option( 'your_option_name_here', 'no_selection' ); or default: –  shabushabu Jun 20 '12 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 '12 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 '12 at 16:19
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.