Ordenar los resultados por nombre y orden de asc en Archive.php

9

Actualmente utilizo el siguiente código para listar publicaciones en Archive.php pero quiero que los resultados se ordenen por nombre en orden ascendente. He comprobado el códice pero la respuesta no está clara para mí, ¿cómo puedo obtener esto? trabajando?

<?php $post = $posts[0]; // ?>

Gracias de antemano.

    
pregunta Dave Burns 23.01.2012 - 11:03

3 respuestas

26

La forma más fácil de hacer esto es usar un gancho (el pre_get_posts hook) para cambiar el orden. ¡Pero debe comprobar que la consulta es una para la que desea modificar el orden! ( is_archive() o is_post_type_archive() debería ser suficiente.)

Por ejemplo, ponga lo siguiente en las funciones de su tema.php ...

add_action( 'pre_get_posts', 'my_change_sort_order'); 
    function my_change_sort_order($query){
        if(is_archive()):
         //If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type )
           //Set the order ASC or DESC
           $query->set( 'order', 'ASC' );
           //Set the orderby
           $query->set( 'orderby', 'title' );
        endif;    
    };
    
respondido por el Stephen Harris 23.01.2012 - 11:32
1
<?php
// we add this, to show all posts in our
// Glossary sorted alphabetically
if ( is_category('Glossary') )  {
    $args = array( 
        'posts_per_page' => -1, 
        'orderby'        => 'title', 
        'order'          => 'ASC' 
    );
    $glossaryposts = get_posts( $args );
}
foreach( $glossaryposts as $post ) : setup_postdata( $post );
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
    
respondido por el Abdelfattah Saied Baraka 20.01.2017 - 22:32
0

además de la respuesta de Stephen, si solo desea consultar y ordenar por título, puede usar esto en su archivo de plantilla:

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

query_posts($args);
    
respondido por el josh 23.01.2012 - 12:38

Lea otras preguntas en las etiquetas