Php 如何以适当的方式向Wordpress快捷码(OOP样式)添加属性?
如何将属性(在本例中是一种语言)添加到Wordpress中的类中,以便在短代码中使用 假设我们有一个名为Php 如何以适当的方式向Wordpress快捷码(OOP样式)添加属性?,php,wordpress,oop,shortcode,Php,Wordpress,Oop,Shortcode,如何将属性(在本例中是一种语言)添加到Wordpress中的类中,以便在短代码中使用 假设我们有一个名为FormHandler class FormHandler { private $language = 'en'; public function __construct($language = null) { if ($language !== null) { $this->language = $language;
FormHandler
class FormHandler {
private $language = 'en';
public function __construct($language = null) {
if ($language !== null) {
$this->language = $language;
}
}
public function display_form() {
if ($this->language === 'se' {
return 'return html for the swedish form';
}
else {
return 'return html for the english form';
}
}
}
add_shortcode( 'contactform', array('FormHandler', 'display_form') );
在wordpress中,我想在内容中添加短代码,比如
[contactform lang=en] or [contactform lang=en] based on the language.
我可以使用全局变量来获取实际语言,但这似乎不是实现这一点的正确方法。您需要在
display\u form
函数中接受一个参数,以接收从短代码传入的属性
public function display_form( $attrs )
{
// you should have your defaults here
$defaults = array(
'lang' => $this->language
);
$args = wp_parse_args( $attrs, $defaults );
// now extract so you can use the variable directly
extract( $args );
if ( $lang == 'se' )
{
$html = 'Swedish';
} else
{
$html = 'English';
}
return $html;
}
现在您可以使用短代码了,但不要忘记语言周围的引号:
[contactform lang=“se”]
您需要在display\u form
函数中接受一个参数,以接收从短代码传入的属性
public function display_form( $attrs )
{
// you should have your defaults here
$defaults = array(
'lang' => $this->language
);
$args = wp_parse_args( $attrs, $defaults );
// now extract so you can use the variable directly
extract( $args );
if ( $lang == 'se' )
{
$html = 'Swedish';
} else
{
$html = 'English';
}
return $html;
}
现在您可以使用短代码了,但不要忘记语言周围的引号:
[contactform lang=“se”]
您需要在display\u form
函数中接受一个参数,以接收从短代码传入的属性
public function display_form( $attrs )
{
// you should have your defaults here
$defaults = array(
'lang' => $this->language
);
$args = wp_parse_args( $attrs, $defaults );
// now extract so you can use the variable directly
extract( $args );
if ( $lang == 'se' )
{
$html = 'Swedish';
} else
{
$html = 'English';
}
return $html;
}
现在您可以使用短代码了,但不要忘记语言周围的引号:
[contactform lang=“se”]
您需要在display\u form
函数中接受一个参数,以接收从短代码传入的属性
public function display_form( $attrs )
{
// you should have your defaults here
$defaults = array(
'lang' => $this->language
);
$args = wp_parse_args( $attrs, $defaults );
// now extract so you can use the variable directly
extract( $args );
if ( $lang == 'se' )
{
$html = 'Swedish';
} else
{
$html = 'English';
}
return $html;
}
现在您可以使用快捷码了,但不要忘记语言周围的引号:
[contactform lang=“se”]
为什么不能直接使用?语言只是一个例子,我很好奇如何在WP中处理属性/对象/短代码不依赖于属性是什么。为什么你不能直接使用?语言只是一个例子,我很好奇如何在WP中处理属性/对象/短代码不依赖于属性是什么。为什么你不能直接使用?语言只是一个例子,我很好奇如何在WP中处理属性/对象/短代码,不依赖于属性是什么。为什么你不能直接使用?语言只是一个例子,我很好奇如何在WP中处理属性/对象/短代码,不依赖于属性是什么。