Wordpress:应用过滤器自定义页面标题时回调函数出错

发布于 2024-11-13 06:18:18 字数 860 浏览 12 评论 0原文

我有一些模板,需要根据用户选择自定义页面标题。我已按照 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带刺的爱情 2024-11-20 06:18:18

我认为您对 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 filter wp_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 as wp_title() has created it, the separator and the separator location. When setting up the filter hook (via add_filter()) you specify how many arguments you need: 1 (the default), 2 or 3. You can't get more than three arguments because wp_title() does not pass more than three arguments to the wp_title filter.

So you should not have to call apply_filters() yourself. You call wp_title() (probably in your header.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 your buildPageTitle() 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文