在我的网站上,我通过应用以下代码来显示即将发生的事件,并且仅显示将来发生的事件。因此,每当有即将发生的事件时,它将显示在我的主页上。
但是,当没有事件时,标题Upcoming Events
会出现在整个框内。我试图在它为空时将其隐藏,但由于我对PHP的了解而无法使用,因此无法在下面找到解决方案。
我希望你们能在这方面帮助我
<div class="service-event widget-event widget">
<div clear="event-items clear">
<div class="event"><h4 class="title">Upcoming Events</h4></div>
<?php
$custom_terms = wp_get_post_terms($post->ID, 'event-category');
if( $custom_terms ){
$tax_query = array();
if( count( $custom_terms > 1 ) )
$tax_query['relation'] = 'OR' ;
foreach( $custom_terms as $custom_term ) {
$tax_query[] = array(
'taxonomy' => 'event-category',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
$args = array( 'post_type' => 'event',
'posts_per_page' => 5,
'post_status' => 'future',
'tax_query' => $tax_query );
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="item">
<div class="date">
<div class="text">
<strong><?php echo get_the_date('j') ?></strong><br>
<?php echo get_the_date('M') ?><br>
</div>
</div>
<div class="info">
<h2 class="title">
<a href="<?php bloginfo('url') ?>/event/<?php echo $post->post_name ?>" class="underline"><?php the_title() ?></a>
</h2>
<?php
$cats = get_the_terms(get_the_ID(), 'event-country');
$names = array();
foreach ($cats as $cat) {
if ($cat->parent) {
array_push($names, "<strong>$cat->name</strong>");
}
}
foreach ($cats as $cat) {
if (!$cat->parent) {
array_push($names, $cat->name);
}
}
?>
<div class="location">
<i class="icon icon-location-blue"></i>
<?php echo implode(', ', $names) ?>
</div>
</div>
</div>
<?php
endwhile;
}
wp_reset_query();
}?>
</div>
</div>
答案 0 :(得分:2)
按如下所示编辑代码,
<?php
$custom_terms = wp_get_post_terms($post->ID, 'event-category');
if ($custom_terms) {
$tax_query = array();
if (count($custom_terms > 1))
$tax_query['relation'] = 'OR';
foreach ($custom_terms as $custom_term) {
$tax_query[] = array(
'taxonomy' => 'event-category',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
$args = array('post_type' => 'event',
'posts_per_page' => 5,
'post_status' => 'future',
'tax_query' => $tax_query);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
?>
<div class="service-event widget-event widget">
<div clear="event-items clear">
<div class="event"><h4 class="title">Upcoming Events</h4></div>
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<div class="item">
<div class="date">
<div class="text">
<strong><?php echo get_the_date('j') ?></strong><br>
<?php echo get_the_date('M') ?><br>
</div>
</div>
<div class="info">
<h2 class="title">
<a href="<?php bloginfo('url') ?>/event/<?php echo $post->post_name ?>" class="underline"><?php the_title() ?></a>
</h2>
<?php
$cats = get_the_terms(get_the_ID(), 'event-country');
$names = array();
foreach ($cats as $cat) {
if ($cat->parent) {
array_push($names, "<strong>$cat->name</strong>");
}
}
foreach ($cats as $cat) {
if (!$cat->parent) {
array_push($names, $cat->name);
}
}
?>
<div class="location">
<i class="icon icon-location-blue"></i>
<?php echo implode(', ', $names) ?>
</div>
</div>
</div>
<?php
endwhile;
?>
</div>
</div>
<?php
}
wp_reset_query();
}
?>
希望这会有所帮助。