簡體   English   中英

簡碼 - 在簡碼 WP PHP HTML 內

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM