Get All Post, Product, Portfolio by Cat Category Term ID như thế nào?

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

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,027
    Đã được thích:
    80
    Điểm thành tích:
    48
    Cho em hỏi cách lấy toàn bộ bài viết, danh sách sản phẩm, danh sách Portfolio theo category id mà em truyền vàocủa website wordpress như thế nào vậy? hiện tại em sử dụng lấy không được.

    Em có thử tất cả các cách dưới đây mà lấy không được:
    PHP:
       $args = array(
        
    'post_type' => 'featured_item',
        
    'orderby' => 'title'
        
    //'cat' => 245,
        //'taxonomy' => 'vinh-phuc',
        //'category' => 'vinh-phuc',
        //'category' => array('vinh-phuc'),
        
    'sort_order'=>'asc'   
        
    );
        
    query_posts($args);
    Cái này em thấy họ chỉ là lấy được tất cả mà em không biết cách sử dụng :((
    PHP:
    <?php

    // gets the ID from a custom field to show posts on a specific page
    $buildType get_post_meta($post->ID'build_type_id'true);
    // run query
    query_posts(array(
        
    'post_type' => 'portfolio',
        
    'showposts' => -1,
        
    'tax_query' => array(
            array(
                
    'taxonomy' => 'build-type',
                
    'terms' => $buildType,
                
    'field' => 'term_id',
            )
        ),
        
    'orderby' => 'title',
        
    'order' => 'ASC' )
    );

    ?>
    Cao nhân chỉ em với ạ, em cảm ơn
     
    Cảm ơn đã xem bài:

    Get All Post, Product, Portfolio by Cat Category Term ID như thế nào?

  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,884
    Đã được thích:
    1,194
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Cách dưới của bạn có thể lấy được tất cả đúng rồi bạn nhé. Bạn hiểu cách truyền vào như sau:

    Truyền vào dưới dạng slug của category.
    PHP:
    $args = array(
    'post_type' => 'featured_item',
    'orderby' => 'title',
    'tax_query' => array(
        array(
            
    'taxonomy' => 'featured_item_category',
            
    'terms' => 'hung-yen'//url slug permalink của category
            
    'field' => 'slug'
        
    )
    ),
    'sort_order'=>'asc'
    );
    query_posts($args);
    Truyền vào dưới dạng ID của category
    PHP:
    $args = array(
    'post_type' => 'featured_item',
    'orderby' => 'title',
    'tax_query' => array(
        array(
            
    'taxonomy' => 'featured_item_category',
            
    'terms' => '123'//id của category
            
    'field' => 'term_id'
        
    )
    ),
    'sort_order'=>'asc'
    );
    query_posts($args);
    Biến featured_item và featured_item_category bạn đổi lại theo đúng dữ liệu bạn cần lấy:
    • Lấy Portfolio của themes flatsome: featured_item và featured_item_category.
    • Lấy sản phẩm: product và product_cat.
    • Lấy tin tức: post và category
    Xem cách lấy tại bài sau: https://itseovn.com/threads/get-prodcut-list-by-category-id-or-list-id-products-woocommerce.323853/

    Hoặc code tương tự như:

    PHP:
    $query = new WP_Query( array(
        
    'post_type'      => 'product',
        
    'post_status'    => 'publish',
        
    'posts_per_page' => -1,
        
    'tax_query'      => array( array(
            
    'taxonomy'   => 'product_cat',
            
    'field'      => 'term_id',
            
    'terms'      => array( get_queried_object()->term_id ),
        ) )
    ) );

    while ( 
    $query->have_posts() ) : $query->the_post();
        echo 
    '<li><a href="'get_permalink() .'"><div class="product__preview"><img src="' get_the_post_thumbnail_url() . '"></div><span>' get_the_title() . '</span></a></li>';
    endwhile;

    wp_reset_postdata();
     
    Chỉnh sửa cuối: 2/6/22
  3. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,884
    Đã được thích:
    1,194
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Lấy danh sách bài viết theo chuyên mục category id truyền vào:
    PHP:
    $argspost = array(
        
    'cat'  =>  $categoryid,
        
    'post_type'   => 'post',
        
    'post_status'    => 'publish',
        
    'orderby'          => 'date',
        
    'order'            => 'DESC'
      
    );
      
    $posts get_posts($argspost);
      foreach ( 
    $posts as $ps ) {
        
    $author_id get_post_field ('post_author'$ps->ID);
        
    $byline sprintf(
            
    esc_html_x'%s''post author''flatsome' ),
            
    '<span class="meta-author vcard"><a class="url fn n" href="' esc_urlget_author_posts_url$author_id ) ) . '">' esc_htmlget_the_author_meta'display_name' $author_id) ) . '</a></span>'
        
    );
        
    $user_email get_the_author_meta'user_email' $author_id);
        
    // Convert email into md5 hash and set image size to 80 px
        
    $user_gravatar_url 'https://www.gravatar.com/avatar/' md5($user_email) . '?s=80';
        
    $author .=  '<img width="40" height="40" style="max-width: 40px;float: left;" alt="Avata user" src="'.$user_gravatar_url.'"/>'.$byline;
        
    //////////////////////////////
        
    $contents .= '<div class="cs-itemslider item-post">';
        
    $contents .= '<div class="cs-imgthumb"><a href="'.get_permalink$ps->ID ).'">'.get_the_post_thumbnail($ps->ID'medium', array( 'class' =>'medium') ).'</a></div>';
        
    $contents .= '<p class="cs-pdate">'.get_the_date('j F Y'$ps->ID).'</p>'
        
    $contents .= '<div class="cs-iteminfo"><a href="'.get_permalink$ps->ID ).'"><p class="csld-titlepro">'.$ps->post_title.$ps->ID.'</a></p>';
        
    $contents .= '<p class="csld-despro">'.wp_strip_all_tagsget_the_excerpt($ps), true ).'</p>';
        
    $contents .= '<p class="cs-pauthor">'.$author.'</p>';
        
    $contents .= '<a class="cs-readmore" href="'.get_permalink$ps->ID ).'">Readmore ></a>';
        
    $contents .= '</div></div>';
        
    ///////////////////////////////
      
    }
    echo 
    $contents;
    Khác
    PHP:
    $args=array(
    'cat'  =>  $cat,
    'order'  =>  DESC,
    'orderby'  =>  rand,
    'post__not_in'  =>  array($post->ID),
    'posts_per_page'  =>  9999,
    'caller_get_posts'  =>  1
    );
     
     
    Chỉnh sửa cuối: 28/7/22
  4. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,884
    Đã được thích:
    1,194
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Hoặc 1 cách khác lấy chi tiết sau:
    PHP:
    $id get_the_ID();
    $argspost = array(
    'cat'  =>  1,
    'post_type'   => 'post',
    'post_status'    => 'publish',
    'orderby'          => 'date',
    'posts_per_page' => -1//nếu muốn lấy toàn bộ bài viết.
    'order'            => 'DESC'
    );
    $posts get_posts($argspost);
    foreach ( 
    $posts as $ps ) {
    //////////////////////////////
    $text get_the_excerpt$ps->ID );
    if ( ! 
    $text ) {
        
    $text get_post_field'post_content'$ps->ID );
    }
    $generated_excerpt wp_trim_words$text40 );
    $contents .= '<div id="cs-post'.$ps->ID.'" class="item-post">';
        
    $contents .= '<div class="cs-iteminfo"><p class="csld-titlepro">'.$ps->post_title.'</p>';
        
    $contents .= '<p class="csld-date">'.get_the_date('j F Y'$ps->ID).'</p>';
        
    $contents .= '<p class="csld-despro">'.$generated_excerpt.'</p>';
        
    $contents .= '<a class="cs-readmore" href="'.get_permalink$ps->ID ).'">Learn More ></a>';
    $contents .= '</div></div>';
    ///////////////////////////////
    }
    wp_reset_postdata();
     
    Chỉnh sửa cuối: 28/7/22


Chủ để tương tự : Post Product
Diễn đàn Tiêu đề Date
Mã nguồn mở Wordpress Add Products WooCommerce to Posts, Chèn chi tiết sản phẩm vào bài viết wordpress 31/3/21
Mã nguồn mở Wordpress Get all post by meta key or value, get meta value by post id wordpress 7/7/22
Mã nguồn mở Wordpress How to Get Total Number of Posts by user id Wordpress 13/6/22
Mã nguồn mở Wordpress Get excerpt post in wordpress, lấy mô tả bài bài viết như thế nào? 23/3/22
Mã nguồn mở Wordpress Get author info by post id wordpress như thế nào? 2/11/21