P
P
Pavel Sergeev2015-12-04 09:46:38
CMS
Pavel Sergeev, 2015-12-04 09:46:38

How to sort posts in wordpress category?

Hello!
There is a category.php file. It contains the following code:

<?php get_header(); ?>
    <?php get_sidebar(); ?>
    <div class="content">
        <h2 class="page-title"><?php printf( __( '%s', 'masteritca' ),single_cat_title( '', false )); ?></h2>
        <div class="catalog-wrapper">
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                <div class="catalog-item">
                    <div class="bestsellers-img">
                        <?php the_post_thumbnail(); ?>
                    </div>
                    <span class="bestsellers-name"><?php the_title(); ?></span>
                    <a href="<?php the_permalink(); ?>" class="bestsellers-price"><?php the_field( 'product_price'); ?></a>
                </div>
                <?php endwhile; endif;?>
                    <?php if(function_exists( 'wp_pagenavi')) { wp_pagenavi(); } ?>
            </div>
    </div>
    <?php get_footer(); ?>

I can't seem to find a way to sort the output posts by title. I will be grateful for advice.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Khristoev, 2015-12-04
@Haoss

as an example, opened <?php query_posts('cat='.$cat_list.'&order=ASC'); ?> and closed, ASC was replaced with the desired

<?php
 $category = get_the_category(); 
 $cat_list = $category[0]->cat_ID;
?>
                            
                                <?php query_posts('cat='.$cat_list.'&order=ASC'); ?>
                                <?php if (have_posts()): while (have_posts()): the_post(); ?>
                                    <tr>
                                        <td>
                                            <?php the_post_thumbnail('full'); ?>
                                        </td>
                                        <td class="separator">&nbsp;</td>
                                        <td>
                                            <?php the_content(); ?>
                                        </td>
                                    </tr>
                                <?php endwhile; endif; wp_reset_query(); ?>

'cat='.$cat_list is needed to display from this category, without it it throws everything out.

P
Pavel Chesnokov, 2015-12-06
@cesnokov

You can also use functions.php:

function modify_category_query_order( $query ) {
    if ( $query-> is_category()) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'modify_category_query_order' );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question