$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Parse error: syntax error, unexpected '}' in|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Parse error: syntax error, unexpected '}' in

matteradmin17PV0评论

I'm fairly new at this, so please excuse my ignorance.

I'm trying to order my posts in the site's categories with a button, 'ascending' / 'descending'.

I'm trying to use 'sortit' function because that's the only source I could find online; been working on this all the past week.

So what I did here is;

                                        <li class="dropdown">
                                        <a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#" role="button" aria-expanded="false">Sırala</a>
                                        <ul class="dropdown-menu">
                                        <li class="nav-item"><a href="?sortby=asc" class="nav-link dropdown-item" type="button" role="tab">Yeniden Eskiye</a></li>
                                        <li class="nav-item"><a href="?sortby=desc" class="nav-link dropdown-item" type="button" role="tab">Eskiden Yeniye</a></li>
                                        </ul>
                                        </li>

Add a code to the menu, so the URL will be '?sortby=asc' or '?sortby=desc'

(Web site is in Turkish so ignore the texts)

The next step was adding a function.

    function sortIt($sortType)
    {
    global $wp_query;
    $cat_ID = get_query_var('cat');

    if (strcmp($sortType, 'ASC') )
    {
        $newQuery = new WP_Query( array(
        'orderby' => 'date' ,
        'order'   => 'ASC',
        'cat' => $cat_ID,
        'posts_per_page' => '10') );
    }

    if (strcmp($sortType, 'DESC') )
    {
        $newQuery = new WP_Query( array(
        'orderby' => 'date' ,
        'order'   => 'DESC',
        'cat' => $cat_ID,
        'posts_per_page' => '10') );
    }

    return $newQuery;
    }

Next step is displaying them when the button is clicked, but it will crash into each other with the current order so and if-else must be created here. What I did is

<?php if (is_page('echo esc_url( wp_get_current_url()/?sortby=asc')) {
                          if ( $newQuery->have_posts() ) : while ( $newQuery->have_posts() ) : $newQuery->the_post();
                          } else { if (is_page('echo esc_url( wp_get_current_url()/?sortby=desc')) {
                          if ( $newQuery->have_posts() ) : while ( $newQuery->have_posts() ) : $newQuery->the_post();
                          } } else { if (have_posts()) : while (have_posts()) : the_post();
                          } ?>

But now I'm getting an error, "Parse error: syntax error, unexpected '}' in"

Any ideas? Any help?

Thank you.

I'm fairly new at this, so please excuse my ignorance.

I'm trying to order my posts in the site's categories with a button, 'ascending' / 'descending'.

I'm trying to use 'sortit' function because that's the only source I could find online; been working on this all the past week.

So what I did here is;

                                        <li class="dropdown">
                                        <a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#" role="button" aria-expanded="false">Sırala</a>
                                        <ul class="dropdown-menu">
                                        <li class="nav-item"><a href="?sortby=asc" class="nav-link dropdown-item" type="button" role="tab">Yeniden Eskiye</a></li>
                                        <li class="nav-item"><a href="?sortby=desc" class="nav-link dropdown-item" type="button" role="tab">Eskiden Yeniye</a></li>
                                        </ul>
                                        </li>

Add a code to the menu, so the URL will be '?sortby=asc' or '?sortby=desc'

(Web site is in Turkish so ignore the texts)

The next step was adding a function.

    function sortIt($sortType)
    {
    global $wp_query;
    $cat_ID = get_query_var('cat');

    if (strcmp($sortType, 'ASC') )
    {
        $newQuery = new WP_Query( array(
        'orderby' => 'date' ,
        'order'   => 'ASC',
        'cat' => $cat_ID,
        'posts_per_page' => '10') );
    }

    if (strcmp($sortType, 'DESC') )
    {
        $newQuery = new WP_Query( array(
        'orderby' => 'date' ,
        'order'   => 'DESC',
        'cat' => $cat_ID,
        'posts_per_page' => '10') );
    }

    return $newQuery;
    }

Next step is displaying them when the button is clicked, but it will crash into each other with the current order so and if-else must be created here. What I did is

<?php if (is_page('echo esc_url( wp_get_current_url()/?sortby=asc')) {
                          if ( $newQuery->have_posts() ) : while ( $newQuery->have_posts() ) : $newQuery->the_post();
                          } else { if (is_page('echo esc_url( wp_get_current_url()/?sortby=desc')) {
                          if ( $newQuery->have_posts() ) : while ( $newQuery->have_posts() ) : $newQuery->the_post();
                          } } else { if (have_posts()) : while (have_posts()) : the_post();
                          } ?>

But now I'm getting an error, "Parse error: syntax error, unexpected '}' in"

Any ideas? Any help?

Thank you.

Share Improve this question asked Sep 24, 2022 at 14:08 gfo95gfo95 256 bronze badges 3
  • First check your use of is_page() function: developer.wordpress.org/reference/functions/is_page Second, I guess, you do not need 'echo' function within string. – ddur Commented Sep 24, 2022 at 15:48
  • <?php if (is_page(array('/?sortby=asc', '/?sortby=desc'))) { ?> how about this? – gfo95 Commented Sep 24, 2022 at 16:04
  • No, that is query argument, is_page() requires page ID or slug or title. I guess, you will have to change a lot here. I recommend to start reading here: developer.wordpress.org/reference/functions/get_query_var or here: wpbeginner.com/plugins/reorder-wordpress-posts-using-drag-drop/… Sorry, I can't help you more than that. – ddur Commented Sep 24, 2022 at 16:48
Add a comment  | 

2 Answers 2

Reset to default 1

In that last block, there's a mix of bracketed and bracket-free if statements that make it hard to debug. I'd suggest rewriting it like this and using indentation to make it easier to see what is going on at a glance:

if (is_page(esc_url( wp_get_current_url() . '/?sortby=asc'))) {
    if ( $newQuery->have_posts() ) { 
        while ( $newQuery->have_posts() ) {
            $newQuery->the_post();
        }   
    }
} else { 
    if (is_page(esc_url( wp_get_current_url() . '/?sortby=desc'))) {
        if ( $newQuery->have_posts() ) {
            while ( $newQuery->have_posts() ) {
                $newQuery->the_post();
            }
        }
    } else { 
        if (have_posts()) {
            while (have_posts()) {
                the_post();
            }
        }
    }
}

An editor with syntax highlighting, like VSCode for example, also makes this a lot easier — it changes pairs of bracket colors so that they match.

There are some other problems in the code, though:

  • As @ddur mentioned is_page() is also used incorrectly, you can see this answer for more details (which you've already seen).
  • You could use else if to simplify these nested if statements quite a lot and make it easier to read
  • Those echo statements don't work the way you think, see the code above instead

The error you're getting is a standard PHP error that results from the fact that you're not matching the order and pairing of curly braces ({ and }) in your code... ...PHP is expecting one thing, but finding another.

Also, you're breaking things by the way you're using the echo, you shouldn't be echoing anything inside of is_page().

I don't know if this code will work, but here it is properly formatted:

if( is_page( esc_url( wp_get_current_url() . '/?sortby=asc' ) ) ) :
    if( $newQuery->have_posts() ) :
        while( $newQuery->have_posts() ) : $newQuery->the_post();
    endif;
elseif( is_page( esc_url( wp_get_current_url() . '/?sortby=asc' ) ) ) :
    if( $newQuery->have_posts() ) :
        while( $newQuery->have_posts() ) : $newQuery->the_post();
    endif;
else :
    if( have_posts() ) :
        while( have_posts() ) : the_post();
    endif;
endif;

Try to stick to a coding pattern, like using:

if() :
  //do a thing
endif;

Or...

if() {
  //do a thing
}

Mixing between the two makes it hard for you to keep track of what's what.

wp_get_current_url() isn't an actual WP function, so I don't know where that comes from or what it outputs.

I think your best bet is to check if the parameter matches sortby and if it does then run you alternative queries.

if( isset( $_GET['sortby'] ) ) :
  if( $newQuery->have_posts() ) :
    while( $newQuery->have_posts() ) : $newQuery->the_post();
  endif;
else :
  if( have_posts() ) :
    while( have_posts() ) : the_post();
  endif;
endif;

Again, I don't know if any of this will work. I don't know what $newQuery is or how it's built, I don't know where you got wp_get_current_url()...

Post a comment

comment list (0)

  1. No comments so far