【问题标题】:How can I switch themes and still be able to show the Maintain same contents?如何切换主题并仍然能够显示保持相同的内容?
【发布时间】:2021-09-28 16:02:54
【问题描述】:

我在 wordpress 网站上安装了两个主题。我已按照推荐将我所有的非主题特定功能移动到一个插件中,并且除了包含在我的 functions.php 文件中的 customizer.php 之外,所有功能都运行良好。我将它从functions.php移到插件中。定制器内容在第一个主题被激活时显示,但在我切换到另一个主题时不会显示。下面是代码sn-ps供参考:

我使用以下代码将 customizer.php 包含到我的插件中

require( plugin_dir_path( __FILE__ ) . 'inc/customizer.php' );

下面是控制徽标的面板

if ( class_exists( 'WP_Customize_Panel' ) ):

    $wp_customize->add_panel( 'panel_general', array(
        'priority' => 29,
        'capability' => 'edit_theme_options',
        'title' => __( 'General options', 'gswmi' )
    ));

    /* LOGO */
    $wp_customize->add_setting( 'site_logo', array(
        'sanitize_callback' => 'esc_url_raw',
        'transport' => 'postMessage'
    ));

    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'themeslug_logo',
        array(
        'label' => __( 'Logo' ),
        'section' => 'title_tagline',
        'settings' => 'site_logo',
        'priority' => 1,
        )
    )); endif;

第一个主题激活时会显示徽标和内容

但在激活第二个主题时不会显示

我尝试了不同的修复方法,甚至删除了customizer.php 代码中的文本域,但似乎都不起作用。我希望能够切换主题,并且仍然能够在定制器和前端中显示相同的内容,而不管激活的主题如何。我将不胜感激。

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    多亏了我在网上找到的一些文章(学分如下),我终于能够对其进行排序。

    默认情况下,我上面设置的类型是'theme_mod'

    主题模组存储在数据库选项表中的单个数组中,每个主题一个字段。就我而言,我在数据库中 wp_options 表的 theme_mods_(mythemename) 下的单个数组中找到了所有主题自定义。您可以猜到,这对性能不利。

    为了使我的徽标和其他设置无论我使用的主题如何都可以轻松使用,我只需将设置类型从默认设置类型 'theme_mod' 更改为 'option'

    但是有什么区别呢?为了尽量保持简单:

    1. Theme Modifications(Theme Mod) 表示特定主题可用的内容。 WordPress 将定制器管理的所有选项保存为数据库中的单个数组。

    2. 选项可用于任何主题和/或任何插件。与定制器相比,WordPress 将每个值作为单个记录保存在数据库中。

    更新下面的代码

    if ( class_exists( 'WP_Customize_Panel' ) ):
    
    $wp_customize->add_panel( 'panel_general', array(
        'priority' => 29,
        'capability' => 'edit_theme_options',
        'title' => __( 'General options', 'gswmi' )
    ));
    
    /* LOGO */
    $wp_customize->add_setting( 'site_logo', array(
        'type'              => 'option',
        'sanitize_callback' => 'esc_url_raw',
        'transport' => 'postMessage'
    ));
    
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'themeslug_logo',
        array(
        'label' => __( 'Logo' ),
        'section' => 'title_tagline',
        'settings' => 'site_logo',
        'priority' => 1,
        )
    )); endif;
    

    上述 Setting 的值将在 WP_OPTIONS 表中获得自己的列,当在前端访问该值进行输出时,性能会有所提升。

    学分:

    https://www.usablewp.com/learn-wordpress/wordpress-customizer/theme-mods-vs-options-in-wordpress/

    https://tommcfarlin.com/wordpress-options/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-08
      • 2022-01-22
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 2022-01-03
      • 2023-03-19
      • 2022-01-01
      相关资源
      最近更新 更多