2017-06-02 27 views
0

例如,我將有4個類別。對於每個類別,我會顯示5個最近的帖子。我將有類別,如早餐,甜點,午餐和美味的食物。每個類別都會有一個「全部看」鏈接,這樣用戶就可以看到所有的早餐類別信息。在主頁上,我會列出最近發佈的5篇帖子,當用戶點擊「查看全部」鏈接時,它會將它們鏈接到整個早餐類別。如果他們點擊「全部查看」,我想要在該類別的所有早餐。其他類別也一樣。 目前我的代碼看起來像這樣,但我堅持「看到所有」鏈接。我不知道如何將它鏈接到主類別。如何根據類別顯示所有帖子?

<?php 
 

 
get_header(); 
 

 
?> 
 

 
<!-- recipe --> 
 
<section class="recipe-wrap"> 
 

 
\t <?php 
 
\t /* 
 
\t * Loop through Categories and Display Posts within 
 
\t */ 
 
\t $post_type = 'recipe'; 
 
\t $category_link = get_category_link($cat->cat_ID); 
 
\t 
 
\t // Get all the taxonomies for this post type 
 
\t $taxonomies = get_object_taxonomies(array('post_type' => $post_type)); 
 
\t 
 
\t foreach($taxonomies as $taxonomy) : 
 
\t 
 
\t  // Gets every "category" (term) in this taxonomy to get the respective posts 
 
\t  $terms = get_terms($taxonomy); 
 
\t 
 
\t  foreach($terms as $term) : ?> 
 

 
\t <div class="recipe-category owl-carousel-slide"> 
 
\t \t <div class="row"> 
 

 
\t \t \t <h2><?php echo $term->name; ?><a href="#">see all</a></h2> 
 
\t   
 
\t   <div class="recipe-category-carousel owl-carousel owl-theme"> 
 

 
\t   \t <?php 
 
\t \t   $args = array(
 
\t \t     'post_type' => $post_type, 
 
\t \t     'posts_per_page' => 10, //show all posts 
 
\t \t     'tax_query' => array(
 
\t \t      array(
 
\t \t       'taxonomy' => $taxonomy, 
 
\t \t       'field' => 'slug', 
 
\t \t       'terms' => $term->slug, 
 
\t \t     ) 
 
\t \t    ) 
 
\t \t 
 
\t \t   ); 
 
\t \t   $posts = new WP_Query($args); 
 
\t \t 
 
\t \t   if($posts->have_posts()): while($posts->have_posts()) : $posts->the_post(); ?> 
 

 
\t    <div class="item recipe-box"> 
 

 
\t    \t <a href="<?php the_permalink(); ?>"> 
 
\t     \t <img src="<?php echo(types_render_field('artwork', array('raw' => true))); ?>"> 
 
\t     \t <p><?php the_title(); ?></p> 
 
\t     </a> 
 
\t    </div> 
 

 
       <?php endwhile; endif; ?> 
 
\t \t \t   </div> 
 
\t \t \t   
 
\t \t \t   </section> 
 
\t \t \t 
 
\t \t \t  <?php endforeach; 
 
\t \t \t 
 
\t \t \t endforeach; ?> 
 

 
\t   </div> 
 

 
     </div> 
 
    </div> 
 
</section> 
 
<!-- /recipe --> 
 

 
<?php 
 

 
get_footer(); 
 

 
?>

+0

這不是一個[最小,完整,可驗證示例](https://stackoverflow.com/help/mcve)。請通過發佈一個最小的,完整的和可驗證的例子來幫助答覆者和未來的讀者。 –

回答

0

試試下面的代碼

foreach ($terms as $term) { 

    // Get term link by using get_term_link() 

    $term_link = get_term_link($term); 

    echo '<a href="' . esc_url($term_link) . '">' . $term->name . '</a>'; 

} 
+0

現在它顯示2個術語名稱(我使用「查看全部」)。我想爲每個類別顯示1個術語名稱(請參閱全部)。我怎麼做? –

+0

我需要包含我提供的代碼嗎?你能告訴我嗎?包括我的代碼和你的代碼嗎? :) –

0
<div id="mini_stream"> 
    <ul> 
<? $args = array(
    'post_type' => 'post', 
    'posts_per_page' => 4, 
    'category_name'=>'Product', 
); 

$loop = new wp_Query($args); 

while($loop->have_posts()) : $loop->the_post(); 
    echo '<a href="'.get_permalink().'">'; 
    echo get_the_post_thumbnail($post->ID, 'category-thumb'); 
    the_title('<h6>', '</h6>'); 
    echo '</a>'; 
endwhile; 

wp_reset_query(); ?> 
    </ul> 
</div> 

=>使用這種方法可能你得到所有發佈

相關問題