doulao7572
2017-07-25 14:42
浏览 51
已采纳

Bootstrap Post Carousel在页面模板中阻止其他PHP

I have created a post carousel within a WordPress page-template, but all other PHP (such as Advanced Custom Fields content and such) get blocked. The wp_debug shows no errors and I can't find any error within the code.

Below is the code I have used to create the carousel with recent posts from a custom post type:

When I entirely remove the slider all PHP below it does work/load, but I can't seem to find a mistake in the code.

<div class="carousel-inner" role="listbox">
<?php
    $i = 2; 
    global $post; 
    $args = array( 
            'numberposts'       => -1,
            'post_type'         => 'work',
            'orderby'           => 'date',
            'order'             => 'ASC',
    );
    $myposts = get_posts($args);
    if($myposts):
          $chunks = array_chunk($myposts, $i);
          $html = "";
          foreach($chunks as $chunk) {
            ($chunk === reset($chunks)) ? $active = "active" : $active = "";
            $html .= '<div class="item '.$active.'">';
            foreach($chunk as $post) {
              $html .= '<div id="timeline-item" class="col-lg-6 col-md-6 col-sm-6 col-xs-6"><div><h6 style="text-align: left;">';
              $html .= get_the_date('Y');
              $html .= '</h6><h2 style="text-align: left;">';
              $html .= get_the_title();
              $html .= '</h2><p style="text-align: left;">';
              $html .= get_post_field('post_content');
              $html .= '</p></div></div>';
            };
            $html .= '</div>';                
            };
          echo $html;             
        endif;
?>
</div>

图片转代码服务由CSDN问答提供 功能建议

我在WordPress页面模板中创建了一个post carousel,但是所有其他PHP(例如Advanced Custom Fields内容) 等等被封锁了。 wp_debug没有显示错误,我在代码中找不到任何错误。

下面是我用来创建旋转木马的代码,其中包含来自自定义帖子类型的最新帖子:

当我完全删除滑块时,它下面的所有PHP都可以正常工作/加载,但我似乎无法在代码中找到错误。

 <  code>&lt; div class =“carousel-inner”role =“listbox”&gt; 
&lt;?php 
 $ i = 2;  
全球$ post;  
 $ args = array(
'numberposts'=&gt; -1,
'post_type'=&gt;'work',
'orderby'=&gt;'date',
'order'=&gt;  'ASC',
); 
 $ myposts = get_posts($ args); 
 if($ myposts):
 $ chunks = array_chunk($ myposts,$ i); 
 $ html =“”; \  n foreach($ chunk作为$ chunk){
($ chunk === reset($ chunks))?  $ active =“active”:$ active =“”; 
 $ html。='&lt; div class =“item'。$ active。'”&gt;'; 
 foreach($ chunk as $ post){\  n $ html。='&lt; div id =“timeline-item”class =“col-lg-6 col-md-6 col-sm-6 col-xs-6”&gt;&lt; div&gt;&lt; h6 style  =“text-align:left;”&gt;'; 
 $ html。= get_the_date('Y'); 
 $ html。='&lt; / h6&gt;&lt; h2 style =“text-align:left;  “&gt;'; 
 $ html。= get_the_title(); 
 $ html。='&lt; / h2&gt;&lt; p style =”text-align:left;“&gt;'; 
 $ html。=  get_post_field('post_content'); 
 $ html。='&lt; / p&gt;&lt; / div&gt;&lt; / div&gt;'; 
}; 
 $ html。='&lt; / div&gt;';  
}; 
 echo $ html;  
 endif; 
?&gt; 
&lt; / div&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujing1967 2017-07-25 15:00
    已采纳

    You are overriding $post. In WordPress never override $post
    Try replacing this part.

    foreach($chunk as $slide) {
              $html .= '<div id="timeline-item" class="col-lg-6 col-md-6 col-sm-6 col-xs-6"><div><h6 style="text-align: left;">';
              $html .= get_the_date('Y', $slide);
              $html .= '</h6><h2 style="text-align: left;">';
              $html .= get_the_title($slide);
              $html .= '</h2><p style="text-align: left;">';
              $html .= get_post_field('post_content', $slide);
              $html .= '</p></div></div>';
            };
    

    Not sure if this will solve the rest of the problems. It might.
    And if it doesn't at least your code will be a bit cleaner.

    EDIT Seeing the full script this is very likely the problem.

    line 109 if( have_rows('skill-bars') will take the wrong $post which you broke.

    点赞 打赏 评论

相关推荐 更多相似问题