【发布时间】:2017-06-02 03:48:13
【问题描述】:
例如,我将有 4 个类别。对于每个类别,我将显示 5 个最近的帖子。我将有早餐、甜点、午餐和咸味食品等类别。每个类别都会有一个“查看全部”链接,因此用户可以查看所有早餐类别的帖子。在主页上,我将列出 5 个最近的帖子,当用户单击“查看全部”链接时,它会将它们链接到整个早餐类别。如果他们单击“查看全部”,我想要该类别中的所有早餐。其他类别也一样。 目前我的代码看起来像这样,但我坚持使用“查看全部”链接。我不知道如何将其链接到主要类别。
<?php
get_header();
?>
<!-- recipe -->
<section class="recipe-wrap">
<?php
/*
* Loop through Categories and Display Posts within
*/
$post_type = 'recipe';
$category_link = get_category_link($cat->cat_ID);
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) : ?>
<div class="recipe-category owl-carousel-slide">
<div class="row">
<h2><?php echo $term->name; ?><a href="#">see all</a></h2>
<div class="recipe-category-carousel owl-carousel owl-theme">
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => 10, //show all posts
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<div class="item recipe-box">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo(types_render_field('artwork', array('raw' => true) )); ?>">
<p><?php the_title(); ?></p>
</a>
</div>
<?php endwhile; endif; ?>
</div>
</section>
<?php endforeach;
endforeach; ?>
</div>
</div>
</div>
</section>
<!-- /recipe -->
<?php
get_footer();
?>
【问题讨论】:
-
这不是Minimal, Complete, and Verifiable example。请通过发布一个最小、完整且可验证的示例来帮助回答者和未来的读者。