0

I'm creating a custom WordPress dashboard menu and I want to display post list there. I try to display with loop inside a function, but getting error:

Parse error: syntax error, unexpected '}'

Here is my code:

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>';
    }
}

How to fix this?. Is it possible to display the post list in custom dashboard menu by looping the post inside a function?

Kashif Rafique
  • 1,273
  • 1
  • 12
  • 25
edoha
  • 33
  • 1
  • 6

1 Answers1

0

You should be editing code in an IDE with the appropriate linters installed, as they will help diagnose snytax errors like these.

If you take a look at the entire error, it will tell you what line it's on as well. I just pasted it into my editor and you can see right where the error is.

Screenshot of IDE

Looking backwards, it appears you didn't close your while loop. Note the code you're using is the Alternative Syntax for your while loop, but standard curly bracket syntax for your if statement. You need to add an endwhile; after your echo and before the curly bracket closing your if statement:

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;
    }
}

Note that you also have a WordPress specific error in your echo. the_title() by default actually outputs the title. You're effectively conflating the_title() and [get_the_title()`](https://codex.wordpress.org/Function_Reference/get_the_title). You'll want to use one or the other. Either:

echo '<h1>'. get_the_title() .'</h1>';

or drop the echo and use:

the_title( '<h1>', '</h1>' );
Xhynk
  • 13,513
  • 8
  • 32
  • 69