我正在创建一个自定义的 WordPress 仪表板菜单,我想在那里显示帖子列表。我尝试在函数内使用循环显示,但出现错误:
Parse error: syntax error, unexpected '}'
这是我的代码:
function _submenu_cb() {
$args = array ( 'post_type' => 'product', 'post_status' => 'pubish' );
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
echo '<h1>'.the_title().'</h1>';
}
}
如何解决这个问题?。是否可以通过在函数内循环帖子来在自定义仪表板菜单中显示帖子列表?
你应该修改代码的IDE与适当的棉短绒安装,因为它们将帮助像这样的诊断snytax错误。
如果您查看整个错误,它还会告诉您它位于哪一行。我只是将它粘贴到我的编辑器中,您可以直接看到错误所在。
向后看,您似乎没有关闭while
循环。注意:您正在使用的代码是替代语法为您的while
循环,但对你的标准的花括号语法if
的语句。您需要在结束语句的大括号endwhile;
之后echo
和之前添加一个if
:
function _submenu_cb() {
$args = array ( 'post_type' => 'product', 'post_status' => 'pubish' );
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
// Do Stuff Here, like output the title
endwhile;
}
}
请注意,您的echo
. the_title()
默认情况下实际输出标题。您实际上是在混淆the_title()
和[
get_the_title()`]( https://codex.wordpress.org/Function_Reference/get_the_title )。您会想要使用其中之一。任何一个:
echo '<h1>'. get_the_title() .'</h1>';
或删除回声并使用:
the_title( '<h1>', '</h1>' );
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句