创建一个函数来输出选择性刷新的模板代码(我将HTML包装在<div class="cta-wrap">
以便更容易地定位此特定标记块。)
function wpse247234_cta_block() {
if ( ( get_theme_mod( \'intro_page\' ) ) != \'\' ) {
$intro_id = get_theme_mod( \'intro_page\' );
$intro_header = get_the_title( $intro_id );
$intro_excerpt = get_the_excerpt( $intro_id );
$intro_link = get_the_permalink( $intro_id );
$intro_linktext = get_post_meta( $intro_id, \'emm_cta_text\', true );
echo \'<div class="cta-wrap">\';
echo \'<h1>\' . esc_html( $intro_header ) . \'</h1>\' . \'<p>\' . esc_html( $intro_excerpt ) . \'</p>\';
if ( ! get_post_meta( $intro_id, \'emm_cta_text\', true ) ) {
echo \'<p><a class="cta" href="\' . esc_url( $intro_link ) . \'">Learn More</a></p>\';
} else {
echo \'<p><a class="cta" href="\' . esc_url( $intro_link ) . \'">\' . esc_html( $intro_linktext ) . \'</a></p>\';
}
echo \'</div>\';
}
}
通过调用上述新创建的函数更新模板:
wpse247234_cta_block();
设置自定义程序
function wpse247234_customize_register( $wp_customize ) {
$wp_customize->add_section( \'intro\', array (
\'title\' => __( \'intro\', \'text-domain\' ),
\'priority\' => 999,
) );
$wp_customize->add_setting( \'intro_page\' , array(
\'sanitize_callback\' => \'absint\',
\'transport\' => \'postMessage\'
) );
$wp_customize->add_control( \'intro_page\', array(
\'label\' => __( \'Page to use for intro section\', \'text-domain\' ),
\'section\' => \'intro\',
\'settings\' => \'intro_page\',
\'type\' => \'dropdown-pages\',
\'priority\' => 1
) );
$wp_customize->selective_refresh->add_partial( \'intro_page\', array(
\'selector\' => \'.cta-wrap\',
\'container_inclusive\' => true,
\'render_callback\' => \'wpse247234_cta_block\',
\'fallback_refresh\' => false, // Prevents refresh loop when document does not contain .cta-wrap selector. This should be fixed in WP 4.7.
) );
}
add_action( \'customize_register\', \'wpse247234_customize_register\' );
在刷新项时设置其样式
在刷新分部时,受影响的元素将具有该类
customize-partial-refreshing
添加到它。您可以这样设计:
.cta-wrap.customize-partial-refreshing {
// styles...
}
有用的链接
Selective Refresh in the CustomizerTheme Options – The Customizer API看一下如何Twenty Sixteen v1.3 句柄选择性刷新有助于启动并运行此示例