首页 > 解决方案 > PHP - 如何在 WP 上随机显示一个类别的帖子

问题描述

我需要一些关于 wordpress 网站主页的帮助。

该网站的主页显示所有按类别排序的帖子。我想按日期显示除“À découvrir”类别(类别名称:decouvrir)之外的所有帖子。

这是我页面的完整代码:

   <?php get_header(); ?>
	
	<div class="container">
		
		<div id="content">

			<?php
				$categories = get_categories( array(
				    'orderby' => 'slug',
				    'order'   => 'ASC'
				) );
			?>

			<div class="col-md-12">
				<h1 id="h1home">Visitez <strong>St-Cyprien,</strong><br> certainement le meilleur quartier de Toulouse.</h1>
				<h2 id="pres_home">Qu’ils soient commerçants ou habitants, à Saint-Cyprien, il n’y a que des voisins.<br/>
Ce quartier populaire a l’âme d’un faubourg et l’étoffe d’un village. Loin des guerres de clochers, il y règne comme une ambiance de marché. On y vit ou on y vient, on y sort ou on y dort car l’attractivité de Saint-Cyprien n’a d’égal que sa simplicité.<br/>
En d’autres termes, et sans prétention ni chauvinisme, il s’agit certainement du meilleur quartier de Toulouse.</h2>
			</div>
		
			<?php if(get_theme_mod( 'sp_featured_slider' ) == true) : ?>
				<?php get_template_part('inc/featured/featured'); ?>
			<?php endif; ?>
		
			
		
			<div id="main" <?php if(get_theme_mod('sp_sidebar_homepage') == true) : ?>class="fullwidth"<?php endif; ?>>
				
				<ul class="sp-grid">
						<?php

							$tab_num = array(
								0 => array(
									'num' => 3,
									'template' => 'grid-trois'
								),
								1 => array(
									'num' => 4,
									'template' => 'grid-quatre'
								),
								2 => array(
									'num' => 6,
									'template' => 'grid-six'
								)
							);

							$i = 0;

							foreach( $categories as $category ) {


								if( ($category->count) >= 2 && $category->slug !== 'boutiques'){

								 $category_link = sprintf( 
							        '<a href="%1$s" alt="%2$s" class="link_tous">%3$s</a>',
							        esc_url( get_category_link( $category->term_id ) ),
							        esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
							        esc_html( 'Voir tous' )
							    );
						?>

						<div class="col-md-12">
							<h2 class="cat-name"><?php echo $category->name ?></h2>
							<?php echo sprintf( esc_html__( '%s', 'Voir tous' ), $category_link ); ?>
						</div>
							<ul class="sp-grid">


								<?php
								$query = new WP_Query( array( 'posts_per_page' => $tab_num[$i][num], 'category_name' => $category->slug ) );
								if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 
									
									get_template_part('content', $tab_num[$i][template]);
								
								endwhile;
								endif;
								?>
							</ul>
							
						<?php
							
								if($i < 2){
									$i++;
								}else{
									$i = 0;
								}
							}
						}
						?>

				<!-- <div class="col-md-12"><h2>Actus</h2></div>
				<?php
				$query = new WP_Query( array( 'posts_per_page' => 3, 'category_name' => 'actus' ) );
				if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 
					
					get_template_part('content', 'grid-trois');
				
				endwhile;
				endif;
				?>
				
				<div class="col-md-12"><h2>À découvrir</h2></div>
				<?php
				$query = new WP_Query( array( 'posts_per_page' => 4, 'category_name' => 'decouvrir' ) );
				if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 
					
					get_template_part('content', 'grid-quatre');
				
				endwhile;
				endif;
				?> -->

				<!-- C'est bientôt -->
					<!-- <div class="col-md-12"><h2>C'est bientôt</h2></div>
					<?php
					$query = new WP_Query( array( 'posts_per_page' => 3, 'category_name' => 'bientot' ) );
					if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 
						
						get_template_part('content', 'grid-trois');
					
					endwhile;
					endif;
					?> -->

				<!-- Patrimoine -->
					<!-- <div class="col-md-12"><h2>Patrimoine</h2></div>
					<ul class="sp-grid">
						<?php
						$query = new WP_Query( array( 'posts_per_page' => 6, 'category_name' => 'patrimoine' ) );
						if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 
							$i = 1;
							get_template_part('content', 'grid-six');
						
						endwhile;
						endif;
						?>
					</ul> -->
				<!-- FIN Patrimoine -->

				<!-- Restau -->
					<!-- <div class="col-md-12"><h2>Restaurants</h2></div>
					<?php
					$query = new WP_Query( array( 'posts_per_page' => 3, 'category_name' => 'restos' ) );
					if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 
						
						get_template_part('content', 'grid-trois');
					
					endwhile;
					endif;
					?> -->
				<!-- Fin restau -->

				<!-- Sorties -->
				<!-- 	<div class="col-md-12"><h2>Sorties</h2></div>
					<ul class="sp-grid">
						<?php
						$query = new WP_Query( array( 'posts_per_page' => 4, 'category_name' => 'sorties' ) );
						if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 
							
							get_template_part('content', 'grid-quatre');
						
						endwhile;
						endif;
						?>
					</ul> -->
				<!-- Fin Sorties -->
				

				<?php if(get_theme_mod( 'sp_promo' ) == true) : ?>
							<?php get_template_part('inc/promo/promo'); ?>
						<?php endif; ?>
				
				<?php if(get_theme_mod('sp_home_layout') == 'grid' || get_theme_mod('sp_home_layout') == 'full_grid') : ?></ul><?php endif; ?>
				
					<!-- <?php solopine_pagination(); ?> -->
				



			
			</div>

<?php if(get_theme_mod('sp_sidebar_homepage')) : else : ?><?php get_sidebar(); ?><?php endif; ?>

<?php get_footer(); ?>

我尝试在第 37 行和第 104 行使用 'orderby' => 'rand' 但它没有做任何事情......我也在第 70 行尝试过,但它随机显示所有帖子,而不仅仅是一个类别...

如果你能看看并告诉我该怎么做,那就太棒了!非常感谢你的帮助!

标签: phpwordpress

解决方案


推荐阅读