-
Tuyển Dụng
SCW Tuyển 03 WordPress Dev
Có từ 3 năm kinh nghiệm với PHP/Wordpress.
SCW Tuyển Full Stack Developer
[HCM] Tuyển dụng Full-stack Engineer || 1,500 – 2,000 USD
SCW Tuyển PHP Laravel Dev
Phát triển các ứng dụng web, xây dựng website, API theo giải pháp.
-
Settings
Choose Color Theme
Sticky Header
Full Screen
Tìm kiếm một đối tác chuyên nghiệp - giá cả hợp lý cho dự án website của bạn? Chúng tôi sẵn sàng nhận liên hệ từ bạn! Liên Hệ Ngay 👋
Ở bài trước mình đã hướng dẫn các bạn cách chuyển file template sang cấu trúc file của WordPress. Hôm nay, mình sẽ tiếp tục hướng dẫn các hàm Hàm Get Bài Viết – Các Hàm Get Data Hay Dùng trong WP. Cùng theo dõi bào viết dưới đây để chuyển code template web tĩnh thành 1 website động hoàn toàn nhé 👇👇👇
Cách Tạo bài viết trong site admin wordpress
Hàm Get bài viết theo chuyên mục
Ví dụ như với giao diện này mình sẽ tạo ra nhiều chuyên mục bài viết như: thể thao, tin tức, bóng đá, kinh tế…. thì mình sẽ phải load bài viết từng chuyên mục ra sao cho phù hợp.
Đây là code Get bài viết theo chuyên mục bài viết :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!-- Query get posts by categories --> <?php $GetPostsQuery = new WP_query(); $GetPostsQuery->query('post_status=publish&showposts=8&post_type=post&cat=2'); ?> <?php global $wp_query; $wp_query->in_the_loop = true; ?> <?php while ($GetPostsQuery->have_posts()) : $GetPostsQuery->the_post(); ?> <?php $category = get_the_category(); ?> ///Phần dưới này các bạn chèn code HTML - code giao diện vào - Sau đó truyền data vào cho phù hợp <div class="featured-post" style="background-image: url(<?php echo get_the_post_thumbnail_url( get_the_id(), 'full', array( 'class' =>'large') ); ?>);"> <div class="post-info"> <a href="#" class="cat-name tt-u"> <?php echo $category[0]->cat_name;?></a> <h3 class="fw-6 fz-16"><a href="<?php the_permalink(); ?>" class="text-white"><?php the_title(); ?></a></h3> </div> </div> //// <?php endwhile; wp_reset_postdata(); ?> <!-- Query get posts by categories --> |
Chú thích :
- showposts= 8 : 8 là số bài viết bạn muốn lấy
- cat=2 : 2 là ID của chuyên mục mà bạn muốn get data . muốn xem CatID xem như hình dưới.
- <?php echo get_the_post_thumbnail_url( get_the_id(), ‘full’, array( ‘class’ =>’large’) ); ?> : Cách lấy url ảnh đại diện của bài viết.
- <?php echo get_the_post_thumbnail( get_the_id(), ‘full’, array( ‘class’ =>’large’) ); ?> : Còn đây là code lấy ảnh đại diện của bài viết.
- <?php echo $category[0]->cat_name;?> : Code lấy chuyên mục của bài viết.
- <?php the_permalink(); ?> : Code lấy đường dẫn của bài viết.
- <?php echo get_the_date( ‘d-m-Y’ ); ?> : Code lấy ngày đăng của bài viết.
- <?php the_title(); ?> : Code lấy tên của bài viết.
- <?php the_excerpt(); ?> : Code lấy nội dung tóm tắt của bài viết.
- <?php the_content();?> : code lấy nội dung chính của bài viết
- ?php echo get_the_author(); ?> : Code get tác giả đã đăng bài viết hiện tại .
Hàm Get bài viết mới nhất
Nếu đoạn code này được đặt trong index.php sẽ lấy ra những bài viết mới nhất.
Đặt trong file archive.php sẽ lấy danh sách bài viết của chuyên mục bào viết đó dựa vào url bạn truy cập.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!--Query Get Posts Default --> <?php while (have_posts()) : the_post(); ?> ///Phần dưới này các bạn chèn code HTML - code giao diện vào - Sau đó truyền data vào cho phù hợp <div class="featured-post" style="background-image: url(<?php echo get_the_post_thumbnail_url( get_the_id(), 'full', array( 'class' =>'large') ); ?>);"> <div class="post-info"> <a href="#" class="cat-name tt-u"> <?php echo $category[0]->cat_name;?></a> <h3 class="fw-6 fz-16"><a href="<?php the_permalink(); ?>" class="text-white"><?php the_title(); ?></a></h3> </div> </div> //// <?php endwhile; ?> <!--Query Get Posts Default --> |
vậy là sau khi sử dụng 2 hàm Get bài viết trên thì bạn đã có thể làm gần hoàn chỉnh trang chủ của 1 website tin tức rồi đấy. Nếu bạn cần load ra những bài viết xem nhiều, phổ biến, load bài viết liên quan… thì hãy xem thêm bài viết :
Top 10 Những đoạn code phổ biến – hay dùng trong lập trình Theme WordPress
Đây là toàn bộ code của file index.php tới thời điểm hiện tại – các bạn hãy làm theo và xem copy code để hiểu rõ hơn :
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
<?php get_header(); ?> <section class="main-wrapper mb-40"> <div class="container"> <div class="featured-wrap"> <div class="col-md-5 col-sm-12"> <div class="post-carousel owl-carousel"> <!-- Query get posts by categories --> <?php $GetPostsQuery = new WP_query(); $GetPostsQuery->query('post_status=publish&showposts=2&post_type=post&cat=2'); ?> <?php global $wp_query; $wp_query->in_the_loop = true; ?> <?php while ($GetPostsQuery->have_posts()) : $GetPostsQuery->the_post(); ?> <?php $category = get_the_category(); ?> <div class="featured-post" style="background-image: url(<?php echo get_the_post_thumbnail_url( get_the_id(), 'full', array( 'class' =>'large') ); ?>);"> <div class="post-info"> <a href="#" class="cat-name tt-u"> <?php echo $category[0]->cat_name;?></a> <h3 class="fw-6 fz-16"><a href="<?php the_permalink(); ?>" class="text-white"><?php the_title(); ?></a></h3> </div> </div> <?php endwhile; wp_reset_postdata(); ?> <!-- Query get posts by categories --> </div> </div> <div class="col-md-3 col-sm-6"> <div class="post-carousel owl-carousel"> <!-- Query get posts by categories --> <?php $GetPostsQuery = new WP_query(); $GetPostsQuery->query('post_status=publish&showposts=2&post_type=post&cat=4'); ?> <?php global $wp_query; $wp_query->in_the_loop = true; ?> <?php while ($GetPostsQuery->have_posts()) : $GetPostsQuery->the_post(); ?> <?php $category = get_the_category(); ?> <div class="featured-post" style="background-image: url(<?php echo get_the_post_thumbnail_url( get_the_id(), 'full', array( 'class' =>'large') ); ?>);"> <div class="post-info"> <a href="#" class="cat-name tt-u"> <?php echo $category[0]->cat_name;?></a> <h3 class="fw-6 fz-16"><a href="<?php the_permalink(); ?>" class="text-white"><?php the_title(); ?></a></h3> </div> </div> <?php endwhile; wp_reset_postdata(); ?> <!-- Query get posts by categories --> </div> </div> <div class="col-md-4 col-sm-6"> <div class="top-wrap mb-10"> <div class="post-carousel owl-carousel"> <!-- Query get posts by categories --> <?php $GetPostsQuery = new WP_query(); $GetPostsQuery->query('post_status=publish&showposts=2&post_type=post&cat=5'); ?> <?php global $wp_query; $wp_query->in_the_loop = true; ?> <?php while ($GetPostsQuery->have_posts()) : $GetPostsQuery->the_post(); ?> <?php $category = get_the_category(); ?> <div class="featured-post" style="background-image: url(<?php echo get_the_post_thumbnail_url( get_the_id(), 'full', array( 'class' =>'large') ); ?>);"> <div class="post-info"> <a href="#" class="cat-name tt-u"> <?php echo $category[0]->cat_name;?></a> <h3 class="fw-6 fz-16"><a href="<?php the_permalink(); ?>" class="text-white"><?php the_title(); ?></a></h3> </div> </div> <?php endwhile; wp_reset_postdata(); ?> <!-- Query get posts by categories --> </div> </div> <div class="bottom-wrap"> <div class="post-carousel owl-carousel"> <!-- Query get posts by categories --> <?php $GetPostsQuery = new WP_query(); $GetPostsQuery->query('post_status=publish&showposts=2&post_type=post&cat=6'); ?> <?php global $wp_query; $wp_query->in_the_loop = true; ?> <?php while ($GetPostsQuery->have_posts()) : $GetPostsQuery->the_post(); ?> <?php $category = get_the_category(); ?> <div class="featured-post" style="background-image: url(<?php echo get_the_post_thumbnail_url( get_the_id(), 'full', array( 'class' =>'large') ); ?>);"> <div class="post-info"> <a href="#" class="cat-name tt-u"> <?php echo $category[0]->cat_name;?></a> <h3 class="fw-6 fz-16"><a href="<?php the_permalink(); ?>" class="text-white"><?php the_title(); ?></a></h3> </div> </div> <?php endwhile; wp_reset_postdata(); ?> <!-- Query get posts by categories --> </div> </div> </div> </div> <div class="news-ticker-wrap"> <span class="ticker-title tt-u">Breaking News</span> <ul class="news-ticker"> <!--Query Get Posts Default --> <?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> <!--Query Get Posts Default --> </ul> <div class="ticker-control"> <i class="ti-angle-left" id="prev-ticker"></i> <i class="ti-angle-right" id="next-ticker"></i> </div> </div> <div class="main-content mt-20 ov-hidden"> <div class="col-md-8 sm-padding"> <div class="news-block padding-15 bg-white bd-grey mb-40"> <h2 class="block-heading mb-40">Trending News</h2> <div class="grid-posts"> <!--Query Get Posts Default --> <?php while (have_posts()) : the_post(); ?> <div class="col-xs-6 padding-10"> <div class="grid-post"> <a href="<?php the_permalink(); ?>" class="mb-20"> <?php echo get_the_post_thumbnail( get_the_id(), 'full', array( 'class' =>'large img_default_post') ); ?> </a> <div class="post-info padding-15 ptb-20 bd-grey"> <h4 class="mb-15 fw-6"><a href="<?php the_permalink(); ?>" class="text-dark"><?php the_title(); ?></a></h4> <span class="date"><i class="ti-timer"></i> <?php echo get_the_date( 'd-m-Y' ); ?></span> </div> </div> </div> <?php endwhile; ?> <!--Query Get Posts Default --> </div> </div> <div class="news-block padding-15 bg-white bd-grey mb-40"> <h2 class="block-heading mb-40">World News</h2> <div class="two-col-posts"> <div class="col-md-6 padding-10"> <div class="top-wrap mb-10"> <div class="post-carousel owl-carousel"> <!-- Query get posts by categories --> <?php $GetPostsQuery = new WP_query(); $GetPostsQuery->query('post_status=publish&showposts=2&post_type=post&cat=2'); ?> <?php global $wp_query; $wp_query->in_the_loop = true; ?> <?php while ($GetPostsQuery->have_posts()) : $GetPostsQuery->the_post(); ?> <?php $category = get_the_category(); ?> <div class="post-item post1_3"> <a href="#" class="mb-20"><?php echo get_the_post_thumbnail( get_the_id(), 'full', array( 'class' =>'large') ); ?></a> <div class="post-info padding-15 ptb-20 bd-grey"> <h4 class="mb-15 fw-6"><a href="<?php the_permalink(); ?>" class="text-dark"><?php the_title(); ?></a></h4> <span class="date"><i class="ti-timer"></i> <?php echo get_the_date( 'd-m-Y' ); ?></span> </div> </div> <?php endwhile; wp_reset_postdata(); ?> <!-- Query get posts by categories --> </div> </div> </div> <div class="col-md-6 padding-10"> <div class="small-posts"> <!-- Query get posts by categories --> <?php $GetPostsQuery = new WP_query(); $GetPostsQuery->query('post_status=publish&showposts=3&post_type=post&cat=2'); ?> <?php global $wp_query; $wp_query->in_the_loop = true; ?> <?php while ($GetPostsQuery->have_posts()) : $GetPostsQuery->the_post(); ?> <?php $category = get_the_category(); ?> <div class="sm-post-item"> <?php echo get_the_post_thumbnail( get_the_id(), 'full', array( 'class' =>'large') ); ?> <div class="sm-post-content bd-grey"> <h4 class="fz-14"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <p class="fz-12 fw-6 tt-u no-margin"><?php echo get_the_date( 'd-m-Y' ); ?></p> </div> </div> <?php endwhile; wp_reset_postdata(); ?> <!-- Query get posts by categories --> </div> </div> </div> </div> <div class="banner-in-content padding-15 bg-white bd-grey ov-hidden mb-40"> <a href="#"><img src="<?php bloginfo('template_directory') ?>/img/banner.jpg" alt="Banner AD"></a> </div> <div class="news-block padding-15 bg-white bd-grey"> <h2 class="block-heading mb-40">Sports News</h2> <div class="news-items"> <div class="col-xs-12 padding-10"> <!-- Query get posts by categories --> <?php $GetPostsQuery = new WP_query(); $GetPostsQuery->query('post_status=publish&showposts=1&post_type=post&cat=4'); ?> <?php global $wp_query; $wp_query->in_the_loop = true; ?> <?php while ($GetPostsQuery->have_posts()) : $GetPostsQuery->the_post(); ?> <?php $category = get_the_category(); ?> <div class="list-post-item"> <a href="<?php the_permalink(); ?>" class="img-thumb"><?php echo get_the_post_thumbnail( get_the_id(), 'full', array( 'class' =>'large') ); ?></a> <div class="post-info padding-15 ptb-20 bd-grey"> <h4 class="mb-15 fw-6"><a href="<?php the_permalink(); ?>" class="text-dark"><?php the_title(); ?></a></h4> <p class="fz-13"><?php the_excerpt(); ?></p> <span class="date"><i class="ti-timer"></i> <?php echo get_the_date( 'd-m-Y' ); ?></span> </div> </div> <?php endwhile; wp_reset_postdata(); ?> <!-- Query get posts by categories --> </div> <!-- Query get posts by categories --> <?php $GetPostsQuery = new WP_query(); $GetPostsQuery->query('post_status=publish&showposts=4&post_type=post&cat=4'); ?> <?php global $wp_query; $wp_query->in_the_loop = true; ?> <?php while ($GetPostsQuery->have_posts()) : $GetPostsQuery->the_post(); ?> <?php $category = get_the_category(); ?> <div class="col-md-3 col-xs-6 padding-10"> <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail( get_the_id(), 'full', array( 'class' =>'large img-full') ); ?></a> </div> <?php endwhile; wp_reset_postdata(); ?> <!-- Query get posts by categories --> </div> </div> </div> <aside class="col-md-4 sm-padding"> <div class="sidebar-wrap"> <div class="single-sidebar bd-grey bg-white"> <a href="#"><img src="<?php bloginfo('template_directory') ?>/img/banner-2.jpg" alt="Sidebar Ad" class="img-w1"></a> </div> <div class="single-sidebar no-padding"> <a href="https://www.accuweather.com/en/us/new-york-ny/10007/weather-forecast/349727" class="aw-widget-legal"> </a><div id="awcc1499974479079" class="aw-widget-current" data-locationkey="" data-unit="c" data-language="en-us" data-useip="true" data-uid="awcc1499974479079"></div><script data-cfasync="false" src="<?php bloginfo('template_directory') ?>/../../cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script><script type="text/javascript" src="<?php bloginfo('template_directory') ?>/../../../oap.accuweather.com/launch.js"></script> </div> <div class="single-sidebar no-padding"> <div class="side-tab"> <ul class="tab-menu mb-15"> <li class="active"><a href="#recent" data-toggle="tab">Recent</a></li> <li><a href="#popular" data-toggle="tab">Popular</a></li> <li><a href="#ramdom" data-toggle="tab">RAMDOM</a></li> </ul> <div class="tab-content bd-grey padding-15"> <div class="tab-pane fade in active" id="recent"> <ul class="list-post-items"> <?php $getposts = new WP_query(); $getposts->query('post_status=publish&showposts=4&post_type=post'); ?> <?php global $wp_query; $wp_query->in_the_loop = true; ?> <?php while ($getposts->have_posts()) : $getposts->the_post(); ?> <li> <?php echo get_the_post_thumbnail( get_the_id(), 'full', array( 'class' =>'large') ); ?> <div class="list-post-content bd-grey"> <h4 class="fz-14"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <p class="fz-12 fw-6 tt-u no-margin"><?php echo get_the_date( 'd-m-Y' ); ?></p> </div> </li> <?php endwhile; wp_reset_postdata(); ?> </ul> </div> <div class="tab-pane fade in" id="popular"> <ul class="list-post-items"> <?php $getposts = new WP_query(); $getposts->query('post_status=publish&showposts=4&post_type=post'); ?> <?php global $wp_query; $wp_query->in_the_loop = true; ?> <?php while ($getposts->have_posts()) : $getposts->the_post(); ?> <li> <?php echo get_the_post_thumbnail( get_the_id(), 'full', array( 'class' =>'large') ); ?> <div class="list-post-content bd-grey"> <h4 class="fz-14"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <p class="fz-12 fw-6 tt-u no-margin"><?php echo get_the_date( 'd-m-Y' ); ?></p> </div> </li> <?php endwhile; wp_reset_postdata(); ?> </ul> </div> <div class="tab-pane fade in" id="ramdom"> <ul class="list-post-items"> <?php $postquery = new WP_Query(array('posts_per_page' => 4, 'orderby' => 'rand')); if ($postquery->have_posts()) { while ($postquery->have_posts()) : $postquery->the_post(); $do_not_duplicate = $post->ID; ?> <li> <?php echo get_the_post_thumbnail( get_the_id(), 'full', array( 'class' =>'large') ); ?> <div class="list-post-content bd-grey"> <h4 class="fz-14"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <p class="fz-12 fw-6 tt-u no-margin"><?php echo get_the_date( 'd-m-Y' ); ?></p> </div> </li> <?php endwhile; } wp_reset_postdata(); ?> </ul> </div> </div> </div> </div> <div class="single-sidebar"> <h3 class="fw-8 fz-18 tt-u mb-30">News Category</h3> <ul class="cat-list"> <!-- Get category --> <?php $args = array( 'hide_empty' => 0, 'taxonomy' => 'category', ); $cates = get_categories( $args ); foreach ( $cates as $cate ) { ?> <li> <a href="<?php echo get_term_link($cate->slug, 'category'); ?>"><?php echo $cate->name ?></a> </li> <?php } ?> <!-- Get category --> </ul> </div> <div class="single-sidebar"> <h3 class="fw-8 fz-18 tt-u mb-30">Subscribe News24</h3> <form action="#" class="subscribe-form"> <input type="email" name="email" placeholder="Enter Your Email"> <input type="submit" value="Subscribe"> </form> </div> </div> </aside> </div> </div> </section> <?php get_footer(); ?> |
Vậy là nếu biết uyển chuyển xử dụng những đoạn code Get data , tùy biến code cho phù hợp với giao diện thì các bạn sẽ thành công . Còn muốn xem chi tiết những đoạn code hay sử dụng trong Code Theme WordPress thì các bạn có thể xem chi tiết tại đây.
Bài sau mình sẽ hướng dẫn các bạn cách code trang chi tiết của bài viết – trang nội dung bài viết , các bạn hãy theo dõi nhé!
Xem Thêm
Comment
Kết quả xổ số hôm nay Trực Tiếp: KQXS, XSKT, Số Trúng 3 miền, Xổ Số điện toán, xổ số Vietlove, KQXS 3 Miền mượt số #1. Tường thuật trực tiếp kết quả xổ số kiến thiết ngày hôm nay chuẩn nhất - nhanh nhất tại xskt.net.vn!