2013-12-14 125 views
0

您好,我正在嘗試針對特定帖子進行分類以顯示在特定頁面上。WordPress針對特定頁面上的特定帖子類別

我建立一個住房建築商的網站,所以我必須交的類別,如

1)房屋出售 2)顯示舍 3)特點的家園 4)可用批次

,我有網頁核心反應到類別。 1)顯示舍2)可用公寓3)可用批次

我試圖做到的是,我要顯示這樣

組合在頁(適用HOMES) - 所有帖子說的是分類/蛞蝓 「出售公寓」

在頁面(SHOW HOMES) - 所有帖子說的是分類/蛞蝓 「展家」

我不知道如何做到這一點....

這是我得到了多少:

<?php if(have_posts()):?> 
<?php while(have_posts()) : the_post(); 
     $categoris = get_the_category(); 

     print_r($categoris); 

     if($categories){ 
      $class_names = array(); 

      foreach($categories as $category){ 
       $class_names[] = $category->slug; 
      } 

      $classes = join(' ', $class_names); 
     } 
?> 

<div class="row"> 
    <div class="large-4 columns"> 
     <div class="panel bodyHeight"> 
     <?php if(has_post_thumbnail()) : ?> 
     <a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a> 
     <?php endif;?> 
     </div> 
    </div> 
    <div class="large-8 columns"> 
     <div class="panel bodyHeight"> 
     <p>Price:  
        <?php $custom_fields = get_post_custom(); 
        $price = $custom_fields['price']; 
        foreach($price as $key => $value){ 
        echo $value; 
        } 
        ?> 
     </p> 
     <p>SqF:  
        <?php $custom_fields = get_post_custom(); 
        $sqf = $custom_fields['sqf']; 
        foreach($sqf as $key => $value){ 
        echo $value; 
        } 
        ?> 
     </p> 
     <p>Number of Bedrooms:  
        <?php $custom_fields = get_post_custom(); 
        $bed = $custom_fields['num_bedrooms']; 
        foreach($bed as $key => $value){ 
        echo $value; 
        } 
        ?> 
     </p> 
     <p>Number of Bathrooms:  
        <?php $custom_fields = get_post_custom(); 
        $bath = $custom_fields['num_bathrooms']; 
        foreach($bath as $key => $value){ 
        echo $value; 
        } 
        ?> 
     </p> 
     <p>Home Description:  
        <?php $custom_fields = get_post_custom(); 
        $desc = $custom_fields['description']; 
        foreach($desc as $key => $value){ 
        echo $value; 
        } 
        ?> 
     </p> 
     <p>Home Location:  
        <?php $custom_fields = get_post_custom(); 
        $address = $custom_fields['address']; 
        foreach($address as $key => $value){ 
        echo $value; 
        } 
        ?> 
     </p> 
     </div> 
    </div> 
</div> 
<?php endwhile;?> 
<?php else: ?> 
<div class="row"> 
    <div class="large-4 columns"> 
     <div class="panel bodyHeight"> 
     <h3>No posts were found.</h3> 
     </div> 
    </div> 
</div> <?php endif;?> 

</div> 

回答

0

想通了:

<?php 
    // The Query get all for posts categorized as 'featured' 
    query_posts(array ('category_name' => 'featured', 'posts_per_page' => -1)); 
?> 
<?php 
    // The Loop 
    while (have_posts()) : the_post(); 
     // Get all custom fields 
     $custom_fields = get_post_custom(); 
     // pull a field name 'description' 
     $desc = $custom_fields['description']; 
     // print value 
     foreach($desc as $key => $value){ 
     echo $value; 
     } 
    endwhile; 
?> 

<?php 
    // Reset Query 
    wp_reset_query(); 
?> 

如果有人可以編輯這段代碼,使其更短或更好,請讓我知道我不知道它是否適用於foreach循環..但正如代碼作品!

0

要做到這一點的最佳方法是與自定義帖子類型。這使您可以創建自定義檔案和分類。

這個插件是對非編碼:

http://wordpress.org/plugins/custom-post-type-ui/

但一如既往,食品是你的朋友:

http://codex.wordpress.org/Post_Types

通過學習使用自定義文章類型你不」不得不求助於任何糾結的代碼來實現你的目標。

要從特定蛞蝓,你可以做這樣的事情,在「類別名」是你塞得樁的陣列

$idObj = get_category_by_slug('category-name'); 
$id = $idObj->term; 
$posts = get_posts(array ('category' => $id, 'posts_per_page' => 5)); 
foreach ($posts as $post) { 

setup_postdata($post); 

/** use the post tags here **/ 

} 
+0

謝謝,我已閱讀第二個鏈接,但我處於主要時間線下,無法及時學習自定義類型。目前,我擁有我需要正確工作的一切,我需要的僅僅是學習如何按類別(過濾)當前帖子。 我現在所做的是爲每個頁面創建單獨的頁面,因此我有page-show-home.php並且我有頁面'Show Home'使用該頁面作爲模板。我現在需要的是一個由slug/category – Ljubisa

+0

的過濾器啊,所以我有點不清楚。每個頁面都會知道這個slu,,你只需要從這個slu category的類別中獲得帖子。那是對的嗎? –

+0

也要明白你的slu will不會是「單詞」,他們會成爲「單詞」。「word word」將是名字 –

相關問題