2012-12-28 69 views
0

我無法正常工作。有人可以爲類別模板提供一個快速片段,顯示屬於名爲「產品A」的類別的帖子。在過去的3個小時裏,我一直使用試錯法,但沒有運氣。顯示屬於某個類別的帖子 - Wordpress

謝謝!

這是我一直在玩什麼周圍 -

<?php 
/* 
Template Name: yadayada 
*/ 
?> 

<?php get_header(); ?> 
<?php get_sidebar(); ?> 

<?php query_posts('cat=32&showposts=5'); ?> 
<div class="post"> 

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 


<div <?php post_class() ?> id="post-<?php the_ID(); ?>"> 
<div class="post-description"> 
<h2><?php the_title(); ?></h2> 
<?php the_content(); ?> 
</div> 
</div> 


<?php endwhile; else: ?> 
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 


</div> 
+0

爲什麼不向我們展示一些無法使用的試錯法? – maiorano84

+0

'showposts'已棄用。嘗試使用'posts_per_page = 5'代替,這是[query_posts()](http://codex.wordpress.org/Function_Reference/query_posts)函數的適當參數。你還應該在末尾使用'wp_reset_query()'。不過,你不提WP版。 –

回答

0

首先讓你的產品種類ID; (如果你使用,你的自定義查詢你的貓ID它是要去工作,而不是完美的類別名稱。)

<?php 
query_posts('cat=1'); 
while (have_posts()) : the_post(); 
the_content(); 
endwhile; 
?> 
1

您可以使用WP_Query類。 我之前完成的一種方法是首先創建Product-A的類別名稱,並將slug的產品「全部小寫」。

然後實例化該類的新實例。傳入'category_name = product-a'的參數您不會使用此參數傳遞類別名稱,而是傳遞段落名稱。一旦你做,你應該能夠使用WP_Query如下:

<?php $my_query = new WP_Query('category_name=product-a'); ?> 
    <?php if ($my_query->have_posts()) : ?> 
     <?php while ($my_query->have_posts()) : $my_query->the_post() ?> 
      <article <?php post_class() ?> id="post-<?php the_ID(); ?>"> 
       <h2><?php the_title(); ?></h2> 
       <div class="product-excerpt"><?php the_content(); ?> </div> 
      </article> 
     <?php endwhile; ?>   
     <?php else : ?> 
      <h2>Not Found</h2>  
    <?php endif; ?> 

幾乎一切是一樣的正規循環,但不是隻是

<?php if(have_post()) : while(have_post()) : the_post() ?> 

你會使用的對象符號指到這個特定的查詢。

<?php if($my_query->have_post()) : while($my_query->have_post()) : $my_query->the_post() ?> 

希望它有幫助。

相關問題