从我的 Wordpress 插件内部更改 wp_title [英] Changing wp_title from inside my Wordpress Plugin

查看:35
本文介绍了从我的 Wordpress 插件内部更改 wp_title的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎完成了我的房地产经纪人的 wp 插件,我搜索了 404 等网站,我注意到我的财产详细信息页面被蜘蛛搜索,其中所有 45 个页面标题是:(详细信息 | 站点名称)(页面标题从通过查询字符串传递的 ID 动态显示)

nearly finished my wp plugin for an estate agent, I spidered the site for 404's etc, i noticed that my property details pages were spider'd in which all 45 page titles were : ( details | sitename ) (page titles are shown dynamicly from an ID being passed via querystring)

现在我已经修复了我的好网址,网址看起来像这样...

now I've got my nice urls fixed, urls look like this...

wpsite.com/details/20043/property+for+sale+in+this+area

其中...

  • propid = 20043
  • propname = property+for+sale+in+this+area

这两个都是用于重写网址的查询字符串值.

both of these are querystring values which are used to re-write the urls.

'query_vars' => array('propid', 'propname'),
'rules' => 
array( '(.+?)/([^/]+)/([^/]+)/?$' => 'index.php?pagename=$matches[1]&propid=$matches[2]&propname=$matches[3]' )
);

现在,当加载属性详细信息页面时,我正在尝试连接到 wordpress 过滤器wp_title 但这并不像我预期的那样工作..

Now when the property details page is loaded im trying to hook into the wordpress filter wp_title but this isnt working the way i expected..

这是我用来生成标题的代码

this is the code im using to generate the titles

function wp_myplugin_property_title()
{
    $wp_acquaint_id = get_option("wp_system_id");
    $propid = get_query_var('propid');
    if(isset($propid)){
        $seotitle = wp_myplugin_seo_title($propid);
    }else{
        $seotitle = "TEST Title";   
    }
    return $seotitle;   
}

if( is_page('details') ){
    add_filter('wp_title', wp_myplugin_property_title, 100);
}

该函数中使用的函数:wp_myplugin_seo_title($propid) 生成我想要使用的实际标题...

the function used within that function: wp_myplugin_seo_title($propid) generates the actual title i want to use...

function wp_myplugin_seo_title($propid)
{
    $wp_acquaint_id = get_option("wp_acquaint_id");
    $xml = wp_myplugin_get_property($propid);
    foreach($xml->PropertiesDataSet->Properties as $node) {
        include('xml_loop.php');

        if($bedrooms==0){ }else{ $seo_title.= $bedrooms." bedroom "; }

        $seo_title.= wp_myplugin_get_property_type($type_id)." "; //ie:flat
        $seo_title.= str_replace("(","",$street);
        $seo_title.= " ".$town." | ".get_bloginfo('name');
    }
    return $seo_title;
}

我发现在过滤器周围放置 if(is_page()) 后,页面标题不会改变,如果我删除 is_page,道具详细信息页面标题可以正常工作,但是!!!

Im finding that with the if(is_page()) in place around the filter the page title dosnt change and if i remove the is_page, the prop details page title works fine but!!!

在属性列表页面上,页面标题循环遍历该页面上的所有属性并生成大约 1000 个字符长的页面标题..!

while on the property listing page the page title is looping through all the properties on that page and producing a page title around 1000 char long..!

我一直在寻找更好的方法来解决这个问题,但任何帮助都会很棒..

I have been hunting around for a better way to deal with this but any help would be great..

干杯

马蒂

ps:目前正在运行 Yoast 的 wordpress seo!这就是为什么我在 add_filter 中将优先级设置为 100 只是为了看看它是否会覆盖标题..

ps: currently running wordpress seo by Yoast! thats why ive set the priority as 100 in the add_filter just to see if it would overwrite the title..

推荐答案

在 functions.php 中使用 is_page 不起作用,因为它在 wp 知道将要呈现的页面之前运行,甚至如果它是一个页面开始.将 is_page() 放在函数中,它应该可以工作.喜欢:

Using is_page in functions.php doesn't work, as it runs before wp knows what page it's going to render, or even if it's a page to begin with. Stick the is_page() inside the function, and it should work. Like:

function wp_myplugin_property_title()
{ 
  if( is_page('details') ){
    $wp_acquaint_id = get_option("wp_system_id");
    $propid = get_query_var('propid');
    if(isset($propid)){
        $seotitle = wp_myplugin_seo_title($propid);
    }else{
        $seotitle = "TEST Title";   
    }
    return $seotitle;   
  }
}


add_filter('wp_title', wp_myplugin_property_title, 100);

这篇关于从我的 Wordpress 插件内部更改 wp_title的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆