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.
<?php if (is_page(array('/?sortby=asc', '/?sortby=desc'))) { ?>how about this?