【发布时间】:2017-11-04 22:21:07
【问题描述】:
我正在做类似的事情。我有一个食谱博客,我会有不同的类别,如早餐、甜点、午餐等……但我不知道如何将它们链接到类别页面。例如,如果用户点击早餐类别的“查看全部”,它将显示早餐类别中的所有早餐帖子。这意味着它将链接到主要的早餐类别页面。对于每个类别,我将显示 10 个最近的帖子,因此用户不会看到所有帖子。因此,如果他们单击“查看全部”链接,他们可以选择查看所有帖子。它会将它们链接到包含所有早餐帖子的主类别页面。当然如果他们点击甜点“查看全部”链接,他们会链接到甜点类别页面,他们可以看到所有甜点帖子。
这是我当前的代码: (但是我应该为“查看全部”链接添加什么 php 代码?如果您查看我的“查看全部”标签,现在是 #。它没有链接到某个地方,但是我如何使其动态链接到类别页面。例如,如果我在甜点部分,如果我点击“查看全部”,它将链接到甜点类别。)
<?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();
?>
请查看我提供的示例,以便您知道我在说什么。 我附上了一些图片和链接。
这是链接的示例。
这是博客链接
【问题讨论】:
标签: php html wordpress categories advanced-custom-fields