Archived topic
Post Order By DESC
13 replies · Started by Rafael on May 23, 2022
Hello:
I need help about this two items:
1-How Can I make posts displayed in alphabetical order??
2-IN the menu I have a principal category, and want when I clic on it dispplay subcategory, insted all posts, and when I clic in subcategory display all posts of that subcategory??
Apreciate you help.....
Rafael
Hi there.
1. try this snippet to order alphabetically by title:
add_action( 'pre_get_posts', function( $query ) {
if ( $query->is_main_query() && ! is_admin() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
} );
2. Just to be clear, on a parent category page you don't want to list ANY posts ? Just a list of child categories ?
Hi David...
Thanks for your fast reply.....
Regarding to item 2, Yes...on a parent category I dont want to list any post..but display child categories of that parent category with count of post in each child category.............and display posts only in child categoy.....
Like this...........
Rafael
Ok try this PHP Snippet:
add_action('wp', function(){
if ( is_category() ) {
$term = get_queried_object();
if ( $term->parent == 0 ) {
add_filter( 'generate_do_template_part', function( $do ) { return false; });
add_action('generate_after_loop', 'db_show_the_children_categories');
remove_action( 'generate_after_loop', 'generate_do_post_navigation' );
}
}
});
function db_show_the_children_categories(){
$cat = get_category( get_query_var( 'cat' ) );
$cat_id = $cat->cat_ID;
$child_categories=get_categories(
array(
'parent' => $cat_id,
'order' => 'ASC'
)
);
echo '<ul class="child-categories">';
foreach ( $child_categories as $child ) {
$child_html = sprintf(
'<li><a href="%1$s">%2$s</a><span class="count">%3$s</span></li>',
esc_url( get_category_link( $child->term_id ) ),
esc_html( $child ->cat_name ),
esc_html( $child ->count )
);
echo $child_html;
}
echo '</ul>';
}
Way it works is to first check if were on a parent category and if so it sets the generate_do_template_part to bypass the posts being output in the loop, and then hooks in the custom db_show_the_children_categories to show a list of subcategories.
And a little CSS for basic styling:
.child-categories {
list-style: none;
width: 100%;
}
.child-categories li {
border-bottom: 1px solid #ccc;
}
.child-categories .count {
margin-left: 10px;
background-color: #000;
color: #fff;
padding: 0 10px;
border-radius: 1em;
}
Thank you David.....That's first class service!!!!
Awesome - glad to be of help!
I have inserted the codes...and its perfect!!..
Only that paging shouldn't show in child-categoris list.....How to solve that??
Ah.. i made an edit to the snippet here:
https://generatepress.com/forums/topic/post-order-by-desc/#post-2229418
the change is just to the first code block. The line added was:
remove_action( 'generate_after_loop', 'generate_do_post_navigation' );
Done....Thanks....
You're welcome
Hi David...
What to change in the code for this case??:
Parent
-Child (count)
--Grandchild (count)
-----------------------------------------------------------------
add_action('wp', function(){
if ( is_category() ) {
$term = get_queried_object();
if ( $term->parent == 0 ) {
add_filter( 'generate_do_template_part', function( $do ) { return false; });
add_action('generate_after_loop', 'db_show_the_children_categories');
remove_action( 'generate_after_loop', 'generate_do_post_navigation' );
}
}
});
function db_show_the_children_categories(){
$cat = get_category( get_query_var( 'cat' ) );
$cat_id = $cat->cat_ID;
$child_categories=get_categories(
array(
'parent' => $cat_id,
'order' => 'ASC'
)
);
echo '<ul class="child-categories">';
foreach ( $child_categories as $child ) {
$child_html = sprintf(
'<li><a href="%1$s">%2$s</a><span class="count">%3$s</span></li>',
esc_url( get_category_link( $child->term_id ) ),
esc_html( $child ->cat_name ),
esc_html( $child ->count )
);
echo $child_html;
}
echo '</ul>';
---------------------------------------------------------------------------
Try this code instead:
add_action('wp', function(){
if ( is_category() ) {
$term = get_queried_object();
if ( $term->parent == 0 ) {
add_filter( 'generate_do_template_part', function( $do ) { return false; });
add_action('generate_after_loop', 'db_show_the_children_categories');
remove_action( 'generate_after_loop', 'generate_do_post_navigation' );
}
}
});
function db_show_the_children_categories(){
$cat = get_category( get_query_var( 'cat' ) );
$cat_id = $cat->cat_ID;
$child_categories=get_categories(
array(
'parent' => $cat_id,
'order' => 'ASC'
)
);
echo '<ul class="child-categories">';
foreach ( $child_categories as $child ) {
$child_html = sprintf(
'<li><a href="%1$s">%2$s</a><span class="count">%3$s</span>',
esc_url( get_category_link( $child->term_id ) ),
esc_html( $child ->cat_name ),
esc_html( $child ->count )
);
$grandchild_categories = get_terms( $child->taxonomy, array(
'parent' => $child->term_id,
'hide_empty' => false
) );
if ( $grandchild_categories ) {
$child_html .= '<ul class="child-categories">';
foreach ( $grandchild_categories as $grandchild ) {
$child_html .= sprintf(
'<li><a href="%1$s">%2$s</a><span class="count">%3$s</span></li>',
esc_url( get_category_link( $grandchild->term_id ) ),
esc_html( $grandchild ->name ),
esc_html( $grandchild ->count )
);
}
$child_html .= '</ul>';
}
$child_html .= '</li>';
echo $child_html;
}
echo '</ul>';
}
Thanks for your help, David....
RAfael
You're welcome