我创建了一个WP插件,它使用查询字符串根据访问者选择的内容拉入页面数据。显然,这“模拟”了其他页面,但页面标题与WP Admin中设置的标题没有变化。
我一直想勾搭上wp_title
动态更改标题标签,但无法使此标签正常工作。
以下功能工作:
public function custom_title($title) {
return \'new title\';
}
add_filter( \'wp_title\', array($this, \'custom_title\'), 20 );
// changes <title> to \'new title\'
只要我尝试向它传递一个变量,它就会失败。
public function custom_title($title, $new_title) {
return $new_title;
}
WordPress抱怨它缺少第二个参数,我想这是有意义的,因为函数是在页面加载时调用的。。。我希望我能像
$this->custom_title($title, \'new title);
在我的插件中,但这看起来不可能?
我把这个贴在这里是因为我认为这是一个PHP类的一般问题。
我是否可以全局化返回的变量,例如,我想从另一个函数中的查询返回“title”列,例如$query->title
当函数运行时,它将从数据库返回数据
public function view_content()
{
$query = $this->db->get_row(\'SELECT title FROM ...\');
$query->title;
}
我现在需要$query->title设置为页面标题。
public function custom_title()
{
if($query->title)
{
$new_title = $query->title;
}
}
我的班级精简版:
class FB_Events {
private static $_instance = null;
private $wpdb;
public $_token;
public $file;
public $dir;
public $assets_dir;
public $assets_url;
private $message;
public function __construct($file = \'\', $version = \'1.0.0\')
{
global $wpdb;
$this->db = $wpdb;
$this->fb_events_table = $this->db->prefix . \'fb_events\';
$this->_version = $version;
$this->_token = \'fb_events\';
$this->_db_version = \'1.0.0\';
$this->file = $file;
$this->dir = dirname($this->file);
$this->assets_dir = trailingslashit($this->dir) . \'assets\';
$this->assets_url = esc_url(trailingslashit(plugins_url(\'/assets/\', $this->file)));
register_activation_hook($this->file, array($this, \'install\'));
add_action(\'admin_menu\', array($this, \'admin_menu\'));
add_action(\'init\', array($this, \'rewrite_rules\'), 10, 0);
add_filter(\'query_vars\', array($this, \'event_query_var\'), 0, 1);
// Add shortcodes for html output
add_shortcode(\'fb_events_form\', array($this, \'add_event_public\'));
add_shortcode(\'fb_events_display\', array($this, \'view_events\'));
add_shortcode(\'fb_events_featured\', array($this, \'featured_events\'));
}
/**
* Register rewrite rules
* @access public
* @since 1.0.0
* @return void
*/
public function rewrite_rules()
{
add_rewrite_rule(\'^events/add/?\', \'index.php?pagename=events/add\', \'top\');
add_rewrite_rule(\'^events/([^/]*)/?\',\'index.php?pagename=events&event=$matches[1]\', \'top\');
}
/**
* Register event query string
* @access public
* @since 1.0.0
* @return void
*/
public function event_query_var($vars)
{
$vars[] = \'event\';
return $vars;
}
/**
* View events from public website
* @access public
* @since 1.0.0
* @return void
*/
public function view_events()
{
$event = get_query_var(\'event\');
if(isset($event) && $event != \'\')
{
if($event != \'add\')
{
$event = $this->db->get_row(
$this->db->prepare("
SELECT e.*, u.display_name AS username
FROM {$this->fb_events_table} AS e
LEFT JOIN {$this->db->prefix}users AS u
ON e.user_id = u.id
WHERE e.slug = %s
", $event)
);
if($event > 0)
{
/**
* *********************************
* NEED TO SET <title> to $event->name
* *********************************
*/
ob_start();
if(isset($event->active) && $event->active == 0)
{
//error
}
else
{
//show the event
}
}
else
{
// event does not exist error
}
}
}
}
/**
* Main FB_Events Instance
* @since 1.0.0
* @static
* @return Main FB_Events instance
*/
public static function instance($file = \'\', $version = \'1.0.0\') {
if(is_null(self::$_instance))
{
self::$_instance = new self($file, $version);
}
return self::$_instance;
}
}