添加自定义帖子编辑链接以选择性地刷新部分内容

时间:2017-11-20 作者:Dylan

我正在使用Customize PostsCustomize Object Selector WordPress自定义程序插件。

自定义帖子编辑链接在初始页面加载时显示在预览中,但不会应用于通过选择性刷新添加到页面的部分。有没有办法将编辑链接添加到刷新的分区中?

我使用php创建的设置以及动态创建的JavaScript设置。下面是一个使用php创建的设置的快速示例:

namespace MyTheme\\Customizer;

add_filter( \'customize_register\', __NAMESPACE__ . \'\\\\register\', 30 );

function register( \\WP_Customize_Manager $wp_customize ) {

    $wp_customize->add_section( \'theme_options\', array(
        \'title\'    => __( \'Theme options\', \'mytheme\' ),
        \'priority\' => 130, // Before Additional CSS.
    ) );

    $wp_customize->add_setting( \'selected_posts\', array(
        \'default\'           => [],
        \'transport\'         => \'postMessage\',
        \'sanitize_callback\' => __NAMESPACE__ . \'\\\\sanitize_ids\',
    ) );

    $selected_posts = new \\CustomizeObjectSelector\\Control( $wp_customize, \'selected_posts\', array(
        \'label\' => __( \'Posts\', \'mytheme\' ),
        \'section\' => \'theme_options\',
        \'post_query_vars\' => array(
            \'post_type\' => \'post\',
            \'post_status\' => \'publish\',
        ),
        \'select2_options\' => array(
            \'multiple\' => true,
            \'allowClear\' => true,
            \'placeholder\' => __( \'— Select —\', \'default\' ),
        ),
    ) );

    $wp_customize->add_control( $selected_posts );

    $wp_customize->selective_refresh->add_partial( \'selected_posts\', array(
        \'selector\' => \'.editorsPicks\',
        \'render_callback\' => __NAMESPACE__ . \'\\\\customize_partial_selected_posts\',
        \'container_inclusive\' => true,
        \'fallback_refresh\' => false,
    ) );
}

/**
 * Sanitize IDs
 *
 * @param array $ids An array of IDs.
 * @return array A sanitized array of IDs.
 */
function sanitize_ids( $ids ) {
    return array_map( \'intval\', $ids );
}

function customize_partial_selected_posts() {
    get_template_part( \'partials/layouts/editors-picks\' );
}

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

自定义帖子识别由中的选择器定义的帖子字段部分WP_Customize_Posts_Preview::get_post_field_partial_schema() 通过查找closest .hentry or .post-{id} elements. 所以只要你的帖子editors-picks partials使用post_class() 用于每个元素的容器和其他微格式的函数,如.entry-title 对于子元素,则应自动添加post字段部分。以《我的孩子》主题《2017》为例,它是如何定义content-excerpt.php 模板部件以及该模板部件的获取方式included.

结束

相关推荐