Wordpress:应用过滤器自定义页面标题时回调函数出错
我有一些模板,需要根据用户选择自定义页面标题。我已按照 Codex 文档向“wp_title”标记添加了一个过滤器挂钩,但是当应用过滤器时,我收到一条警告,我想说的是关于回调函数声明中的参数的错误:
警告:缺少 buildPageTitle 的参数 4( )在 /Applications/XAMPP/xamppfiles/htdocs/.../blog/wp-content/themes/.../inc/my_functions.php 第 2 行
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();
?>
中我可以解决将 $brand var 声明为全局的问题在 buildPageTitle() 函数中,但我更喜欢将其作为参数传递,因为在其他模板中需要不同的变量
I have some templates where I need to customize the page title according to user selection. I have added a filter hook to 'wp_title' tag following Codex docs but when the filter is applied I'm receiving a warning, I'd say an error, regarding parameters in the callback function declaration:
Warning: Missing argument 4 for buildPageTitle() in /Applications/XAMPP/xamppfiles/htdocs/.../blog/wp-content/themes/.../inc/my_functions.php on line 2
my_functions.php
1 <?php
2 function buildPageTitle($sep, $echo, $seplocation, $brand) {
3 return $brand.$sep;
4 }
5 ...
Template
<?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();
?>
I can solve the problem declaring $brand var as global in the buildPageTitle() function but I prefer to pass it as a parameter as in other templates different vars will be needed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您对 WordPress 过滤器的工作原理有错误的认识。有一个函数
wp_title()
和一个过滤器wp_title
。您调用该函数,该函数会执行一些工作来创建标题,然后将其输出传递给过滤器,以便其他代码可以进一步自定义结果。函数和过滤器不一定使用相同的参数。对于函数
wp_title()
您可以传递分隔符、是否回显标题以及分隔符位置。过滤器可以获取wp_title()
创建的标题、分隔符和分隔符位置。设置过滤器挂钩时(通过add_filter()
),您可以指定需要的参数数量:1(默认)、2 或 3。您不能获得超过三个参数,因为wp_title()
不会向wp_title
过滤器传递超过三个参数。因此您不必自己调用
apply_filters()
。您调用wp_title()
(可能在您的header.php
模板文件中,此函数调用过滤器本身。如果您想访问您的
品牌
变量,您应该将其放入全局变量中,或者让您的buildPageTitle()
函数调用其他将返回它的函数,具体使用什么策略取决于您的具体情况。您想在那里使用模板和标题格式?I think you have a wrong idea how WordPress filters work. There is a function
wp_title()
and a filterwp_title
. You call the function, which does some work to create a title, and then passes its output to the filter, so other code can further customize the result.The function and the filter do not necessarily use the same arguments. For the function
wp_title()
you can pass the separator, whether to echo the title or not, and the separator location. The filter can get the title aswp_title()
has created it, the separator and the separator location. When setting up the filter hook (viaadd_filter()
) you specify how many arguments you need: 1 (the default), 2 or 3. You can't get more than three arguments becausewp_title()
does not pass more than three arguments to thewp_title
filter.So you should not have to call
apply_filters()
yourself. You callwp_title()
(probably in yourheader.php
template file, and this function calls the filter itself.If you want to access your
brand
variable you should either put it in a global variable or let yourbuildPageTitle()
function call some other function that will return it. What strategy to use depends on your situation. Can you tell more about the different templates and the title formats you want to use there?