doulao7572 2017-07-25 14:42 采纳率: 0%
浏览 56
已采纳

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>
  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集