Tạo Shortcode bài viết mới trong widget theo category như thế nào

Thảo luận trong 'Mã nguồn mở Wordpress' bắt đầu bởi seolagi, 6/12/20.

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,029
    Đã được thích:
    81
    Điểm thành tích:
    48
    Hiện tại, em không muốn xài cái bài viết mới nhất trong widget có sẵn vì nó không lấy được theo category mà nó lấy hết, bây giờ em muốn nếu là trang chủ thì nó lấy bài viết random ngẫu nhiên, còn nếu là trang chuyên mục nó sẽ lấy tất cả bài viết mới.

    Bạn nào có code chèn vào function.php để tạo Shortcode xong vào widget tạo file html text cái mình gọi Shortcode đó ra để lấy danh sách bài viết theo mình yêu cầu ở trên không?
     
    Cảm ơn đã xem bài:

    Tạo Shortcode bài viết mới trong widget theo category như thế nào

    health24h thích bài này.
  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,904
    Đã được thích:
    1,199
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Thêm code sau vào function.php của themes nhé:
    PHP:
    //[shortcode_categoryposts page="5"]
    function itseovn_postsbycategory($args$content) {
         
    $string .="<div class='cs-witem'>";
        if ( 
    is_front_page())
        {
            
    $the_query = new WP_Query( array( 'orderby'   => 'rand''posts_per_page' => $args['page'] ) );
             
    $string .= '<h2 class="widget-title">C&#x00F3; th&#x1EC3; b&#x1EA1;n quan t&#x00E2;m?</h2>';
        }else{
            
    $the_query = new WP_Query( array( 'orderby'   => 'desc''posts_per_page' => $args['page'] ) );
             
    $string .= '<h2 class="widget-title">B&#x00E0;i vi&#x1EBF;t m&#x1EDB;i</h2>';
        }
        if ( 
    $the_query->have_posts() ) {
         
    $string .= '<ul class="postsbycategory widget_recent_entries">';
         while ( 
    $the_query->have_posts() ) {
         
    $the_query->the_post();
         
    $string .= '<li><a href="' get_the_permalink() .'" rel="bookmark">' get_the_title() .'</a></li>';
         }
         }else{
        }
        
    $string .= '</ul>';
        
    $string .="</div>";
        return 
    $string;
        
    wp_reset_postdata();
    }
    add_shortcode('shortcode_categoryposts''itseovn_postsbycategory');
    //add_filter('widget_text', 'do_shortcode');
    Để gọi nó ra ngoài thì bạn tạo 1 widget tùy biến HTML và chèn shortcode
    Mã:
    [shortcode_categoryposts page="5"]
    Hoặc có thể tùy biến lại lấy theo category thì thay câu query bằng:
    Lấy theo category name truyền vào
    PHP:
    $the_query = new WP_Query( array( 'category_name' => $args['catename'], 'posts_per_page' => $args['page'] ) );
    • Gọi: [itseovn_postsbycategory catename="tên category" page="5"]
    Lấy theo category id truyền vào
    PHP:
    $the_query = new WP_Query( array( 'category_name' => $args['cateid'], 'posts_per_page' => $args['page'] ) );
    • Gọi: [itseovn_postsbycategory cateid="cate id" page="5"]
    Tự động lấy theo category hiện tại
    PHP:
    $current_cat_id  get_query_var('cat');
    $the_query = new WP_Query( array( 'cat' => $current_cat_id'posts_per_page' => $args['page'] ) );
    Hoặc gọi trong code thì gọi như sau:
    Mã:
    <?php echo do_shortcode('[shortcode_categoryposts page="5"]') ?>
     
    Chỉnh sửa cuối: 15/3/24


Chủ để tương tự : Tạo Shortcode
Diễn đàn Tiêu đề Date
Mã nguồn mở Wordpress Xin code tự động tạo user trong function.php wordpress 6/10/23
Mã nguồn mở Wordpress Lỗi khi tạo subfolder website wordpress, subfolder web vào mục con không được 26/6/23
Mã nguồn mở Wordpress Tạo Popup đặt hàng từ Contact Form 7 Wordpress 13/4/22
Mã nguồn mở Wordpress Tạo trang cảm ơn sau khi submit trong Contact form 7 như thế nào? 6/1/22
Mã nguồn mở Wordpress Tắt đăng ký tạo thành viên mới trong Flatsome Wordpress 18/12/21