GET_PAGE_BY_TITLE在与变量一起使用时不起作用

时间:2013-05-23 作者:fightstarr20

我的页面标题是This is my title, 如果我尝试像这样从标题中检索ID,则它不起作用:

$mytitle = \'This is my title\';
$mytitle2 = get_page_by_title( $mytitle, OBJECT, \'mycustompost\' );
print_r($mytitle2);
但如果我这样做,它确实会起作用:

$mytitle2 = get_page_by_title( \'This is my title\', OBJECT, \'mycustompost\' );
print_r($mytitle2);
有什么好处?做get_page_by_title 不接受变量?

2 个回复
SO网友:Xtremefaith

伙计,这对我来说也是一个恼人的问题,因为我有一个传递变量的函数,当测试时,它会显示一个值(with special characters, like "&". 如果我静态重置该值,它会工作,但在其他方面会出现相同的问题。我在变量上运行了html\\u entity\\u decode(),现在它工作得很好,所以我想如果它对某人有帮助的话,我会把它传下去。这是我的职责:

public function get_id_by_code($coupon){
    $test = get_page_by_title( html_entity_decode( $coupon ), OBJECT, \'coupon\' );
    return $test->ID;
}

SO网友:Ravinder Kumar

检索帖子/页面/自定义帖子类型id工作正常

$mytitle = \'This is my title\';
$mytitle2 = get_page_by_title( $mytitle, OBJECT, \'mycustompost\' );
print_r($mytitle2->ID);
Note:在WP config中启用WP\\U DEBUG。php查看所有错误和警告,并使用var\\u dump检查变量中的值。

For more info: read get_page_by_title() not returning anything

结束

相关推荐

Dynamic Menu drops pages?

I\'ve created a Dynamic Menu that highlights the selected page and colors for example the blog page \"red\" while the other links (home, about, contanct) remain white. My question is how can I make the Dynamic Menu work when say the blog goes to