2012-08-23 30 views
1

我正在使用function.php中的循环,并且我知道循环不工作。我需要全球化吗?我环顾四周,我不确定。这是我的功能:function.php中的循环

function loadDiffSection($name) 
{ 
    $term = get_term_by('name', $name, 'category'); 
    $termIdFeat = $term->term_id; 

    $everything = ""; 

    if(have_posts()): 
     query_posts("cat=".$termIdFeat."&posts_per_page=5"); 
     $count = 1; 

     while(have_posts()) : the_post(); 
      set_post_thumbnail_size(520 , 0, true); 
      $everything .="<a href='?p=".the_ID()."'>"; 
       $everything .="<div class='pushLeft grid thisPost pos_".$count."'>"; 
        $everything .="<div class='capMedia hide'>"; 
          $everything .="<font size='3'>".get_the_title()."</font><br /><br />"; 
          $actualLen = strlen(strip_tags(removeImagefromContent())); 
          $limit = 200; 

          if($actualLen > $limit) 
          { 
           $everything .= substr(strip_tags(removeImagefromContent()), 0, $limit)."..."; 
          } 
          else 
          { 
           $everything .= strip_tags(removeImagefromContent()); 
          } 
        $everything .= "</div>"; 
        $everything .= get_the_post_thumbnail(); 
       $everything .= "</div></a>"; 
      $count++; 
     endwhile; 
    endif; 
    header('Content-Type: application/json; charset=UTF-8'); 
    echo json_encode(array("returned" => $everything)); 
    exit; 
} 

我使用这段代码来调用一个AJAX请求,所以它返回一个NULL响应。这导致我相信该循环在该函数内不起作用。

+0

你的错误是什么?以及为什么要在单字段JSON响应中包装纯HTML响应? – moonwave99

+0

没有错误,它只是返回一个NULL json响应,这意味着循环不起作用。我检查了。 – Majo0od

回答

0

在我看来像你需要括在正确的外壳您while语句可以这么说,

while(have_posts()) : the_post();应该是while((have_posts()) : the_post()); ,然后将该;上月底杀死,而右边有它的样子这while((have_posts()) : the_post()): 这是一个冒号而不是分号需要。试试看。

+0

不,这不是问题。直接来自wordpress网站的循环语法如下:<?php if(have_posts()):while(have_posts()):the_post(); ?>链接:http://codex.wordpress.org/The_Loop – Majo0od