【问题标题】:How do I display all posts based on category?如何根据类别显示所有帖子?
【发布时间】: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();

?>

【问题讨论】:

标签: php html wordpress


【解决方案1】:

试试下面的代码

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>';

}

【讨论】:

  • 现在它显示 2 个术语名称(我使用“查看全部”)。我想为每个类别显示 1 个术语名称(查看全部)。我该怎么做?
  • 我需要包含我提供的代码对吗?你也可以给我看看吗?包括我的代码加上你的代码好吗? :)
【解决方案2】:
<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>

=> 使用这个方法可以得到所有的帖子

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 2019-04-29
    • 2016-06-13
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多