Support » Theme: Twenty Fourteen » How to order twentyfourteen posts by title on category?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Contortion

    (@contortion)

    I have tried adding $query like so

    <?php
    /**
     * The template for displaying Category pages
     *
     * @link http://codex.wordpress.org/Template_Hierarchy
     *
     * @package WordPress
     * @subpackage Twenty_Fourteen
     * @since Twenty Fourteen 1.0
     */
    
    get_header(); ?>
    
    	<section id="primary" class="content-area">
    		<div id="content" class="site-content" role="main">
    
    			<?php
    			$query = new WP_Query( array ( 'orderby' => 'title', 'order' => 'ASC' ) );
    			if ( $query->have_posts() ) : ?>
    
    			<header class="archive-header">
    				<h1 class="archive-title"><?php printf( __( 'Category Archives: %s', 'twentyfourteen' ), single_cat_title( '', false ) );  ?></h1>
    
    				<?php
    					// Show an optional term description.
    					$term_description = term_description();
    					if ( ! empty( $term_description ) ) :
    						printf( '<div class="taxonomy-description">%s</div>', $term_description );
    					endif;
    				?>
    			</header><!-- .archive-header -->
    
    			<?php
    					// Start the Loop.
    					while ( $query->have_posts() ) : $query->the_post();
    
    					/*
    					 * Include the post format-specific template for the content. If you want to
    					 * use this in a child theme, then include a file called called content-___.php
    					 * (where ___ is the post format) and that will be used instead.
    					 */
    					get_template_part( 'content', get_post_format() );
    
    					endwhile;
    					// Previous/next page navigation.
    					twentyfourteen_paging_nav();
    
    				else :
    					// If no content, include the "No posts found" template.
    					get_template_part( 'content', 'none' );
    
    				endif;
    			?>
    		</div><!-- #content -->
    	</section><!-- #primary -->
    
    <?php
    get_sidebar( 'content' );
    get_sidebar();
    get_footer();

    but it gives me a list of all posts, not the posts in the specific category I am viewing

    Contortion

    (@contortion)

    Ok so I got it kind of working with this

    $args=array(
    		'cat' => $cat,
            'orderby' => 'title',
            'order'    => 'ASC'
    
        );
    
        query_posts($args);

    So now I get posts ordered by title, but now the pagination doesn’t work. I press page 2 of the category in the twentyfourteen theme and it gives me page 1 results again even though it says page 2 in the browser address bar

    Anyone know what to do now?

    Michael

    (@alchymyth)

    don’t edit the template; try and use the 'pre_get_posts' action;

    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    example for your question (to be added to functions.php in the child theme):

    function category_archive_corrections( $query ) {
        if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if ( is_category() ) {
            $query->set( 'orderby', 'title' );
            $query->set( 'order', 'ASC' );
            return;
        }
    }
    add_action( 'pre_get_posts', 'category_archive_corrections' );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to order twentyfourteen posts by title on category?’ is closed to new replies.