2013-12-14 129 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”将是名字 –

相关问题