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.

Earlier, I asked this question and received a code which should work. Unfortunately, this code is not working.

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.

The code within functions.php is controlling an options panel drop-down which allows the user to choose pagination or next/previous links.

This code is displaying Next/Previous links at all times, even when I choose pagination. I need the pagination option to work too!

What I have in functions.php

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

What I have in archives.php

<?php
function my_theme_navigation() 
{ 
    global $shortname;

    if( get_option( $shortname . '_next_prev_or_paginate' ) == 'Next' ) : 
        // the block for next-prev navigation 
        previous_posts_link ('Newer');
        next_posts_link('Older');
    else : 
        // the block for 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 
            ) 
        ); 
    endif; 
}
?>

For all relevant codes...

The original pagination code (which has been combined with prev/next)

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

The previous/next which has been combined with pagination

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

The function for the theme options, chooses between pagination and next/prev links

array( "name" => "Paginate or next/previous links?", 
    "desc" => "Choose your option", 
    "id" => $shortname."_next_prev_or_paginate", 
    "type" => "select", 
    "options" => array("Next", "Pagination"), 
    "std" => "Next"), 
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted
+50

1) Move the function to the functions.php file, that's where it belongs. Put it at the end before any closing ?>:

function my_theme_navigation() 
{ 
    global $shortname;

    if( get_option( $shortname . '_next_prev_or_paginate' ) == 'Next' ) : 
        // the block for next-prev navigation 
        previous_posts_link ('Newer');
        next_posts_link('Older');
    else : 
        // the block for 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 
            ) 
        ); 
    endif; 
}

2) Looks like the only thing you're missing is the function call, that will print the navigation.
Not very familiar with archives.php, so using single.php as example:

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */

get_header(); ?>

        <div id="primary">
            <div id="content" role="main">

                <?php while ( have_posts() ) : the_post(); ?>

                    <nav id="nav-single">
                    <?php my_theme_navigation(); /* CALLING THE FUNCTION TO ECHO THE NAVIGATION */ ?>
                    </nav><!-- #nav-single -->

                    <?php get_template_part( 'content', 'single' ); ?>

                    <?php comments_template( '', true ); ?>

                <?php endwhile; // end of the loop. ?>

            </div><!-- #content -->
        </div><!-- #primary -->

<?php get_footer(); ?>

Original answer

Hypothesis

If, as the question says,

this code is displaying Next/Previous links at all times

(My emphasis) It much probably means that the if/else is not working.


Option Value

Supposing $shortname is equal to "shortname", I created an option in the table wp_options. phpMyAdmin


Test

Putting this code in header.php, inside <div id="main"> from TwentyEleven:

function my_theme_navigation() { 
  if( 'Next' == get_option( 'shortname_next_prev_or_paginate' ) ) : 
      // the block for next-prev navigation 
      echo 'Previous/Next selected.';
  else : 
      // the block for pagination
      echo 'Pagination selected.';
  endif; 
}
my_theme_navigation();

Result and Possible Solution

It does echo correctly when the option value is manually changed in the database.

Make sure the following functions/commands are working in this order:

  1. get_option, does it grabs the value? The value exists in the database?
  2. paginate_links, does it prints correctly if called alone? Without get_option and if/else?
  3. if/else, if the previous are working this one gotta be working
share|improve this answer
2  
@siouxfan45 : It does not compute..! How on earth are you "building a theme options panel" without knowing "nothing about PHP"¿? ... I strongly suggest you to locate all the relevant code and add it to your Question. Unfortunately, seeing the final rendered Html doesn't help.... –  brasofilo Jul 10 '12 at 15:01
    
Thus far, I've done it by asking questions and combining a ton of tutorials online. I've added what I believe is the relevant codes. –  AndrettiMilas Jul 10 '12 at 15:14
    
@siouxfan45 : Debugging PHP values with echo, var_dump and print_r are of extreme help. This is a simple test ( pastebin.com/GeMEnELT ), put it in archives.php and check the results when you change the theme option value ("Paginate or next/previous links?"). –  brasofilo Jul 10 '12 at 15:35
    
The code provided is displaying the correct Option value when I switch it in theme options. The code displays next/prev and pagination. As shown here: themeforward.com/demo2/?cat=50 - How do I turn this into a functional code to choose between my next/prev links or pagination? –  AndrettiMilas Jul 10 '12 at 16:25
    
If I finally understand correctly, you are only missing the function call to print the selected navigation, answer updated, is this it? –  brasofilo Jul 10 '12 at 16:46
show 1 more 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.