2017-10-09 55 views
0

你好,我试图让这个wp_query循环的工作,但得到错误,虽然意想不到,虽然另一个类似的LOPP是罚款输出最近的新闻。请指教。第一个循环用于输出“新闻”类别的帖子。通过wp_Query输出帖子的麻烦

Parse error: syntax error, unexpected 'endwhile' (T_ENDWHILE) 
add_shortcode('faq_all', 'FAQ_all_function'); 

function FAQ_all_function($atts) { 

    $query = new WP_Query(array(
    'category_name' =>'FAQ', 
    'posts_per_page' => -1, 
    'order' => 'ASC', 
    'orderby' => 'title', 
)); 
if ($query->have_posts()) { ob_start(); ?> 
<div class="col-lg-12 col-xs-12 faq"><?php 
    while ($query->have_posts()) ; $query->the_post(); 
?> 
<article><h5><a href="<?php the_permalink() ?>"> the_title() </a></h5> 
<div class="post-text"> the_content() <br> 
<div class="inner-height collapse" id="<?php the_ID() ?>"></div> 
<div class="inner-height-long collapse" id="<?php the_ID() ?>"></div> 
<button class="btn transparent" type="button" data-toggle="collapse" data-target="#<?php the_ID() ?>" aria-expanded="true" aria-controls="<?php the_ID() ?>"><i class="icon-chevron-down"></i>Открыть полность</button> 
</div> 
    </article><?php 
    ?>  
</div> <?php 
endwhile; ob_get_clean(); wp_reset_postdata(); 

} 
} 

这是正确输出常见问题类别的工作循环。你可以检查两个循环作为工作检查,但第二个基于wp-> query requset wp数据库是完全可行的 输出基于php文档的缓冲片段并打印出来。

add_shortcode('faq1', 'FAQ_function1'); 
function FAQ_function1($atts , $faq1) { 
/*start buffering content with nulled variable t the start of the loop*/ 
$faq1=null; 
    $query = new WP_Query(array(
     'category_name' =>'FAQ', 
     'posts_per_page' => -1, 
     'order' => 'ASC', 
      'cat' => '3', 
     'orderby' => 'title', 
    )); 
    if ($query->have_posts()) { ob_start(); 
?> 
<div class="col-lg-6 col-xs-12"> 

      <?php while ($query->have_posts()) :static $counter = 0; $query->the_post(); 
$count = $query->post_count; 
if ($counter > ($count/2)): 
?> 
<article> 
       <h5 href="<?php the_permalink(); ?>"><?php the_title(); ?></h5> 
<div class="post-text"><?php the_content(); ?></div> 
     </article> 

    <?php endif; 
      $counter++; 
      endwhile; ?> </div> <?php return ob_get_clean(); 
/*start buffering content with nulled variable of the start of the loop. Finished with content - outputting. */ 

     wp_reset_postdata(); ?> 


    <?php 

} 

也许这是一个错过brasket,但有眼痛,看是什么inorrect:

回答

0

这种替换代码:

add_shortcode('faq_all', 'FAQ_all_function'); 

function FAQ_all_function($atts) { 

    $query = new WP_Query(array(
    'category_name' =>'FAQ', 
    'posts_per_page' => -1, 
    'order' => 'ASC', 
    'orderby' => 'title', 
)); 
if ($query->have_posts()) { ob_start(); ?> 
<div class="col-lg-12 col-xs-12 faq"><?php 
    while ($query->have_posts()): $query->the_post(); 
?> 
<article><h5><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h5> 
<div class="post-text"><?php the_content(); ?><br> 
<div class="inner-height collapse" id="<?php the_ID() ?>"></div> 
<div class="inner-height-long collapse" id="<?php the_ID() ?>"></div> 
<button class="btn transparent" type="button" data-toggle="collapse" data-target="#<?php the_ID() ?>" aria-expanded="true" aria-controls="<?php the_ID() ?>"><i class="icon-chevron-down"></i>Открыть полность</button> 
</div> 
    </article><?php 
    ?>  
</div> 
<?php 

endwhile; 
ob_get_clean(); 
wp_reset_postdata(); 
} 
} 

你需要替换“;”与':'在这里:

while ($query->have_posts()); $query->the_post(); 

你也没有包装一些函数在php标签。

+0

谢谢,Ali_k。我会尝试。因为它只是正确的。会让你知道。谢谢一堆! –

+0

没问题,如果它适合你,请检查这个答案。 –

0

while ($query->have_posts()) ;

我认为这应该是一个冒号而不是分号。

顺便说一句:我强烈建议不要使用该语法功能。使用大括号。它们让阅读代码更容易(缩进,btw),每个带有基本语法高亮功能的编辑器都可以匹配它们,而且它们不是一个PHP特有的东西,不熟悉php的人会跳过。

+0

当然是。该代码的某些部分工作。我会替换检查出来。 –