将快捷代码属性值获取到另一个函数

时间:2017-10-09 作者:Hudai

我正在尝试获取另一个函数的shortcode属性值,以添加内联样式。我想使用$color\'s值转换为另一个函数。

这是我的密码

<?php

/**
* 
*/
class MakeShortcode
{

    public $color;
    public function __construct()
    {
        add_action( \'init\', array($this, \'PostBLock\') );
        add_shortcode( \'PostBLock\', array($this, \'getpostblock\') );

        add_action( \'wp_enqueue_scripts\', array( $this, \'inlinestyle\') );

    }

    public function getpostblock( $atts ){
        extract( shortcode_atts( 
        array(
            \'section_color\' => \'#000000\',

        ), $atts) );

        $this->color = $section_color;

        $output = \'First LIne\';
        $output .= \'Second Line\';
        return $output;
    }
    public function inlinestyle(){
        wp_enqueue_style(
            \'custom-style\',
            get_template_directory_uri() . \'/css/style.css\'
        );
        $custom_css = "
            div[data-id=289374].post-block-post-left.active,
            div[data-id=289374].post-block-post-right.active{
                background : {$this->color};
                border-color : {$this->color};
            }";
        wp_add_inline_style( \'custom-style\', $custom_css );
    }
}
$test = new MakeShortcode();
但是backgroundborder-color 变为空。那意味着我不会$this->color 该函数中的值。我怎么能做到?

1 个回复
SO网友:Piyush Rawat
<?php
class MakeShortcode
{

public $color;
public function __construct()
{
    add_action( \'init\', array($this, \'PostBLock\') );
    add_shortcode( \'PostBLock\', array($this, \'getpostblock\') );

    add_action( \'wp_enqueue_scripts\', array( $this, \'inlinestyle\') );

}

public function getpostblock( $atts ){
    extract( shortcode_atts( 
    array(
        \'section_color\' => \'#000000\',

    ), $atts) );

    return $section_color;
}
public function inlinestyle(){
 $color = $this->getpostblock();  
 wp_enqueue_style(
        \'custom-style\',
        get_template_directory_uri() . \'/css/style.css\'
    );
    $custom_css = "
        div[data-id=289374].post-block-post-left.active,
        div[data-id=289374].post-block-post-right.active{
            background : {$color};
            border-color : {$color};
        }";
    wp_add_inline_style( \'custom-style\', $custom_css );
}
}
$test = new MakeShortcode();
结束

相关推荐

Shortcode IF statment help

下面是我一直致力于运行短代码的代码。我已经尝试实施if() 声明,但没有任何效果。我有两个输出。我想做的是,如果属性(\'img\', \'user\', \'text\') 显示在快捷码中$output1.否则,如果属性(\'img\', \'text\', \'length\', \'material\', \'power\') 显示在快捷码中$output2.function hire_equipment_func($atts) { extract( s