我有一个 <a></a>
我页面上的标记围绕着我从高级自定义字段中提取的一些内容。
一切都很好,但我希望链接是有条件的。
我希望评论是:如果 .client-quote 类在页面上,隐藏 a 标签。
这是整个循环代码:
<!-- Main Loop =========================================== -->
<div class="container blog-card-container">
<div class="card-columns">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if( get_field('quote') ): ?>
<a href="<?php the_permalink(); ?>">
<?php endif; ?>
<div class="card">
<!-- Image if loop =========================================== -->
<?php if ( in_category('14') ) : ?>
<div class="client-header-logo-card" style="background-color: <?php the_field('client_brand_colour'); ?>;">
<?php
$image = get_field('client_logo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
<?php else: ?>
<div class="blog-thumb-container">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
</div>
<?php endif ?>
<!-- Meta Data if loop =========================================== -->
<div class="blog-clients-card-block">
<?php if ( in_category('14') ) : ?>
<div class="client-text-block">
<p class="blog-cat-label"><?php the_category(', '); ?></p>
<h2><?php the_title(); ?></h2>
<?php if( get_field('quote') ): ?><p class="client-quote"><span style="color:<?php the_field('client_brand_colour'); ?>; font-weight:bold;">“ </span><?php the_field('quote'); ?><span style="color:<?php the_field('client_brand_colour'); ?>;font-weight:bold;"> ”</span></p><?php endif; ?>
<?php if( get_field('quote_name') ): ?><p class="client-name" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_name'); ?></p><?php endif; ?>
<?php if( get_field('quote_position') ): ?><p class="client-position" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_position'); ?></p><?php endif; ?>
<?php if( get_field('button_text') ): ?>
<a class="btn btn-sm btn-client-archive" href="<?php the_permalink(); ?>" style="background-color:<?php the_field('client_brand_colour'); ?>;" role="button"><?php the_field('button_text'); ?></a>
<?php endif; ?>
<?php if( get_field('video_url') ): ?>
<div class="embed-container">
<?php the_field('video_url'); ?>
</div>
<?php endif; ?>
</div>
<?php else: ?>
<p class="blog-cat-label"><?php the_category(', '); ?></p>
<h2 class="blog-card-title"><?php the_title(); ?></h2>
<p class="card-text"><?php the_excerpt(__('(more…)')); ?></p>
<p><strong><?php the_author(); ?></strong> | <?php the_date(); ?> </p>
<?php endif ?>
</div>
<?php if( get_field('quote') ): ?>
</a>
<?php endif; ?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>
我试过像这样在链接周围添加 if 标签:
<?php if( get_field('quote') ): ?>
<a href="<?php the_permalink(); ?>">
<?php endif; ?>
CODE AND CONTENT HERE
<?php if( get_field('quote') ): ?>
</a>
<?php endif; ?>
但这似乎不起作用,我想是因为它可能在其中的循环之前运行?所以它让我有点困惑......也许我需要一个函数在页面加载后运行或类似的东西?我不完全确定。
非常感谢任何帮助,因为它让我发疯!
最佳答案
执行 php 检查以查看它是否存在,并将结果放入变量 ($yourTag)。在那之后,在你想要的任何地方回应它。
<?php
if(get_field('quote') == ''){
$yourTag = "<a href=\"".get_the_permalink()."\">" ;
} else {
$yourTag = "";
}
?>
然后像这样将它放在 HTML 中的任意位置
<div> <?php echo $yourTag; ?> </div>
关于javascript - PHP - 如果 div 类出现在页面上 - 隐藏单独的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48502280/