2013-02-02 118 views
-1

因此,我已經運行了一個bizzar問題,我知道它正在按照數組值的方式工作,但_html變量返回類似字符串(29)「」,這對我的知識來說意味着29個空格,因爲空格被視爲字符(隨時糾正我)。_html返回一個空字符串?

任何方式中,類:

<?php 

class AisisCore_Template_Helpers_Loop{ 

    protected $_options; 

    protected $_wp_query; 

    protected $_html = ''; 

    public function __construct($options = array()){ 
     global $wp_query; 

     if(isset($options)){ 
      $this->_options = $options; 
     } 

     if(null === $this->_wp_query){ 
      $this->_wp_query = $wp_query; 
     } 
    } 

    public function init(){} 

    public function loop(){ 
     if(isset($this->_options)){ 
      if(isset($this->_options['query'])){ 
       $this->_query_post($this->_options['query']); 
      }elseif(isset($this->_options['type']) && $this->_options['type'] == 'single'){ 
       $this->_single_post(); 
      }else{ 
       $this->_general_wordpress_loop(); 
      } 
     }else{ 
      $this->_general_wordpress_loop(); 
     } 
    } 

    protected function _general_wordpress_loop(){ 
     if($this->_wp_query->have_posts()){ 
      while($this->_wp_query->have_posts()){ 
       $this->_wp_query->the_post(); 
       the_excerpt(); 
      } 
     } 
    } 

    protected function _query_post($query){ 
     $empty_query = $this->_wp_query; 
     $wp_query = new WP_Query($query); 

     if($wp_query->have_posts()){ 
      while($wp_query->have_posts()){ 
       $wp_query->the_post(); 
       the_excerpt(); 
      } 
     } 

     next_posts_link('&laquo; Older Entries'); 
     previous_posts_link('Newer Entries &raquo;'); 

     $wp_query = $empty_query; 
    } 

    protected function _single_post(){ 
     if($this->_wp_query->have_posts()){ 
      while($this->_wp_query->have_posts()){ 
       $this->_wp_query->the_post(); 

       if(isset($this->_options['wrapper'])){ 
        $this->_html .= '<div '; 

        if(isset($this->_options['wrapper']['class'])){ 
         $this->_html .= 'class="'.$this->_options['wrapper']['class'].'"'; 
        }elseif(isset($this->_options['wrapper']['id'])){ 
         $this->_html .= 'class="'.$this->_options['wrapper']['id'].'"'; 
        } 

        $this->_html .= ' >'; 
       } 

       if(isset($this->_options['title_header'])){ 
        $this->_html .= '<'.$this->_options['title_header'].'>'; 
        the_title(); 
        $this->_html .= '</'.$this->_options['title_header'].'>'; 
       }else{ 
        the_title(); 
       } 

       $this->_html .= '<a href="'.get_author_posts_url(get_the_author_meta('ID')).'">'.the_author_meta('display_name').'</a>'; 
       the_date(); 
       if(isset($this->_options['image'])){ 
        if(isset($this->_options['size']) && isset($this->_options['args'])){ 
         the_post_thumbnail($this->_options['image']['size'], $this->_options['image']['args']); 
        }else{ 
         the_post_thumbnail('medium'); 
        } 
       } 

       the_content(); 

       if(isset($this->_options['wrapper'])){ 
        $this->_html .= '</div>'; 
       } 
      } 
     } 
    } 

} 

專注於函數是_single_post()函數。實例化並使用這個類,我們做到以下幾點:

$array = array(
    'wrapper' => array(
     'class' => 'span12' 
    ), 
    'title_header' => 'h1', 
    'image' => array(
     'size' => 'type', 
     'args' => array(
      'align' => 'centered', 
      'class' => 'thumbnail marginBottom20 marginTop20' 
     ) 
    ), 
    'type' => 'single' 
); 

$loop = new AisisCore_Template_Helpers_Loop($array); 
$loop->loop(); 

因此,大家可以看到我們設置的包裝DIV,一個title_header,圖像屬性和類型的單。問題是,我進入每個if語句,如果這是一個包裝,並且類鍵被設置爲值..但$ this - > _ html返回空。

當我var_dump($ this - > _ html);我得到一個空字符串。有人可能會問:「你是不是進入了你做這件事的地方 - > _ html。='有些內容';?答案是肯定的,我的字符串看起來是空的,幫我看看我在做什麼錯在這裏。

+0

我敢打賭,它的打印效果很好,但HTML標籤呈現爲... HTML。嘗試檢查頁面的來源? – Eric

+0

如果是id,請查看源代碼中的h1標籤,p標籤和其他關聯標籤,但我不要DONT。因此在這裏張貼。 – Adam

+0

不需要防守。你沒有說你檢查了源,所以值得問。 – Eric

回答

0

嗯,在那裏你打電話之類的東西the_title()the_author_meta('display_name')the_post_thumbnailthe_content()的地方是錯誤的。這些原因輸出,他們不返回字符串。

而且,不知道你在哪裏var_dumping數據,不可能說出預期的結果。