我正在开发一个自定义登录页面插件,该插件按预期工作。但是,当我导航到主页时,出现错误
Notice: Object of class WP_Post could not be converted to int in C:\MAMP\htdocs\wp-includes\general-template.php on line 1018
注释掉部分代码后,我发现 foreach
循环导致了问题。需要一个解决方案。
// Set shortcode Tag
$login_shortcode = 'custom_login_form';
// Retrieve Login Page ID
$page_id = ! empty( $page_id ) ? $page_id : '';
$pages_args = array(
'post_type' => 'page'
);
$pages = get_pages( $pages_args );
foreach ( $pages as $page ) {
if ( ( $page->post_title == 'Login' && strpos( $page->post_content, $login_shortcode ) ) && $page->post_status == 'publish' ) {
$page_id = $page->ID;
}
}
最佳答案
经过我的实验,我发现只要对变量 $page 使用另一个名称,错误通知就消失了:
我不使用 $page ,而是使用 $page_list
$pages_args = array( 'post_type' => 'page' );
$page_list = get_pages( $pages_args );
// <a href="https://stackoverflow.com/questions/49141011/wordpress-notice-object-of-class-wp-post-could-not-be-converted-to-int-on-home" rel="noreferrer noopener nofollow">Wordpress: Notice: Object of class WP_Post could not be converted to int on homepage</a>
foreach ( $page_list as $individual_page ) {
if ( ( $individual_page->post_title == 'Login' && strpos( $individual_page->post_content, $login_shortcode ) ) && $individual_page->post_status == 'publish' ) {
$page_id = $individual_page->ID;
}
}
关于php - WordPress:注意:主页上的 WP_Post 类对象无法转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49141011/