Order posts by name
-
I want to order the post for a certain category by name. How can i do that?
I created a category-anime.php file and copied the content from category.php
anime is the category name.
<?php
/**
* The category archive template file
*
* @package Graphene
* @since Graphene 1.1.5
*/
get_header();
?>
<h1 class="page-title archive-title">
<?php
printf(__('Category Archive: <span>%s</span>', 'graphene'), single_cat_title('', false));
?>
</h1>
<?php graphene_tax_description(); ?>
<?php
/**
* Run the loop for the category page to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-category.php and that will be used instead.
*/
while ( have_posts() ) {
the_post();
get_template_part( 'loop', 'anime' );
}
/* Posts navigation. */
graphene_posts_nav();
?>
<?php get_footer(); ?>then i created a loop-anime.php and copied the content from loop.php.
What do i need to change in the loop to order my posts by name?
I need an answer.
Is it possible to order the posts by their title?
If yes, how?
Add this,
global $query_string;
query_posts( $query_string . '&order=ASC' );right before
while ( have_posts() ) {
Thanks for your reply.
Still your code is missing something: ‘&orderby=title
global $query_string;
query_posts( $query_string . '&orderby=title&order=ASC' );How can i do the same for loop.php?
I tried adding the code but it’s not working.
ModQuote:I tried adding the code but it’s not working.Have you refreshed your browser and server caches?
If i use the code in the loop i get 2 unique posts with the second post being repeated several times
AdminRemove those custom
category-anime.php
andloop-anime.php
files. Insert this code into your child theme’sfunctions.php
file instead:/**
* Sort posts in certain category archives according to post title
*/
function my_sort_archive_by_title( $query ){
if ( $query->is_archive() && $query->is_main_query() ) {
/* Argument to is_category() is an array of the IDs of the
categories whose posts should be sorted by post title */
if ( is_category( array( 1, 9 ) ) ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
}
add_action( 'pre_get_posts', 'my_sort_archive_by_title' );
Viewing 7 posts - 1 through 7 (of 7 total)
- You must be logged in to reply to this topic.