2016-12-29 2 views
1

같은 페이지에 단편을 두 번 추가하려하지만 두 번째 단락이 아무 것도 표시하지 않는 것이 문제입니다. 나는 이것이 쿼리에 문제가 될 수 있다고 생각하지만, 이것에 대해서는 확실하지 않다. 내 짧은 코드는 다음과 같습니다.동일한 페이지에서 두 번 단축 코드 Wordpress

<?php 

defined('ABSPATH') or die('No script kiddies please!'); 

if (! is_admin()) { 
     require_once(ABSPATH . 'wp-admin/includes/post.php'); 
} 

function eleva_shortcode($atts = array()){ 

    $atts=shortcode_atts(array(

     'display' => '3', 
     'limit' => '5', 
     'length' => '15', 
     'title' => "", 
     'category' => "" 
    ), $atts) ; 

    $display = $atts['display'] ; 
    $pars = intval($display); 
    $col = 0; 

    if($pars==1 || $pars==2 || $pars==3 || $pars==4 || $pars==6){ 
     $col = 12/$pars; 
    } 

    else{ 

     $pars =3; 

     $col = 12/$pars; 

    } 

    $limit = $atts['limit']; 

    $pars_limit = intval($limit); 

    $length = $atts['length']; 

    $pars_length = intval($length); 

    $title = $atts['title']; 

    $id = post_exists($title); 

    $taxonomy = $atts['category']; 


    ?> 

    <script> 

      var tQ = jQuery.noConflict(); 

     tQ(document).ready(function(){ 

      if (tQ(window).width() > 992) { 

       var testimonialItem = tQ('#testimonials-slider-shortcode div[class*="col-md-"]'); 

       for(var i = 0; i < testimonialItem.length; i+=<?php echo $pars;?>) { 

        testimonialItem.slice(i, i+<?php echo $pars;?>).wrapAll('<div class="item"></div>'); 

       } 

      } else{ 

       var testimonialItem = tQ('#testimonials-slider-shortcode div[class*="col-md-"]'); 

       for(var i = 0; i < testimonialItem.length; i+=1) { 

        testimonialItem.slice(i, i+1).wrapAll('<div class="item"></div>'); 

       } 

      } 

      var testimonialBoxShort = 0; 

      tQ('#testimonials-slider-shortcode .item').each(function(){ 

       if(tQ(this).height() > testimonialBoxShort){ 

        testimonialBoxShort = tQ(this).height(); 

       } 

      });  

      tQ('#testimonials-slider-shortcode .item').height(testimonialBoxShort); 

      tQ('#testimonials-slider-shortcode .item').first().addClass('active'); 

     }); 

    </script> 

    <div id="testimonials-slider-shortcode" class="carousel slide" data-ride="carousel"> 

     <ol class="carousel-indicators"></ol> 

     <div class="carousel-inner" role="listbox"> 

     <?php 



     if($id == 0 && empty($taxonomy)){ 

      $queryT = new WP_Query(array('post_type' => 'testimonials')); 

     }elseif ($id == 0 && !empty($taxonomy)){ 

       $queryT = new WP_Query(array('post_type' => 'testimonials', 'tax_query' => array(
          array(
          'taxonomy' => 'categorytestimonials', 
          'field' => 'slug', 
          'terms' => $taxonomy, 
         )))); //p is for the id; 

     }elseif ($id != 0 && empty($taxonomy)){ 

       $queryT = new WP_Query(array('post_type' => 'testimonials', 'p' => $id)); //p is for the id; 

     }else{ 

       $queryT = new WP_Query(array('post_type' => 'testimonials', 'p' => $id, 'tax_query' => array(
          array(
          'taxonomy' => 'categorytestimonials', 
          'field' => 'slug', 
          'terms' => $taxonomy, 
         )))); //p is for the id;)); //p is for the id; 

     }  

     if ($queryT->have_posts()) : ?> 

      <!-- the loop --> 

      <?php $count = 0; 

      while ($queryT->have_posts()) : $queryT->the_post(); ?> 

      <?php //variables 

       $author = get_post_meta(get_the_ID(), 'author', true); 

       $link = get_permalink(); 

      ?> 

      <?php if($count < $pars_limit || $pars_limit==0): ?> 

      <div class="col-md-<?php echo $col;?>"> 

       <div class="img-wrap"> 

        <?php if (has_post_thumbnail()) : // check if the post has a Post Thumbnail assigned to it. ?> 

         <a href="<?php echo $link;?>"> 

          <?php the_post_thumbnail('full');?> 

         </a> 

        <?php else : ?> 

         <a href="<?php echo $link;?>"> 

          <img alt="<?php bloginfo('name'); ?>" src="<?php echo get_template_directory_uri(); ?>/images/default.jpg"> 

         </a> 

        <?php endif; ?> 

       </div> 

       <div class="info-wrap"> 

        <p><?php echo eleva_testimonials_excerpt($pars_length +1); ?></p> 

        <?php // Check if the custom field has a value. 

         if (! empty($author)) { 

          echo '<span><a href="'. $link .'">'. $author . '</a></span>'; 

         } 

        ?> 

       </div> 

      </div> 

      <?php endif; 

        $count= $count +1;?> 

      <?php endwhile; ?> 

      <!-- end of the loop --> 

      <?php wp_reset_postdata(); ?> 

     </div> 

     <a class="left carousel-control" href="#testimonials-slider-shortcode" role="button" data-slide="prev"> 

      <span class="glyphicon glyphicon-chevron-left fa fa-angle-left" aria-hidden="true"></span> 

      <span class="sr-only">Previous</span> 

     </a> 

     <a class="right carousel-control" href="#testimonials-slider-shortcode" role="button" data-slide="next"> 

      <span class="glyphicon glyphicon-chevron-right fa fa-angle-right" aria-hidden="true"></span> 

      <span class="sr-only">Next</span> 

     </a> 

     <?php else : ?> 

      <p><?php _e('Sorry, no testimonials at this time'); ?></p> 

     <?php endif; ?> 

      </div> 
     <?php 
} 

add_shortcode('testimonials', 'eleva_shortcode'); 

?> 

답변

0

종료 후 데이터를 재설정해야합니다. 자세한 내용

/* Restore original Post Data */  `wp_reset_postdata();` 

https://codex.wordpress.org/Class_Reference/WP_Query 제 추측 문제 때문에 동일한 ID 속성

2 개 HTML 요소를 갖는로 일어나고있다

+0

내가 말 루프 후 그랬어 : 위대한 작품을 –

0

는 다음 코드 사용해, 각 슬라이더 소자에 대한 고유 ID를 생성 PHP의 uniqid 기능을 사용하여

<?php 

defined('ABSPATH') or die('No script kiddies please!'); 

if (! is_admin()) { 
     require_once(ABSPATH . 'wp-admin/includes/post.php'); 
} 

function eleva_shortcode($atts = array()){ 

    $atts=shortcode_atts(array(

     'display' => '3', 
     'limit' => '5', 
     'length' => '15', 
     'title' => "", 
     'category' => "" 
    ), $atts) ; 

    $el_id = uniqid(); 

    $display = $atts['display'] ; 
    $pars = intval($display); 
    $col = 0; 

    if($pars==1 || $pars==2 || $pars==3 || $pars==4 || $pars==6){ 
     $col = 12/$pars; 
    } 

    else{ 

     $pars =3; 

     $col = 12/$pars; 

    } 

    $limit = $atts['limit']; 

    $pars_limit = intval($limit); 

    $length = $atts['length']; 

    $pars_length = intval($length); 

    $title = $atts['title']; 

    $id = post_exists($title); 

    $taxonomy = $atts['category']; 

    ob_start(); 
    ?> 

    <script> 

      var tQ = jQuery.noConflict(), 
       elId = '<?php echo $el_id; ?>'; 

     tQ(document).ready(function(){ 

      if (tQ(window).width() > 992) { 

       var testimonialItem = tQ('#testimonials-slider-shortcode-' + elId + ' div[class*="col-md-"]'); 

       for(var i = 0; i < testimonialItem.length; i+=<?php echo $pars;?>) { 

        testimonialItem.slice(i, i+<?php echo $pars;?>).wrapAll('<div class="item"></div>'); 

       } 

      } else{ 

       var testimonialItem = tQ('#testimonials-slider-shortcode-' + elId + ' div[class*="col-md-"]'); 

       for(var i = 0; i < testimonialItem.length; i+=1) { 

        testimonialItem.slice(i, i+1).wrapAll('<div class="item"></div>'); 

       } 

      } 

      var testimonialBoxShort = 0; 

      tQ('#testimonials-slider-shortcode-' + elId + ' .item').each(function(){ 

       if(tQ(this).height() > testimonialBoxShort){ 

        testimonialBoxShort = tQ(this).height(); 

       } 

      });  

      tQ('#testimonials-slider-shortcode-' + elId + ' .item').height(testimonialBoxShort); 

      tQ('#testimonials-slider-shortcode-' + elId + ' .item').first().addClass('active'); 

     }); 

    </script> 

    <div id="testimonials-slider-shortcode-<?php echo $el_id; ?>" class="carousel slide" data-ride="carousel"> 

     <ol class="carousel-indicators"></ol> 

     <div class="carousel-inner" role="listbox"> 

     <?php 



     if($id == 0 && empty($taxonomy)){ 

      $queryT = new WP_Query(array('post_type' => 'testimonials')); 

     }elseif ($id == 0 && !empty($taxonomy)){ 

       $queryT = new WP_Query(array('post_type' => 'testimonials', 'tax_query' => array(
          array(
          'taxonomy' => 'categorytestimonials', 
          'field' => 'slug', 
          'terms' => $taxonomy, 
         )))); //p is for the id; 

     }elseif ($id != 0 && empty($taxonomy)){ 

       $queryT = new WP_Query(array('post_type' => 'testimonials', 'p' => $id)); //p is for the id; 

     }else{ 

       $queryT = new WP_Query(array('post_type' => 'testimonials', 'p' => $id, 'tax_query' => array(
          array(
          'taxonomy' => 'categorytestimonials', 
          'field' => 'slug', 
          'terms' => $taxonomy, 
         )))); //p is for the id;)); //p is for the id; 

     }  

     if ($queryT->have_posts()) : ?> 

      <!-- the loop --> 

      <?php $count = 0; 

      while ($queryT->have_posts()) : $queryT->the_post(); ?> 

      <?php //variables 

       $author = get_post_meta(get_the_ID(), 'author', true); 

       $link = get_permalink(); 

      ?> 

      <?php if($count < $pars_limit || $pars_limit==0): ?> 

      <div class="col-md-<?php echo $col;?>"> 

       <div class="img-wrap"> 

        <?php if (has_post_thumbnail()) : // check if the post has a Post Thumbnail assigned to it. ?> 

         <a href="<?php echo $link;?>"> 

          <?php the_post_thumbnail('full');?> 

         </a> 

        <?php else : ?> 

         <a href="<?php echo $link;?>"> 

          <img alt="<?php bloginfo('name'); ?>" src="<?php echo get_template_directory_uri(); ?>/images/default.jpg"> 

         </a> 

        <?php endif; ?> 

       </div> 

       <div class="info-wrap"> 

        <p><?php echo eleva_testimonials_excerpt($pars_length +1); ?></p> 

        <?php // Check if the custom field has a value. 

         if (! empty($author)) { 

          echo '<span><a href="'. $link .'">'. $author . '</a></span>'; 

         } 

        ?> 

       </div> 

      </div> 

      <?php endif; 

        $count= $count +1;?> 

      <?php endwhile; ?> 

      <!-- end of the loop --> 

      <?php wp_reset_postdata(); ?> 

     </div> 

     <a class="left carousel-control" href="#testimonials-slider-shortcode-<?php echo $el_id; ?>" role="button" data-slide="prev"> 

      <span class="glyphicon glyphicon-chevron-left fa fa-angle-left" aria-hidden="true"></span> 

      <span class="sr-only">Previous</span> 

     </a> 

     <a class="right carousel-control" href="#testimonials-slider-shortcode-<?php echo $el_id; ?>" role="button" data-slide="next"> 

      <span class="glyphicon glyphicon-chevron-right fa fa-angle-right" aria-hidden="true"></span> 

      <span class="sr-only">Next</span> 

     </a> 

     <?php else : ?> 

      <p><?php _e('Sorry, no testimonials at this time'); ?></p> 

     <?php endif; ?> 

      </div> 
     <?php 
    return ob_get_clean(); 
} 

add_shortcode('testimonials', 'eleva_shortcode'); 

?> 
+0

를 참조하십시오! 고마워! 너 한테 다른 걸 물어봐도 될까? 쇼트 코드는 항상 페이지 상단에 나타납니다. 어떻게 해결할 수 있습니까? –

+0

@manuelsilva 우리가 함수에서 반환하는 대신 출력을 출력하고 있기 때문에 지금 업데이트 된 코드를 확인하고 이것을 대답으로 받아 들여주십시오. –

+0

덕분에 작동이 좋아졌습니다. 이걸 aswer로 받아들이는 방법이 있습니까? ? 다시 한번 고마워. –

관련 문제