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

WordPress自定义函数失效排查:自定义字段与Ad Inserter广告无法显示

排查Astra主题自定义函数无法显示自定义字段和广告的问题

我希望在文章元数据上方显示自定义字段above_meta的值,以及通过Ad Inserter插件插入的广告,但当前自定义函数无法实现该效果,请帮忙排查语法错误或其他问题。

相关代码:

/**
 * Single Post Title & Meta Order
 */
if ( ! function_exists( 'astra_get_single_post_title_meta' ) ) {

    function astra_get_single_post_title_meta() {

        // Single Post Title and Single Post Meta.
        do_action( 'astra_single_post_order_before' );

        ?>
        <div class="ast-single-post-order">
            <?php

            do_action( 'astra_single_post_title_before' );

            the_title(
                '<h1 class="entry-title" ' . astra_attr(
                    'article-title-blog-single',
                    array(
                        'class' => '',
                    )
                ) . '>',
                '</h1>'
            ); 

            do_action( 'astra_single_post_title_after' );

            if ( get_field( 'above_meta' ) ) {
                ?>
                <div class="astra-above-meta-single-post">
                    <?php the_field( 'above_meta' ); ?>
                </div>
                <?php
            }

            do_action( 'astra_single_post_meta_before' );

            // Display ad above post meta
            if ( function_exists( 'adinserter' ) ) {
                echo adinserter( 15 );
            }

            // Display post meta
            astra_post_meta();

            do_action( 'astra_single_post_meta_after' );

            ?>
        </div>
        <?php

        do_action( 'astra_single_post_order_after' );
    }
}

排查方向及解决建议

1. 函数未生效:覆盖逻辑问题

你的代码用if ( ! function_exists( 'astra_get_single_post_title_meta' ) )包裹,这意味着只有当Astra主题原本没有定义这个函数时,自定义函数才会被加载。但Astra主题本身自带该函数,所以你的自定义代码根本不会执行。

解决方法:
改用Astra提供的动作钩子插入内容(推荐,避免主题更新导致失效):

  • 插入自定义字段:
    add_action( 'astra_single_post_meta_before', 'display_above_meta_field' );
    function display_above_meta_field() {
        // 先确认ACF函数存在且字段有值
        if ( function_exists( 'get_field' ) && get_field( 'above_meta' ) ) {
            ?>
            <div class="astra-above-meta-single-post">
                <?php the_field( 'above_meta' ); ?>
            </div>
            <?php
        }
    }
    
  • 插入广告:
    // 调整优先级(15)确保在自定义字段之后显示
    add_action( 'astra_single_post_meta_before', 'display_ad_above_meta', 15 );
    function display_ad_above_meta() {
        if ( function_exists( 'adinserter' ) ) {
            echo adinserter( 15 );
        }
    }
    

2. 自定义字段above_meta不显示

  • 确认已安装并激活**Advanced Custom Fields(ACF)**插件,get_field()和the_field()是ACF专属函数,未安装则无法生效。
  • 检查字段名是否完全匹配(大小写敏感),且当前文章确实设置了该字段的值。
  • 若使用自定义文章类型,需确认ACF字段组已关联该文章类型。

3. Ad Inserter广告不显示

  • 确认插件已激活,且广告单元ID15存在并配置了有效广告内容(注意Ad Inserter的单元编号是从1开始的序号,不是后台的ID)。
  • 检查插件设置,是否有针对当前文章/分类的排除规则,导致广告被隐藏。

内容的提问来源于stack exchange,提问作者John Caby

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 09:55:12