php - 如果没有内容,则隐藏整个 div
问题描述
在我的网站中,我通过应用下面的代码来显示即将发生的事件,它只显示未来的事件。因此,每当有即将发生的事件时,它都会显示在我的主页上。
但是,当没有事件时,标题会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>
解决方案
编辑您的代码如下,
<?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();
}
?>
希望这可以帮助。
推荐阅读
- git - .gitignore 不适用于 Visual Studio 2019
- pandas - 熊猫 groupby 并添加计数
- windows - C驱动器可用空间下降非常快,没有明显原因
- java - 用于存储应用程序设置的 Java/JPA 类
- reactjs - React 事件触发多次
- android - 如何有意图地在资产文件夹中共享来自html的纯格式文本
- asp.net-core - .NET Core 3.1 是否向后兼容?
- javascript - Javascript CSV 标头文本格式
- python - 什么时候必须使用 super() 来调用超类方法?
- php - 查询中的多项选择不返回对象