hiển thị bài viết mới , nhóm bài viết theo category không cần plugin

Thảo luận trong 'Lập Trình Wordpress' bắt đầu bởi hongbaohn, 13/8/15.

Đã xem: 1,072

  1. hongbaohn Thành Viên

    Sau đây mình sẽ hướng dẫn mọi người hiển thị danh sách bài viết mới nhất mà không cần phải dùng plugin. Các bạn có thể tham khảo code dưới đây

    <?php
    $args = array('numberposts' => '7');
    $recent_posts = wp_get_recent_posts($args);
    //print_r($recent_posts);
    foreach ($recent_posts as $recent) {
    echo '<li>';
    echo '<div class="item-content">
    <div class="item-title"><a
    href="'. get_permalink($recent["ID"]) . '">' . $recent["post_title"] . '</a></div>
    </div>
    <div style="clear: both;"></div>';
    echo '</li>';
    }
    ?>
    Ở trên là bài viết mới nhất, dưới đây sẽ là hiển thị những bài viết trong category (thường hiện thị ra trang chủ mà hay xuất hiện ở các trang tin tức)

    <?php
    $categories = get_categories();
    foreach ($categories as $cat) {
    $category_ids = $cat->term_id;
    $category_num = $cat->category_count;
    $category_link = get_category_link( $category_ids);
    if ($category_num >=0) {
    echo '<div><div class="title-category"><a href="'.esc_url( $category_link ).'">'.$cat->cat_name.'</a></div>';
    $args=array('category__in' => $category_ids,'showposts'=>5);
    $my_query = new wp_query($args);
    echo '<ul id="post">';
    while ($my_query->have_posts()){
    $my_query->the_post();
    echo '<li><h2><a href="'.get_permalink().'">'.get_the_title().'</h2></li>';
    }
    echo '</ul> </div>';
    }
    }?>
     
    Đang tải...
    nam lim xanh

    Bình Luận Bằng Facebook