在wordpress中没有创建短代码

时间:2015-09-16 05:48:20

标签: wordpress

当我使用这个短代码时,它只显示[product_slider_home]。我还检查了shortcode_exists函数,但它显示为false。我确信在这方面有一些错误。我可以忽略任何非常明显的事情吗?

  if ( shortcode_exists( 'product_slider_home' ) ) 
{
   echo "true";
}
    else
{ 
  echo "false" ;
}







function product_slider_home()
    {
                                    global $wpdb;

                                    $sliders = $wpdb->get_results( "SELECT * FROM wp_posts where post_type_custom='zee_slider'" );


                                $output .=  '<section id="main-slider1" style="margin: 0; padding: 0;">
                                        <div class="carousel slide wet-asphalt">

                                            <div class="carousel-inner" data-ride="carousel">';
                                                foreach ($sliders as $key => $slider) {  //foreach starts
                                                $full_img           =   wp_get_attachment_image_src( get_post_thumbnail_id( $slider->ID ), 'full');
                                                $slider_position    =   get_post_meta($slider->ID, 'slider_position', true );
                                                $boxed              =   (get_post_meta($slider->ID, 'slider_boxed', true )=='yes') ? 'boxed' : '';
                                                $has_button         =   (get_post_meta($slider->ID, 'slider_button_text', true )=='') ? false : true;
                                                $button             =   get_post_meta($slider->ID, 'slider_button_text', true );
                                                $button_url         =   get_post_meta($slider->ID, 'slider_button_url', true );
                                                $video_url          =   get_post_meta($slider->ID, 'slider_video_link', true );
                                                $video_type         =   get_post_meta($slider->ID, 'slider_video_type', true );
                                                $bg_image_url       =   get_post_meta($slider->ID, 'slider_background_image', true );
                                                $custom_image       =   get_post_meta($slider->ID, 'slider_custom_image',true);
                                                $custom_image_url   =   wp_get_attachment_url($custom_image);
                                                $background_image   =   'background-image: url('.wp_get_attachment_url($bg_image_url).')';
                                                $columns            =   false;
                                                if( !empty($image_url) or !empty($video_url) ){
                                                $columns        =   true;
                                                }


                                                if($custom_image){
                                                $embed_code     = '<img src="'.$custom_image_url.'" class="custom" alt="ashirvad">';
                                                $columns        = true;
                                                }


                                        $output .='     <div class="prod_slid item '.($key==0) ? 'active' : '' .'" style="'.( $bg_image_url ) ? $background_image : '' .'">
                                                    <div class=" slider-content "style="height:80vh;">

                                                        <div class="'. ($columns) ? 'col-sm-6' : 'col-sm-6'  .'"style="padding-left:0px">
                                                            <div class="carousel-content ps_cc centered col-lg-9" style="margin-left:0%;padding-left:0px;">

                                                                <p class="'. $boxed .' animation animated-item-2">
                                                                    '. do_shortcode( $slider->post_content ) .'
                                                                </p>';

                                                             if( $has_button ){ 
                                                    $output .='         <br>
                                                                <a class="btn btn-md animation animated-item-3" href="'.$button_url .'">'. $button .'</a>';
                                                                }
                                                        $output .=  '</div>
                                                        </div>';
                                                         if($columns){
                                                              if($key == 1){
                                                                  $class= "sanitary";
                                                              }
                                                              else{
                                                                 $class=""; 
                                                              }
                                                        $output .='<div class="col-sm-6 hidden-xs animation animated-item-4" style="height:81.7vh;">
                                                            <div class="centered" >
                                                                <div class="embed-container'. $class.'"; }?>" style="width:70%;position:absolute;">';
                                                                    $embed_code; 
                                                        $output .='     </div>
                                                            </div>
                                                        </div>';
                                                         }

                                                $output .=' </div>
                                                    </div><!--/.item-->';
                                                     } // endforeach 
                                                $output .=' </div><
                                                    </div>

                                                    <a class="next hidden-xs" href="#main-slider1" data-slide="next">
                                                        <i class="icon-angle-right"></i>
                                                    </a>
                                                    <a class="prev hidden-xs" href="#main-slider1" data-slide="prev" style="left: -10%;">
                                                        <i class="icon-angle-left"></i>
                                                    </a>
                                                </section>';
                                     } 
                             return $output;    //returns the output                        
    add_shortcode('product_slider_home', 'product_slider_home');//creating shortcode

1 个答案:

答案 0 :(得分:1)

您在关闭大括号return $output; //returns the output之后添加了代码}。您需要在return $output; //returns the output之前写下}

用下面的代码替换你的代码并尝试:

<?php
if (shortcode_exists('product_slider_home')) {
    echo "true";
} else {
    echo "false";
}

function product_slider_home() {
    global $wpdb;

    $sliders = $wpdb->get_results("SELECT * FROM wp_posts where post_type_custom='zee_slider'");


    $output .= '<section id="main-slider1" style="margin: 0; padding: 0;">
    <div class="carousel slide wet-asphalt">

        <div class="carousel-inner" data-ride="carousel">';
    foreach ($sliders as $key => $slider) {  //foreach starts
        $full_img = wp_get_attachment_image_src(get_post_thumbnail_id($slider->ID), 'full');
        $slider_position = get_post_meta($slider->ID, 'slider_position', true);
        $boxed = (get_post_meta($slider->ID, 'slider_boxed', true) == 'yes') ? 'boxed' : '';
        $has_button = (get_post_meta($slider->ID, 'slider_button_text', true) == '') ? false : true;
        $button = get_post_meta($slider->ID, 'slider_button_text', true);
        $button_url = get_post_meta($slider->ID, 'slider_button_url', true);
        $video_url = get_post_meta($slider->ID, 'slider_video_link', true);
        $video_type = get_post_meta($slider->ID, 'slider_video_type', true);
        $bg_image_url = get_post_meta($slider->ID, 'slider_background_image', true);
        $custom_image = get_post_meta($slider->ID, 'slider_custom_image', true);
        $custom_image_url = wp_get_attachment_url($custom_image);
        $background_image = 'background-image: url(' . wp_get_attachment_url($bg_image_url) . ')';
        $columns = false;
        if (!empty($image_url) or ! empty($video_url)) {
            $columns = true;
        }


        if ($custom_image) {
            $embed_code = '<img src="' . $custom_image_url . '" class="custom" alt="ashirvad">';
            $columns = true;
        }


        $output .='     <div class="prod_slid item ' . ($key == 0) ? 'active' : '' . '" style="' . ( $bg_image_url ) ? $background_image : '' . '">
                <div class=" slider-content "style="height:80vh;">

                    <div class="' . ($columns) ? 'col-sm-6' : 'col-sm-6' . '"style="padding-left:0px">
                        <div class="carousel-content ps_cc centered col-lg-9" style="margin-left:0%;padding-left:0px;">

                            <p class="' . $boxed . ' animation animated-item-2">
                                ' . do_shortcode($slider->post_content) . '
                            </p>';

        if ($has_button) {
            $output .='         <br>
                            <a class="btn btn-md animation animated-item-3" href="' . $button_url . '">' . $button . '</a>';
        }
        $output .= '</div>
                    </div>';
        if ($columns) {
            if ($key == 1) {
                $class = "sanitary";
            } else {
                $class = "";
            }
            $output .='<div class="col-sm-6 hidden-xs animation animated-item-4" style="height:81.7vh;">
                        <div class="centered" >
                            <div class="embed-container' . $class . '" style="width:70%;position:absolute;">';
            $output .= $embed_code;
            $output .='     </div>
                        </div>
                    </div>';
        }

        $output .=' </div>
            </div>';
    } // endforeach 
    $output .=' </div>
    </div>

    <a class="next hidden-xs" href="#main-slider1" data-slide="next">
        <i class="icon-angle-right"></i>
    </a>
    <a class="prev hidden-xs" href="#main-slider1" data-slide="prev" style="left: -10%;">
        <i class="icon-angle-left"></i>
    </a>
</section>';

    echo $output; //returns the output
    die;
}

add_shortcode('product_slider_home', 'product_slider_home'); //creating shortcode
?>