使主题选项成为主题定制器的原生选项

时间:2014-08-04 作者:AndrettiMilas

与本地人Theme Customization API 有一个独立的选项面板是没有意义的。那么,如何将我的代码(下面)实现到主题定制器中呢?

以下是我的主题定制器的代码:code here

在我的选项面板中,我允许用户在存档或分页中的下一个/上一个链接之间进行选择,代码如下所示:

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"),
以下是使其工作的功能:

// 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;
}
然后,我简单地用这个来称呼它:

<?php my_theme_navigation(); ?>

1 个回复
最合适的回答,由SO网友:Philip Newcomer 整理而成

Step 1: Register the setting in the customizer

将此添加到wptuts_theme_customizer() 函数,该函数将注册新的“分页”部分以及“分页样式”设置:

/* 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

修改您的my_theme_navigation() 功能如下:

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

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

注意,因为主题mod特定于当前活动的主题,所以不需要在它们前面加前缀{$themename}_, 这是多余的。

这是您的原始代码,添加了我的修改:

主题定制器代码:http://pastebin.com/eqjmJU7thttp://pastebin.com/GLUx3RTr

结束

相关推荐