Vòng lặp hiển thị post, nội dung trong WordPress
Vòng lặp hiển thị post
thông thường
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $args=array( 'post_type' => 'post', // Gọi danh sách kiểu post type là post 'orderby' => 'date', // Sắp xếp theo ngày tháng 'order' => 'DESC', 'cat' => 1, //ID cateogry, nếu không thì sẽ lấy tất cả post 'posts_per_page'=>5 // Số bài viết hiển thị ); $query = new WP_Query( $args); ?> <?php if ( $query->have_posts() ) : ?> <?php while ( $query->have_posts() ) : $query->the_post();?> <!-- Content --> <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <!-- End content --> <?php endwhile; ?> <?php wp_reset_query(); ?> <?php endif; ?> |
Vòng lặp hiển thị nội dung thông thường (Đối với page.php
, single.php
,…)
1 2 3 4 5 6 7 8 9 |
<?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <!-- Content --> <h2><?php the_title(); ?></h2> <?php the_content(); ?> <!-- End content --> <?php endwhile; ?> <?php wp_reset_query(); ?> <?php endif; ?> |
Vòng lặp hiển thị post
có phân trang (Đối với single.php
, archive.php
) , sử dụng plugin WP-pageNavi
1 2 3 4 5 6 7 8 9 10 11 |
<?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <!-- Content --> <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <!-- End content --> <?php endwhile; ?> <div class="wrap-pagi"> <?php wp_pagenavi(); /* Vị trí hiển thị phân trang */ ?> </div> <?php wp_reset_query(); ?> <?php endif; ?> |
Vòng lặp hiển thị post
có phân trang đối với page Template
, sử dụng plugin WP-pageNavi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'post_type' => 'post', // Gọi danh sách kiểu post type là post 'orderby' => 'date', // Sắp xếp theo ngày tháng 'order' => 'DESC', 'posts_per_page'=>5, // Số bài viết hiển thị 'paged' => $paged ); query_posts($args);?> <?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <!-- Content --> <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <!-- End content --> <?php endwhile; ?> <div class="wrap-pagi"> <?php wp_pagenavi(); /* Vị trí hiển thị phân trang */?> </div> <?php wp_reset_query(); ?> <?php endif; ?> |
Vòng lặp gọi Post trong Category (hiển thị category con theo cateogry cha và bài post của category con)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php // Start the loop. $categories = get_categories(array( 'child_of' => '7', //Id Category Parent 'orderby' => 'ID', 'order' => 'DESC' ));//Parents category id foreach ($categories as $cat) :?> <div class="date-item-child row"> <div class="style-title-child row"> <h4><?php echo $cat->cat_name; ?></h4> </div> <!-- end /.style-title-child--> <?php $args=array( 'post_type' => 'post', 'orderby' => 'ID', 'order' => 'DESC', 'cat' => $cat->term_id, 'posts_per_page'=>5); $query = new WP_Query( $args); ?> <?php if ( $query->have_posts() ) : ?> <?php while ( $query->have_posts() ) : $query->the_post();?> <!-- nội dung cần hiển thị --> <div class="row-item-pr row"> <div class="introtex-pr row"> <?php the_excerpt(); ?> </div> <!-- end /.introtext-pr--> <div class="info-pr row"> <div class="row-mini"> <div class="com-left"> <?php if ( has_post_thumbnail() ) { ?> <img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"> <?php } ?> </div> <!-- end /.com-left--> <div class="com-right"> <div class="title-pr row"> <h5> <a href="<?php the_permalink(); ?>" class="link"><?php the_title();?></a> </h5> </div> <!-- end /.title-pr--> </div> <!-- end /.com-right--> </div> <!-- end /.row-mini--> </div> <!-- end /.info-pr--> </div> <!-- end /.row-item-pr--> <?php endwhile; ?> <?php wp_reset_query(); ?> <?php endif; ?> </div> <!-- end /.data-item-child--> <?php endforeach; ?> |
Vòng lặp Page (Lấy Page theo ID)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $args=array( 'post_type' => 'page', 'orderby' => 'ID', 'order' => 'DESC', 'post__in' => array('98','102','94','73'), //ID Page cần lấy ); $query = new WP_Query( $args); ?> <?php if ( $query->have_posts() ) : ?> <?php while ( $query->have_posts() ) : $query->the_post();?> <?php the_content(); ?> //Nội dung cần lấy <?php endwhile; ?> <?php wp_reset_query(); ?> <?php endif; ?> |
Lặp 2 div cách nhau
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $i=1; if( have_rows('group_image') ): while ( have_rows('group_image') ) : the_row(); ?> <div class="p1-flex-row"> <div class="p1-flex-photo"> <a href="#"> <img src="<?php the_sub_field('item_image'); ?>" alt="<?php the_title(); ?>" title="<?php the_sub_field('item_title'); ?>"> </a></div> <h4 class="p1-flex-title"> <?php the_title(); ?> </h4> </div> <?php if( $i%3 ==0 ) { echo '</div><div class="p1-flex">';} ?> <?php $i++; ?> <?php endwhile; endif; ?> |
Lấy category con theo ID của category cha
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $customPostTaxonomies = get_object_taxonomies('examples'); if (count($customPostTaxonomies) > 0) { foreach ($customPostTaxonomies as $tax) { $args = array( 'orderby' => 'id', 'order' => 'ASC', 'child_of' => '42', 'show_count' => 0, 'pad_counts' => 0, 'hierarchical' => 1, 'taxonomy' => $tax, 'title_li' => '' ); wp_list_categories($args); } } ?> |