With the native Theme Customization API it makes no sense to have an independent options panel. So how do I implement my code (below) into the Theme Customizer?

Here is the code for my Theme Customizer: code here

In my options panel I allow users to select between Next/Previous Links in archives or pagination, the code looks 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" => "Next/Previous Links"),

Here is the function that makes it work:

// Pagination
function my_theme_navigation() {
    global $shortname;

    if( get_option( $shortname . '_next_prev_or_paginate' ) == 'Next/Previous Links' ) :
    // the block for next-prev navigation
    echo '<div class="button left">';
    next_posts_link('Older');
    echo '</div>';
    echo '<div class="button right">';
    previous_posts_link ('Newer');
    echo '</div>'; 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=%#%','end_size'     => 1,'mid_size'     => 2,                'current' => max( 1, get_query_var('paged') ),                 'total' => $wp_query->max_num_pages            )        );
    endif;
}

I then simply call it using this:

<?php my_theme_navigation(); ?>

1 Answer
1

Step 1: Register the setting in the customizer

Add this to your wptuts_theme_customizer() function, which will register a new “Pagination” section, as well as a “Pagination Style” setting:

/* Section: Pagination */
$wp_customize->add_section( 'themeslug_pagination', array(
    'title' => __( 'Pagination', 'themeslug' ),
) );
$wp_customize->add_setting( 'pagination_style', array(
    'default' => 'next-previous-links',
) );
$wp_customize->add_control( 'pagination_style', array(
    'label'   => __( 'Pagination Style', 'themeslug' ),
    'section' => 'themeslug_pagination',
    'type'    => 'radio',
    'choices' => array(
        'next-previous-links' => __( 'Next/Previous Links', 'themeslug' ),
        'numbered'            => __( 'Numbered', 'themeslug' ),
    ),
) );

Step 2: Modify the pagination function to work with the customizer setting

Modify your my_theme_navigation() function like this:

BEFORE:
if( get_option( $shortname . '_next_prev_or_paginate' ) == 'Next/Previous Links' ) :

AFTER:
if ( 'next-previous-links' == get_theme_mod( 'pagination_style' ) ) :

Note that because theme mods are specific to the currently active theme, you don’t need to prefix them with {$themename}_, which would be redundant.

Here is your original code, with my modifications added:

  • Theme Customizer code: http://pastebin.com/eqjmJU7t
  • Pagination function: http://pastebin.com/GLUx3RTr

Leave a Reply

Your email address will not be published. Required fields are marked *