2017-02-03 18 views
0

我试图筛选出一个柱式冠军,我得到这个开捕致命错误:类stdClass的客体不能转换为字符串线125 ......在WordPress

Catchable fatal error: Object of class stdClass could not be converted to string in line 125

这里是我使用的代码...

add_filter('wpseo_title', 'vehicle_listing_title', 10, 1); 
function vehicle_listing_title($title) { 
    global $post; 
    if (get_post_type() == 'vehicles'){ 

     $model = get_queried_object('vehicle_model'); 
     $location = get_queried_object('vehicle_location'); 
     $title = $model . "used cars for sale in" . $location .'on'. get_bloginfo('name'); <---- this is line 125 
    } 

    return $title; 
    } 
+1

妈的,顾名思义是'get_queried_object'必须返回一个对象,你刚刚用绳子串连,并没有实现'__toString'我想。试着'var_dump($ model,$ location)'看看。 – Chay22

回答

0

get_queried_object()不允许参数。

试试这个:

$post = get_queried_object(); 
$location = $post->post_title; 

也许你vehicle_modelvehicle_location是元字段,然后你必须你使用延伸安置自己的领域,如前进,定做任何插件使用get_post_meta()功能

场?

编辑:看看评论,你使用ACF插件。所以,你的代码应该是:

add_filter('wpseo_title', 'vehicle_listing_title', 10, 1); 
function vehicle_listing_title($title) { 
    if (get_post_type() == 'vehicles'){ 

     $model = get_field('vehicle_model'); 
     $location = get_field('vehicle_location'); 
     $title = $model . "used cars for sale in" . $location .'on'. get_bloginfo('name'); 
    } 

    return $title; 
} 
+0

您的建议只返回没有帖子标题的“二手车出售在sitename”。是的,我正在使用ACF插件。 –

+0

然后你可以很容易地通过'get_field('vehicle_model',$ postId)获取值;'看看文档:https://www.advancedcustomfields.com/resources/get_field/ – Sysix

+0

好吧,我已经尝试过,它也返回没有帖子标题的过滤标题。我也尝试过'$ vehicle_location = get_queried_object_id('vehicle_location')'它会在760onneename'上返回'760辆旧车出售,这似乎更接近所需的结果。你有什么想法如何改变它从一个ID到一个名字? –

0

根据$位置对象的内容,你可能会使用print_r()就可以进入这个称号。

 $title = print_r($model, true) . "used cars for sale in" . print_r($location, true) .'on'. get_bloginfo('name'); <---- this is line 125 

true告诉函数返回结果而不是回显它。

如果你的对象有需要得到的数据,从内,那么你可以做这样的事情的内部结构:

$title = $model[0] . "used cars for sale in" . $location[0] .'on'. get_bloginfo('name'); <---- this is line 125 

一个var_dump($location); var_dump($model);将输出对象的完整内容,所以你可以看到它们的结构。只需将[0]中的'0'替换为所需项目的密钥(或多个密钥IE $model[0][0][0])即可。

此外,我看到你已经在那里有帖子对象($post)。也许你可以窥视那个物体,看看模型和位置是否在那里? var_dump($post);

+0

好吧,我试了一下,它仍然返回相同。这是我用'print_r()''stdClass([term_id] => 760 [name] =>伦敦[slug] =>伦敦[term_group] => 0 [term_taxonomy_id] => 760 [taxonomy] => vehicle_location [term] => [parent] => 19 [count] => 0 [filter] => raw)出售instdClass对象([term_id] => 760 [name] => London [slug] => London [ 'term_taxonomy_id] => 760 [分类学] => vehicle_location [description] => [parent] => 19 [count] => 0 [filter] => raw) –

相关问题