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 |2 Answers
Reset to default 1In 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 nestedif
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()
...
<?php if (is_page(array('/?sortby=asc', '/?sortby=desc'))) { ?>
how about this? – gfo95 Commented Sep 24, 2022 at 16:04