You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP实现:页面存在.client-quote类时隐藏指定<a>标签

解决方法:根据.client-quote类条件控制<a>标签显示

你的核心问题是判断逻辑和需求完全相反——原来的代码是「当quote字段存在时添加<a>包裹」,但你需要的是「当quote字段存在(即页面出现.client-quote类)时隐藏/不输出这个<a>标签」。下面给你两种针对性解决方案:

方案1:纯PHP逻辑修正(推荐,性能最优)

因为.client-quote元素完全由get_field('quote')的值生成,所以我们可以直接在PHP层面控制<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 // 只有当quote不存在时,才输出包裹的<a>标签 ?>
 <?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;">&ldquo; </span><?php the_field('quote'); ?><span style="color:<?php the_field('client_brand_colour'); ?>;font-weight:bold;"> &rdquo;</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 // 对应闭合标签的判断也取反 ?>
 <?php if( !get_field('quote') ): ?>
 </a>
 <?php endif; ?>
 </div>
 <?php endwhile; wp_reset_postdata(); endif; ?>
 </div>
 </div>

修改后,只有当quote字段为空(不会生成.client-quote类)时,才会用<a>标签包裹整个卡片;当quote存在时,直接不输出这个<a>标签,完全匹配你的需求。

方案2:前端JS控制(适合特殊场景)

如果因为某些原因无法修改PHP逻辑(比如.client-quote可能来自其他动态生成的内容),可以用JavaScript在页面加载后判断并处理:

// 等待DOM完全加载完成
document.addEventListener('DOMContentLoaded', function() {
  // 遍历所有包含.client-quote的卡片元素
  document.querySelectorAll('.card .client-quote').forEach(function(quoteElement) {
    // 找到包裹当前卡片的父级<a>标签
    const parentLink = quoteElement.closest('.card').parentElement;
    if (parentLink.tagName === 'A') {
      // 隐藏标签(或用parentLink.remove()直接移除)
      parentLink.style.display = 'none';
    }
  });
});

把这段代码加到主题的自定义脚本区域,或者页面底部的<script>标签中即可生效。

为什么原来的代码没生效?

你原来的判断逻辑是「有quote就添加<a>」,但你实际需求是「有quote就隐藏<a>」,逻辑完全相反,调整判断条件就能直接解决问题啦。

内容的提问来源于stack exchange,提问作者Shaun Taylor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:55:29