我有一些模板需要根据用户选择自定义页面标题。我在 Codex 文档之后向“wp_title”标签添加了一个过滤器挂钩,但是当应用过滤器时,我收到一个警告,我会说一个关于回调函数声明中的参数的错误:
警告:缺少第 2 行 /Applications/XAMPP/xamppfiles/htdocs/.../blog/wp-content/themes/.../inc/my_functions.php 中 buildPageTitle() 的参数 4
my_functions.php
1 <?php
2 function buildPageTitle($sep, $echo, $seplocation, $brand) {
3 return $brand.$sep;
4 }
5 ...
模板
<?php
/*
Template Name: By brand-countries
*/
$brandLabel = get_query_var('brand');
require_once('inc/dbConn.php');
require_once('inc/get_brand_data.php');
require_once('inc/my_functions.php');
add_filter('wp_title', 'buildPageTitle', 10, 4);
apply_filters('wp_title', $sep, false, $seplocation, $brand);
get_header();
?>
我可以解决在 buildPageTitle() 函数中将 $brand var 声明为全局变量的问题,但我更喜欢将它作为参数传递,因为在其他模板中需要不同的变量