- This topic has 11 replies, 4 voices, and was last updated 1 year ago by David.
-
AuthorPosts
-
May 11, 2018 at 10:55 am #573468Robert
We have a category Country Profiles for which we now suppress showing the date using some CSS. We would like to arrange the posts within this category alphabetically by title. We can do it by assigning suitable dates to each post. But can this be automatically with some code?
May 11, 2018 at 5:24 pm #573639LeoStaffCustomer SupportHi there,
This article should help: https://codex.wordpress.org/Alphabetizing_Posts
May 12, 2018 at 8:47 am #574011RobertThanks, Leo!
In our case we do not have that many posts in the category, so we are just assigning dates to achieve alphabetical order. Simple, but it works!May 12, 2018 at 9:34 am #574043LeoStaffCustomer SupportNo problem 🙂
April 10, 2023 at 10:59 am #2604449Liissorry to jump on an old post.
I add that php with code snippets “php” section and just change the ‘Glossary’ to ‘Uncategorized’ for example, i get a critical error.
Am I doing it wrong with code snippets?
<?php
get_header();
?>-
<?php
- “><?php the_title(); ?>
// we add this, to show all posts in our
// Glossary sorted alphabetically
if (is_category(‘Uncategorized’))
{
$args = array( ‘posts_per_page’ => -1, ‘orderby’=> ‘title’, ‘order’ => ‘ASC’ );
$glossaryposts = get_posts( $args );
}
foreach( $glossaryposts as $post ) : setup_postdata($post);
?><?php endforeach; ?>
April 10, 2023 at 4:28 pm #2604727YingStaffCustomer SupportThe code is a PHP template, it’s not something you add to the code snippet.
However, with GP premium and the Query loop block of GenerateBlocks plugin, you can achieve this without writing PHP code:
1. Go to appearance > elements, create a block element, choose
loop template
as the element type.2. Add the GB query loop block, you can create whatever layout you want for the archive.
3. Once the design is done, you can configure the query, choose the category, and add
order
andorderby
parameters.4. Choose
post category archive
>your category
as the location, and publish the element.Or
you can simply add this function to code snippet:
function my_category_orderby( $query ) { if ( $query->is_category( 'uncategorized' ) && $query->is_main_query() ) { $query->set( 'orderby', 'title' ); $query->set( 'order', 'ASC' ); } } add_action( 'pre_get_posts', 'my_category_orderby' );
April 10, 2023 at 11:17 pm #2605039LiisThank you for the elaborate answer!
So the last snippet i can directly put using code snippets plugin php section?
April 10, 2023 at 11:32 pm #2605053Fernando Customer SupportHi Liis,
Yes, you may.
April 12, 2023 at 11:33 pm #2608052LiisThank you so much! Last question, if i would want to put a few categories, would just add then in ” next to ‘uncategorized’ ?
like so:
function my_category_orderby( $query ) {
if ( $query->is_category( ‘uncategorized’ ‘category1’ ) && $query->is_main_query() ) {
$query->set( ‘orderby’, ‘title’ );
$query->set( ‘order’, ‘ASC’ );
}
}
add_action( ‘pre_get_posts’, ‘my_category_orderby’ );April 12, 2023 at 11:42 pm #2608066Fernando Customer SupportSomething like this:
function my_category_orderby( $query ) { if ( $query->is_category( array( 'uncategorized', 'category1' ) ) && $query->is_main_query() ) { $query->set( 'orderby', 'title' ); $query->set( 'order', 'ASC' ); } } add_action( 'pre_get_posts', 'my_category_orderby' );
It’s inserted inside an array and separated by a comma.
April 15, 2023 at 12:12 am #2610338Liisthat worked! fantastic thank you. Both my categories slug and name were the same. But in the future, what if my category name is reviews a to z. Should I put the name there or the slug reviews-a-to-z?
April 15, 2023 at 4:19 am #2610477DavidStaffCustomer SupportHi there,
you can put either the name, the slug or the id or any mixture of those in the
is_category
arrayi find the slug ( or ID ) less ambiguous so i generally stick to those when there is spaces in the name
-
AuthorPosts
- You must be logged in to reply to this topic.