简体   繁体   中英

Wordpress - how to order posts within each category alphabetically by title, without it showing all posts from all categories

I am having difficulties showing posts ASC by title within each wordpress category. Various code I have found will list alphabetically but returns all posts from the website rather than just the category the user is looking at. I don't want to create multiple category.php files or hardcode which category because I would have to manually set a new file up everytime a category is added to the website. I just want the code to only look in the current category but I do not have the knowledge to code this in.

I am using a standard category.php file with loop.php file. I've removed all the code attempting to re-order the posts but one of the examples I was trying was from this website:

http://wordpress.org/support/topic/display-posts-in-ascending-order

My category.php is this:

<?php get_header(); ?>
     <div class="breadcrumbs">
   <span> <?php if(function_exists('bcn_display'))
    {
        bcn_display();
    }?></span>
</div>

    <!-- section -->
    <div class="area">
    <section role="main">
            <h2><?php single_cat_title();  ?></h2>
        <!--<h2><?php the_category();  ?></h2>-->

         <?php echo category_description( $category_id ); ?> 
        <div class="display-posts-listing">

        <?php get_template_part('loop'); ?>
        </div>
        <?php get_template_part('pagination'); ?>

    </section>
    <!-- /section -->

<?php get_sidebar(); ?>
    </div>
<?php get_footer(); ?>

loop is this:

<?php if (have_posts()): while (have_posts()) : the_post(); ?>



    <!-- article -->
    <div class="listing-item">
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <!-- post title -->
        <h3>
            <?php the_title(); ?>
        </h3>
        <!-- /post title -->
        <!-- post thumbnail -->
        <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
                        <?php the_post_thumbnail('full'); // Declare pixel size you need inside the array ?>

        <?php endif; ?>
        <!-- /post thumbnail -->



        <!-- post details -->

                <!--<span class="author"><?php _e( 'Published by', 'html5blank' ); ?> <?php the_author_posts_link(); ?></span>-->
        <!--<span class="comments"><?php comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></span>-->
        <!-- /post details -->

        <?php //html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>

        <?php the_content(); ?>
        <?php edit_post_link(); ?>

    </article>
    </div>
    <!-- /article -->

<?php endwhile; ?>

<?php else: ?>

    <!-- article -->
    <article>
        <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
    </article>
    <!-- /article -->

<?php endif; ?>

Thanks

Cathy

You should try something like this:

if(is_category('your_category') :
    $args = array(
    'post_type'        => 'post',  
    'posts_per_page'   => 'how_many_posts_you_want_-1_if_all',
    'cat'                 => 'your_category_number',
    'orderby'             => 'title', 
    'order'       => 'ASC'
);
else :
    $args = array(
    'post_type'         => 'post',  
    'posts_per_page'    => 'how_many_posts_you_want_-1_if_all',
    'order'         => 'ASC'
);

endif;

$loop = new WP_Query( $args ); 

while($loop->have_posts()) : $loop->the_post(); 
//do your magic here
endwhile;

wp_reset_query();

This should do the trick.

quick edit note: I overlooked that the OP was requesting it for category pages, my advice is for tag pages. Should be able to adapt it by changing is_tag to is_category. Hopefully it helps someone.

I was trying to remember how I did this on some old themes, was having trouble tonight on a new one.

in functions.php

/* this controls some tag.php arguments */
    function modify_query_order_tag( $query ) {
       if ( $query->is_tag() && $query->is_main_query() && !is_admin() ) {
          $query->set( 'orderby', 'title' );
          $query->set( 'order', 'ASC' );
       }
    }
    add_action( 'pre_get_posts', 'modify_query_order_tag' );

in tag.php

/* check functions.php for function modify_query_order_tag to modify arguments */
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    html

    <?php endwhile; endif; ?>

You can of course add other arguments to the function such as posts_per_page etc.

(No idea if I hacked it together myself or if I got it from someone smarter, somewhere else. Probably from someone smarterer.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM