Khi nào nên sử dụng WP_query (), query_posts () và pre_get_posts phần 1 (ok)
https://qastack.vn/wordpress/50761/when-to-use-wp-query-query-posts-and-pre-get-posts
add_action('pre_get_posts','wpse50761_alter_query');
function wpse50761_alter_query($query){
if( $query->is_main_query() ){
//Do something to main query
}
}get_posts ()
<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul>PreviousSử dụng pre_get_posts với WP_Query (ok)NextKhi nào thì dùng WP_Query (), query_posts () và pre_get_posts phần 2 (ok)
Last updated