如何在短码内添加“GET_THEME_MOD”?

时间:2019-02-12 作者:andrew rico

我想在一个短代码中动态显示内容,以便在自定义程序中修改它。我创建了customizer选项卡。现在,我想创建一个短代码,将其放置在页面中的任何位置,同时能够在自定义程序中进行操作。

这就是我正在研究的代码。谢谢

<?php
/*
Plugin Name: ....
 */

// Exit if accessed directly
if(!defined(\'ABSPATH\')){
    exit;
  }

function customizer_inject_css()
{
?>
    <style type="text/css">
        :root {

            --theme-page-width: <?php echo get_theme_mod(\'theme_page_width\', \'1366px\');
            ?>!important;
        }

        .stm-template-car_dealer_two .container {
            max-width: var(--theme-page-width)!important;
        }
    </style>


<?php
}



add_shortcode( \'ui_gallery\',\'function_gallery_shortcode\' );
function function_gallery_shortcode(){
    $dynamic_h1 = "<h1>" . get_theme_mod(\'dymanich1\') . "</h1>";
    return $dynamic_h1;
}


    add_action( \'wp_head\', \'customizer_inject_css\');

    add_action("customize_register","register_customizer");

    function register_customizer( WP_Customize_Manager $wp_customize) {

        $wp_customize->add_panel( \'theme_extension_panel\',
        array(
            \'priority\'            => 2,
            \'capability\'        => \'edit_theme_options\',
            \'title\'                => __(\'Theme Extensions\'),
            \'description\'        => __(\'\'),
        ) );

            $wp_customize->add_section("general_section", array(
                "title"            => __("General"),
                "priority"    => 1,
                "panel"        => "theme_extension_panel"
            ));

                $wp_customize->add_setting( \'theme_page_width\', array(
                    "transport"        => "refresh",
                    "default"        => "",
                ) );
                $wp_customize->add_control(
                    new WP_Customize_Control(
                    $wp_customize,
                    \'theme_page_width_control\',
                    array(
                        \'label\'         => __(\'\'),
                        \'section\'    => \'general_section\',
                        \'settings\'   => \'theme_page_width\',
                        "type"         => "text",
                        "description" => "Max Width",
                        \'priority\'      => 1
                    ) )
                );

               $wp_customize->add_setting( \'dynamich1\', array(
                    "transport"        => "refresh",
                    "default"        => "",
                ) );
                $wp_customize->add_control(
                    new WP_Customize_Control(
                    $wp_customize,
                    \'dynamich1_control\',
                    array(
                        \'label\'         => __(\'\'),
                        \'section\'    => \'general_section\',
                        \'settings\'   => \'dynamich1\',
                        "type"         => "text",
                        "description" => "display h1",
                        \'priority\'      => 1
                    ) )
                );

            }

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

我会发表评论,但缺乏要点。。。您似乎在正确的轨道上:

我发现您的短代码回调和函数的名称不同:(f_gallery_shortcode/function_gallery_shortcode)。

尝试以下操作:

add_shortcode( \'ui_gallery\',\'function_gallery_shortcode\' );
function function_gallery_shortcode(){
    $dynamic_h1 = "<h1>" . get_theme_mod(\'dynamich1\') . "</h1>";
    return $dynamic_h1;
}
正在使用[ui_gallery] 在您的帖子中,应返回自定义值的结果<h1>dymanich1</h1>

相关推荐

Namespaced shortcode?

我正在改造一个旧的WP站点,该站点有许多自定义的短代码,显然由于代码当前的组织方式,这些短代码在性能方面付出了代价。当然,我可以修复优化不好的代码,使用十几个短代码,并且一天就可以完成,但我想知道如何更好地组织它们。根据WordPress\'documentation, 建议将它们放在插件中并在上初始化init. 我们可以通过这样“命名”它们来减少这个钩子中的负载吗?[com.company shortcode attr=\"attr\" prop=\"prop\"] 有人尝试过这样的解决方案吗