PHP函数中的Wordpress Shortcode无法正常工作

时间:2013-09-23 23:08:38

标签: php wordpress function shortcode

我对PHP很陌生,但在创建网站的过去18年里,他对HTML和CSS有着丰富的经验。我的问题是我需要在wordpress中创建一个函数,它也会在其中使用短代码。我已经让它工作到一个点,函数中的短代码只显示文本而不执行它的功能。这是我到目前为止所做的:

function artistArea($atts, $content = null) {
   extract(shortcode_atts(array('number' => '#', 'photo' => '#', 'name' => '#', 'video' => '#', 'audio' => '#', 'gallery' => '#', 'bio' => '#', 'element1' => '#','element2' => '#','element3' => '#','element4' => '#'), $atts));  

    switch ($number) {
        case '1' :
            $element1 = '1';
            $element2 = '2';
            $element3 = '3';
            $element4 = '4';
            break;
        case '2' :
            $element1 = '5';
            $element2 = '6';
            $element3 = '7';
            $element4 = '8';
            break;
        case '3' :
            $element1 = '9';
            $element2 = '10';
            $element3 = '11';
            $element4 = '12';
            break;
        case '4' :
            $element1 = '13';
            $element2 = '14';
            $element3 = '15';
            $element4 = '16';
            break;
        case '5' :
            $element1 = '17';
            $element2 = '18';
            $element3 = '19';
            $element4 = '20';
            break;
        default :
            $element1 = '1';
            $element2 = '2';
            $element3 = '3';
            $element4 = '2';
            break;
        break;
    }

        switch ($audio) {
        case '0' :
            $audioSection = ''; break;
        default :
            $audioSection = '
        <div class="artists_audio">
            <a class="lbp-inline-link-'.$element2.' cboxElement" href="#">AUDIO</a>
            <div style="display: none;"><div id="lbp-inline-href-'.$element2.'" style="padding:10px; background: #fff;">[soundcloud url="'.$audio.'" params="" width=" 100%" height="166" iframe="true" /]</div></div>
        </div>'; break;
        break;
    }

        switch ($video) {
        case '0' :
            $videoSection = ''; break;
        default :
            $videoSection = '       <div class="artists_video">
            <a class="lbp-inline-link-'.$element1.' cboxElement" href="#">VIDEO</a>
            <div style="display: none;"><div id="lbp-inline-href-'.$element1.'" style="padding:10px; background: #fff;">'.$video.'</div></div>
        </div>'; break;
        break;
    }

        switch ($gallery) {
        case '0' :
            $gallerySection = ''; break;
        default :
            $gallerySection = '                 <div class="artists_pictures">
            <a class="lbp-inline-link-'.$element3.' cboxElement" href="#">PICTURES</a>
            <div style="display: none;"><div id="lbp-inline-href-'.$element3.'" style="padding:10px; background: #fff;">[nggallery id='.$gallery.']</div></div>
        </div>'; break;
        break;
    }   


   return'<div class="artist_enclosure">
    <div class="artists_photo">
        <img src="'.$photo.'" alt="'.$name.'" width="150" height="150" class="alignnone size-thumbnail wp-image-39" />
    </div>
    <div class="artists_name"> 
        '.$name.'
    </div>
    <div class="artists_bio">
        '.$bio.'
    </div>

    <div class="artists_media">

        '.$videoSection.'

        '.$audioSection.'

        '.$gallerySection.'

        <div class="artists_booknow">
            <a class="lbp-inline-link-'.$element4.' cboxElement" href="#">BOOK NOW!</a>
            <div style="display: none;"><div id="lbp-inline-href-'.$element4.'" style="padding:10px; background: #fff;">[contact-form-7 id="4" title="Lounge Artists"]</div></div>
        </div>
    </div>
</div>

    ';
}
add_shortcode('artist', 'artistArea');

这是我在一个页面中发布的短代码,除了视频,音频和图库部分外,它的工作非常出色。

[artist number="1" photo="http://www.classiccabaret.com.au/wp-content/uploads/2013/08/moniique-e1376982930857-150x150.jpg" name="Monique Hebrard" bio="Energetic and versatile, Monique is a multi-talented singer, saxophonist, actress, and dancer. Monique has vast experience in hotel and corporate events. Her musical repertoire covers a variety of Jazz, Latin, Pop, Blues, Funk, RnB, and African." video="http://www.youtube.com/watch?v=0cDK2HTLbYg" audio="0" gallery="7" ]

如果有人可以帮助解决这个问题,那将会非常有用。

1 个答案:

答案 0 :(得分:0)

您需要通过do_shortcode()函数

运行输出HTML

例如:

   $output = '<div class="artist_enclosure">
    <div class="artists_photo">
        <img src="'.$photo.'" alt="'.$name.'" width="150" height="150" class="alignnone size-thumbnail wp-image-39" />
    </div>
    <div class="artists_name"> 
        '.$name.'
    </div>
    <div class="artists_bio">
        '.$bio.'
    </div>

    <div class="artists_media">

        '.$videoSection.'

        '.$audioSection.'

        '.$gallerySection.'

        <div class="artists_booknow">
                <a class="lbp-inline-link-'.$element4.' cboxElement" href="#">BOOK NOW!</a>
                <div style="display: none;"><div id="lbp-inline-href-'.$element4.'" style="padding:10px; background: #fff;">[contact-form-7 id="4" title="Lounge Artists"]</div></div>
            </div>
        </div>
    </div>

        ';
return do_shortcode($output);