最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin17浏览0评论

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()...

发布评论

评论列表(0)

  1. 暂无评论