WordPress定位特定页面上的特定帖子类别

时间:2013-12-14 06:48:02

标签: php wordpress

您好我正在尝试定位以定位在特定网页上显示的特定帖子。

我正在建造一个住宅建筑商网站,所以我有类似

的帖子

1)待售房屋 2)展示家园 3)特色家园 4)可用批次

我的页面与类别相对应。 1)显示房屋2)可用房屋3)可用房屋

我想要完成的是我想要显示像这样的组合

页面上(可用的家庭)          - 所有类别/ slugs“待售房屋”

的帖子

页面上(SHOW HOMES) - 所有类别/ slugs“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>

2 个答案:

答案 0 :(得分:0)

执行此操作的最佳方法是使用自定义帖子类型。这允许您创建自定义存档和分类。

此插件适用于非编码人员:

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

但与往常一样,手抄本是你的朋友:

http://codex.wordpress.org/Post_Types

通过学习使用自定义帖子类型,您不必诉诸任何变态代码来实现目标。

要从特定的slug中获取一系列帖子,您可以执行类似这样的操作,其中&#39; category-name&#39;是你的slu

$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 **/

}

答案 1 :(得分: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循环..但是代码有效!