短代码功能

时间:2015-02-21 17:23:27

标签: php wordpress shortcode

问题在于:

我有两个div#post-playlist-list和post-playlist-description

我想使用两个短代码[texte-article]和[playlist-article]

我需要一个函数来获取[texte-article] [/ texte-article]之间的所有内容以放入#post-playlist-list

另一个将[播放列表 - 文章] [/播放列表 - 文章]内容放入#post-playlist-description

的功能

我开始在preg_replace学习,但语法并不容易学习。

我可以帮助我,这会很棒。

好的我试过这个功能我没有错误,但它似乎没有返回任何东西 我不需要逃离Regexp的第二个问题吗?比如“/ [texte-article](。*)[/ texte-article] /”

这是我的single.php中的代码:

        <div id="post-playlist-list" class="box">
        <?php 
        $id = get_the_ID();
        $post = get_post($id);
        $content = apply_filters('the_content', $post->post_content);
        $content = str_replace( ']]>', ']]&gt;', $content );
        echo $content;
        ?>
        </div>
        <div id="post-playlist-description" >
        <?php
        echo $content;
        ?>
        </div>

这是我的single.php中的代码:

    function texte_article_function() {

    preg_replace_callback("/\[texte-article\](.*)\[\/texte-article\]/", function($matches) {
      //$matches contains an array of all matches
      //$matches[0] stores the full pattern match: \[texte-article](.*)\[\/texte-article]
      //$matches[1] stores the first subpattern match: (.*)
      //Do whatever text parsing you need to do here and return the result.
    $content = $matches[0];
      return $content;
 }, $postdata);

}

function playlist_article_function() {

    preg_replace_callback("/\[playlist-article\](.*)\[\/playlist-article\]/", function($matches) {
      //$matches contains an array of all matches
      //$matches[0] stores the full pattern match: \[texte-article](.*)\[\/texte-article]
      //$matches[1] stores the first subpattern match: (.*)
      //Do whatever text parsing you need to do here and return the result.
    $content = $matches[0];
      return $content;
 }, $postdata);

}


function register_shortcodes(){
    add_shortcode('texte-article', 'texte_article_function');
    add_shortcode('playlist-article', 'playlist_article_function');
}
    add_action( 'init', 'register_shortcodes');

1 个答案:

答案 0 :(得分:0)

感谢所有答案:)这是解决方案,使用正则表达式:

<强>的functions.php

  function texte_article_function($content_texte_article) {
        //Get [texte-article] content
        $content_texte_article = preg_replace('#.*\[texte-article\](.*)\[\/texte-article\].*#s', '$1', $content_texte_article);
        return $content_texte_article;
    }

function playlist_article_function($content_playlist_article) {
    //get [fap_playlist] content
    $content_playlist_article = preg_replace('#.*\[playlist-article\](.*)\[\/playlist-article\].*#s', '$1', $content_playlist_article);
    return $content_playlist_article;
}

回调功能:

<?php 
            $id = get_the_ID();
            $post = get_post($id);
            $content_playlist_article = $content;
            $btn_play_index_id = $content;
            echo playlist_article_function($content_playlist_article);
            // echo $content;
            ?>
            <?php
            $content_texte_article = $content;
            echo texte_article_function($content_texte_article);                
    ?>

如果它可以帮助某人。