短代码-在短代码WP PHP HTML中

xzabzqsa  于 2023-01-24  发布在  PHP
关注(0)|答案(2)|浏览(128)

我想知道是否有一种方法可以让巧克力有两个不同的参数?我会解释的
此简码[组技能]=〉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变量的同一个问题

lmvvr0a8

lmvvr0a81#

短代码定义类(创建具有所需名称的文件,并将代码添加到文件中。

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"');
noj0wjuj

noj0wjuj2#

非常感谢大家谁试图帮助我找到了一个很好的方法来合并所有的短代码成一个短代码

<?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;
    });
?>

相关问题