以编程方式设置page_on_front


9

我试图以编程方式将'page_on_front'选项设置为从get_page_by_title命令检索的id值...

$homepage = get_page_by_title( 'Front Page' );
update_option('page_on_front', $homepage);
update_option('show_on_front', 'page');

这不起作用,有人可以帮忙吗?

Answers:


21

get_page_by_title() 返回一个对象。

使用$homepage->ID

您还应该检查您是否确实获得了可用的返回值:

$homepage = get_page_by_title( 'Front Page' );

if ( $homepage )
{
    update_option( 'page_on_front', $homepage->ID );
    update_option( 'show_on_front', 'page' );
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.