正如标题所述,我需要向add_shortcode()
作用换句话说,我传递的那些参数将在的回调函数中使用add_shortcode()
. 我该怎么做?
请注意,这些与以下结构无关[shortcode 1 2 3]
哪里1
, 2
, 和3
是用户传递的参数。在我的情况下,参数仅用于编程目的,不应由用户负责。
谢谢
正如标题所述,我需要向add_shortcode()
作用换句话说,我传递的那些参数将在的回调函数中使用add_shortcode()
. 我该怎么做?
请注意,这些与以下结构无关[shortcode 1 2 3]
哪里1
, 2
, 和3
是用户传递的参数。在我的情况下,参数仅用于编程目的,不应由用户负责。
谢谢
您可以使用closure 为此use
关键字。简单示例:
$dynamic_value = 4;
add_shortcode( \'shortcodename\',
function( $attributes, $content, $shortcode_name ) use $dynamic_value
{
return $dynamic_value;
}
);
另请参见Passing a parameter to filter and action functions.您可以在回调函数中传递属性数组($atts),该函数将在运行add\\u shortcode()时运行。
请注意$user_defined_attributes
在以下示例中:
add_shortcode( \'faq\', \'process_the_faq_shortcode\' );
/**
* Process the FAQ Shortcode to build a list of FAQs.
*
* @since 1.0.0
*
* @param array|string $user_defined_attributes User defined attributes for this shortcode instance
* @param string|null $content Content between the opening and closing shortcode elements
* @param string $shortcode_name Name of the shortcode
*
* @return string
*/
function process_the_faq_shortcode( $user_defined_attributes, $content, $shortcode_name ) {
$attributes = shortcode_atts(
array(
\'number_of_faqs\' => 10,
\'class\' => \'\',
),
$user_defined_attributes,
$shortcode_name
);
// do the processing
// Call the view file, capture it into the output buffer, and then return it.
ob_start();
include( __DIR__ . \'/views/faq-shortcode.php\' );
return ob_get_clean();
}
参考号:hellofromtonya我正在使用一个快捷代码处理我的产品信息,它可以自动生成一个包含信息的表。但这个短代码似乎被调用了两次。我不是一个优秀的后端开发人员,但我正在努力学习一些基础知识,以便能够制作一些基本的PHP函数。我非常感谢你的帮助。提前谢谢。我的代码如下所示:function displayTable() { echo \'<table>\'; echo \'<tbody>\'; $fields = get_field_objects();&