【问题标题】:Missing argument 2 for a custom function自定义函数缺少参数 2
【发布时间】:2016-06-12 22:26:05
【问题描述】:

所以我试图在保存更改后将原始 post_title 替换为同一帖子的自定义字段。但是,我在帖子页面上收到以下错误:

警告:第 113 行 $PATH/public_html/wp-content/themes/$THEME/functions.php 中 wpse33385_filter_title() 缺少参数 2

// replaces the original post_title with the value of pac-short-title

add_filter( 'the_title', 'wpse33385_filter_title', 10, 2);

function wpse33385_filter_title( $title, $post_id )
{
    if( $new_title = types_get_field_meta_value( 'pac-short-title', $post_id ) )
    {
        return $new_title;
    }
    return $title;
}

我很困惑,因为我在 add_filter 中定义了许多参数?

【问题讨论】:

  • 您的相关apply_filters() 电话在哪里?
  • @Tristan 你是什么意思?该功能有效,但在前端显示警告。

标签: php wordpress


【解决方案1】:

此警告可能是由于在某些版本的 WordPress 中使用此过滤器的某些情况下未设置帖子 ID。

解决办法是给post id设置一个默认值。

function wpse33385_filter_title( $title, $post_id = null )

【讨论】:

  • 所有 PHP 版本都支持这个吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多