2015-10-18 74 views
0

我正嘗試從WordPress的某些類別加載帖子。所有這些工作正常,但我無法實現所需的HTML結構。我要的是:按類別的WordPress發佈 - 功能輸出(HTML)的佈局在佈局中不正確

<h2>Heading<a>link</a></h2> 
<div class="row"> 
    ...posts.... 
</div> 

的問題是,當我運行下面是我得到的是更喜歡代碼:

<div>..post...</div> 
<div>..post...</div> 
<div>..post...</div> 
<h2>Heading<a>link</a></h2> 
<div class="row"> <!--- empty ---> </div> 

我運行下面的代碼查詢職位並告訴他們網站。請注意我認爲問題所在的評論。在html標籤發佈之前,echo會被執行嗎?

<?php 
    //Gets category posts 
    global $wp_query; 
    $cats = get_the_category(); 
    $tempQuery = $wp_query; 
    $currentId = $post->ID; 
    // related category posts 
    forEach($cats as $c) { 
     $categoryPosts=" "; 
     $newQuery = "posts_per_page=8&cat=" . $c->cat_ID; 
     query_posts($newQuery); 
     $count = 0; 
     while (have_posts()) { 
      the_post(); 
      if($count<=8 && $currentId!=$post->ID) { 
       $count++; 
       $categoryPosts .= get_content(); 
      } 
     } 
    ?> 
    <!-- The problem probably lies somewhere in this section, but I just cannot figure out what is happening --> 
     <h2>More in: <a href="<?php get_category_link($c->cat_ID); ?>"><?php echo $c->cat_name; ?></a></h2> 
     <div class="row"> 
      <?php echo $categoryPosts; ?> 
     </div> 
     <?php 
    } 
?> 

我這是怎麼retreive每個帖子的格式通過get_content()函數

function get_content() { ?> 
<div class="col-xs-12 col-sm-6 col-md-3"> 
    <a href="<?php the_permalink(); ?>"><?php 
     $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); 
     if ($thumbnail) (string)$thumbnail = $thumbnail[0]; 
     if (!empty($thumbnail)) { ?> 
      <img class="media-object" src="<?php echo $thumbnail; ?>" alt="<?php the_title(); ?>"> 
     <?php } else { ?> 
      <img class="media-object" src="" alt="<?php the_title(); ?>"> 
     <?php } ?> 
     <h4><?php the_title(); ?> </h4></a> 
</div> <?php 
} 

我認爲,功能只是不執行或在它們的排列順序返回。還是有什麼我失蹤?

+0

在你的函數中使用輸出緩衝並返回輸出 – Rasclatt

+0

我設法解決它謝謝。我認爲問題是,該get_content()沒有回顯或返回的帖子,但立即輸出的HTML,這導致了int $ categoryPosts。= get_content();不按照我的意圖行事。 – PeterTheLobster

回答

1

正如我評論,請使用緩衝區裏面的功能,那麼你就不必擔心在機會做緩衝區其他地方要重複使用的功能。當你return緩衝器,輸出/ HTML的位置將被插入,你打算:

function get_content() 
    { 
     ob_start(); ?> 
<div class="col-xs-12 col-sm-6 col-md-3"> 
    <a href="<?php the_permalink(); ?>"><?php 
     $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); 
     if ($thumbnail) (string)$thumbnail = $thumbnail[0]; 
     if (!empty($thumbnail)) { ?> 
      <img class="media-object" src="<?php echo $thumbnail; ?>" alt="<?php the_title(); ?>"> 
     <?php } else { ?> 
      <img class="media-object" src="" alt="<?php the_title(); ?>"> 
     <?php } ?> 
     <h4><?php the_title(); ?> </h4></a> 
</div> <?php 
     // Assign the data to a variable 
     $data = ob_get_contents(); 
     ob_end_clean(); 
     // Return the contents 
     return $data; 
    } 

要使用您可以使用您.=,或者你可以重複像這樣:

echo get_content(); 

做這這樣,您可以將其轉換爲[shortcode]並獲得相同的結果。食物的思想。

+0

哦,我明白你的意思了......這個解決方案更好,更簡單!謝謝 – PeterTheLobster

0

我已經通過調整循環來解決問題了。我的get_content()函數並沒有實際回顯或返回每個帖子結構,而是立即以html的形式輸出,這可能是我得到糟糕的結構和$ categoryPosts。= get_content();沒有按照我想象的那樣工作。

<?php 
    global $wp_query; 
    $cats = get_the_category(); 
    $tempQuery = $wp_query; 
    $currentId = $post->ID; 
    // related category posts 
    forEach($cats as $c) { 
     ob_start(); 
     $categoryPosts=" "; 
     $newQuery = "posts_per_page=10&cat=" . $c->cat_ID; 
     query_posts($newQuery); 
     $count = 0; ?> 
     <h2 class="new-posts">New in <a href="<?php get_category_link($c->cat_ID);?>"><?php echo $c->cat_name;?></a></h2> 
     <div class="row"> 
     <?php 
     while (have_posts()) { 
      the_post(); 
      if($count<8 && $currentId!=$post->ID) { 
       $count++; 
       get_content(); 
      } 
     } ?> 
     </div> <?php 
     ob_end_flush(); 
    } 
?>