如何根据类别显示所有帖子?

时间:2017-06-02 03:48:13

标签: php html wordpress

例如,我将有4个类别。对于每个类别,我将显示5个最近的帖子。我会有早餐,甜点,午餐和咸味食品等类别。每个类别都会有一个“查看全部”链接,因此用户可以查看所有早餐类别帖子。在主页面上,我将列出5个最近的帖子,当用户点击“查看全部”链接时,它会将它们链接到整个早餐类别。如果他们点击“查看全部”,我想要该类别的所有早餐。其他类别也是如此。 目前我的代码看起来像这样,但我坚持使用“see all”链接。我不知道如何将其链接到主要类别。

<?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();

?>

2 个答案:

答案 0 :(得分: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>';

}

答案 1 :(得分: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>

=&GT;使用这种方法可以获得所有帖子