Archived topic
list category in alpha order
12 replies · Started by Nanette on August 8, 2019
I have snippet code but finding it hard to set up a code to have this page in alpha order
https://nir-for-food.com/category/applications/
How can I do that?
Hi there,
i provide a PHP snippet here for ABC ordering:
https://generatepress.com/forums/topic/posts-and-post-length-and-filters/
Instead of the is_home() conditional you can use is_category( 'cat-slug' )
I did it and made the change in conditional but got this:
Don't Panic
The code snippet you are trying to save produced a fatal error on line 6:
Cannot redeclare db_abc_modify_query_order() (previously declared in /www/wp-content/plugins/code-snippets/php/snippet-ops.php(361) : eval()'d code:1)
The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.
Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.
So your code should look like this:
function db_abc_modify_query_order( $query ) {
if ( $query->is_category( 'applications' ) && $query->is_main_query() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'db_abc_modify_query_order' );
This is weird, I have this site on my local for testing and it works on that one. But when I put it in my live site, I still get the same message:
Don't Panic
The code snippet you are trying to save produced a fatal error on line 6:
Cannot redeclare db_abc_modify_query_order() (previously declared in /www/wp-content/plugins/code-snippets/php/snippet-ops.php(361) : eval()'d code:1)
The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.
Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.
Okay on a whim, I changed the priority to 9 and it works, but it works on this page:
https://nir-for-food.com/applications/
But not on this page:
https://nir-for-food.com/category/applications/
So on the site you're seeing the error, can you check to make sure you don't have two snippets that are declaring the same function. That is what the error is saying.
the /appications is a page built with WPSP you can set the order that they are displayed in the Settings tab of the list.
Fix the error above and see of the /category/applications/ is working.
I am so flattered that you think I would understand what WPSP is. :-), but I don't. I do have another snippet I used for another category for the blog home page called industries. But the error did go away when I made the category for applications at a lower priority than 10 like the other one was. It worked for this page
https://nir-for-food.com/applications/
But not for the page you said was built on WSPS, you say I can set the order that they are displayed "in the Settings tab of the list", could you expound on that for my ignorant ears? Where is that tab?
WPSP = WP Show Posts :)
So Dashboard > WP Show Posts > And edit the lists you want to display alphabetically and within its Settings you can select the order.
So the other snippet being used is order the Industry archive correctly?
If that is the case then in your second snippet you need a unique function name ie.
db_abc_modify_query_order
there are two instances of this in the code just change them both to eg.
db_abc_modify_query_order_2
I did change them on top and bottom, like so:
function db_abc_modify_query_order-2( $query ) {
if ( $query->is_category( 'applications' ) && $query->is_main_query() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'db_abc_modify_query_order-2' );
The first instance gave These errors:
parse error : syntax error, unexpected '-', expecting '(' on line 1
parse error : syntax error, unexpected '-', on line 1
parse error : syntax error, unexpected '-', expecting ')' on line 1
parse error : syntax error, unexpected '-', expecting '{' on line 1
parse error : syntax error, unexpected '{', expecting ';' on line 1
Sorry my bad it should read:
db_abc_modify_query_order_2 ie. replace the hyphen with an underscore.
YEA! Thanks, Dave, Alls Well the ends Well.
Awesome - glad to be of help