[英]shortcode - within a shortcode WP PHP HTML
我想知道是否有办法将巧克力定向到两个不同的参数? 我会解释
这个简码 [group skill] => return $skill;
这是第二个简码 [group lang] => return $lang;
我有一个通过 ACF 打开的组,我想每次都将一个简码取出到不同的地方,到预定的地方
这是原始代码
<?php
add_shortcode('group', function ($atts) {
$group_field = get_field('info_global_course');
if ($group_field):
$attributes = shortcode_atts([
'level_skill' => $group_field[level_skill],
'lang' => $group_field[lang],
'if_id_true' => $group_field[if_id_true]
], $atts);
/* I thought about this way, maybe it's not a rally, but maybe it will give you an idea to help me a little
* return $attributes['level_skill'];
* return $attributes['lang'];
*/
endif;
});
?>
编辑:尝试失败后,我更喜欢在HTML之上构建设计(之前它是以Elementor的形式构建的)
我有以下代码:
<?php
/*************************************
* Returns the values from a certain group of the post
*************************************/
add_shortcode('group', function ($atts,$shortcode_twoo,$shortcode_three) {
$group_field = get_field('info_global_course');
if ($group_field):
$attributes = shortcode_atts([
'level_skill' => $group_field[level_skill],
'lang' => $group_field[lang],
'if_id_true' => $group_field[if_id_true]
], $atts);
return '
<div>
<div class="level_skill border_bottom">
<span class="box_left"><i aria-hidden="true" class="far fa-clock"></i> time </span>
<span class="box_right">'.$shortcode .'</span>
</div>
<div class="video border_bottom">
<span class="box_left"><i aria-hidden="true" class="fas fa-video"></i> Study chapters </span>
<span class="box_right">'.$shortcode1.'</span>
</div>
<div class="studants border_bottom">
<span class="box_left"><i aria-hidden="true" class="fas fa-user-graduate"></i> Registered students</span>
<span class="box_right">100</span>
</div>
<div class="level_skill border_bottom">
<span class="box_left"><i aria-hidden="true" class="fab fa-superpowers"></i> level skill</span>
<span class="box_right">מתקדמים</span>
</div>
<div class="lang border_bottom">
<span class="box_left"><i aria-hidden="true" class="fas fa-globe"></i>language</span>
<span class="box_right">עברית</span>
</div>
<div class="if_id_true border_bottom">
<span class="box_left"><i aria-hidden="true" class="fas fa-sticky-note"></i> Diploma</span>
<span class="box_right">כן</span>
</div>
</div>
';
endif;
});
?>
目标是将我将来创建的所有短代码插入到这个数组中
这就是我这样做的原因:
<?php
$shortcode = do_shortcode('[time]');
$shortcode1 = do_shortcode('[chapters]');
?>
或 HTML: [时间] [章节]
但是后来我陷入了如何插入各种SHORTCODE变量的相同问题
短代码定义 Class(创建具有所需名称的文件并将代码添加到文件中。
class MyPlugin_ShortCodes
{
/**
* Register the shortcodes for the public-facing side of the site.
*
* @since 1.0.0
*/
public function register_shortcodes()
{
add_shortcode('group', array($this, 'shortcode'));
}
public function shortcode($attrs, $content = "", $shortcode = "my-plugin")
{
$allAttrs = $attrs;
$attrs = shortcode_atts(array(
'type' => null,
'id' => null,
'code' => null,
), $attrs, $shortcode);
if ($attrs["type"] != null) {
try {
$f = 'do__' . $attrs["type"];
if (method_exists($this, $f))
return $this->$f($attrs, $content, $allAttrs);
return '[The "type" attribute did not match any known shortcode type. Found: ' . $attrs["type"] . ']';
} catch (Exception $ex) {
return json_encode($ex->getMessage());
}
}
return "Don't Know what to process!";
}
function do__skill($attrs, $content, $allAttrs)
{
$html = "Put your skills level here";
return $html;
}
function do__lang($attrs, $content, $allAttrs)
{
$html = "Do Whateaver you want";
return $html;
}
}
现在将以下行添加到您的插件定义文件中
$plugin_shortcodes = new MyPlugin_ShortCodes();
$this->loader->add_action('init', $plugin_shortcodes, 'register_shortcodes');
短代码可以被称为...
do_shortcode('[group type="skill" id="{$ID}"]');
do_shortcode('[group type="lang" id="{$ID}" code="en"]');
do_shortcode('[group type="xxxx"');
非常感谢所有试图帮助我的人,我找到了一种将所有简码组合成一个简码的好方法
<?php
/********************
* Consolidation of shortcodes
**********************/
add_shortcode('total_topics_chapters_group',function() {
$output = '';
$output .= do_shortcode('[time]');
$output .= do_shortcode('[chapters]');
$output .= do_shortcode('[group]');
return $output;
});
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.