【问题标题】:WordPress: How to loop post type to custom dashboard menu?WordPress:如何将帖子类型循环到自定义仪表板菜单?
【发布时间】:2019-02-01 07:41:31
【问题描述】:

我正在创建一个自定义 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>';
    }
}

如何解决这个问题?是否可以通过在函数内循环帖子来在自定义仪表板菜单中显示帖子列表?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您应该在安装了适当的linters 的情况下编辑IDE 中的代码,因为它们将有助于诊断此类语法错误。

    如果您查看整个错误,它也会告诉您它在哪一行。我刚刚将它粘贴到我的编辑器中,您可以看到错误在哪里。

    向后看,您似乎没有关闭while 循环。请注意,您使用的代码是while 循环的Alternative Syntax,但if 语句的标准大括号语法。您需要在您的 echo 之后和结束您的 if 语句的大括号之前添加一个 endwhile;

    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 中还有一个特定于 WordPress 的错误。 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>' );
    

    【讨论】:

      猜你喜欢
      • 2013-12-10
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多