Get post by category id related post at category in wordpress

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

  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
    Để lấy bài viết post theo category chuyên mục theo category id, cat_id bạn làm như sau, sẽ lấy được bài viết theo ý muốn của mình, để làm bài viết liên quan related post in category.

    PHP:
    <?php
        $category 
    get_categoryget_query_var'cat' ) );
        
    $cat_id $category->cat_ID;

        
    $args = array( 'posts_per_page' => 8'offset'=> 1'category' => $cat_id );

        
    $myposts get_posts$args );
        foreach ( 
    $myposts as $post ) : setup_postdata$post ); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
        <?php endforeach;
        
    wp_reset_postdata();
    ?>
    Hoặc
    PHP:
    $post_id "100,200";//danh sách id loại trừ
    $categoryid =  100//chuyên mục category truyền vào
    $args=array(
    'category__in' => $categoryid,
    'post__not_in' => array(explode(','$post_id)),
    'posts_per_page'=> 2// Số sản phẩm lấy ra trên 1 trang.
    //'meta_key'      => 'acf_field', //acf field nếu có
    //'meta_value'    => '10' // giá trị của acf Field
    );
    $my_query = new wp_query$args );
    if( 
    $my_query->have_posts() ) {
        while( 
    $my_query->have_posts() ) {
            
    $my_query->the_post();
            
    $the_excerpt  get_the_excerpt();
            
    $content .='<div class="relatedthumb">
                <a href="' 
    get_the_permalink() .'">'get_the_post_thumbnail().'</a>
                </div>
                <div class="relatedcontent">
                        <a class="cs-blog_title" href="'
    get_the_permalink().'">'get_the_title().'</a>
                        <p class="cs-excerpt">'
    .$the_excerpt.'</p>
                        <a class="cs-botton" href="'
    get_the_permalink().'">Tìm hiểu thêm</a>
                </div>'
    ;
        } 
    //End while
    }
    wp_reset_postdata(); //Restore original Post Data
     
    Cảm ơn đã xem bài:

    Get post by category id related post at category in wordpress

    Last edited by a moderator: 13/3/24


Chủ để tương tự : post category
Diễn đàn Tiêu đề Date
Mã nguồn mở Wordpress Get All Post, Product, Portfolio by Cat Category Term ID như thế nào? 27/1/21
Mã nguồn mở Wordpress Lấy category id từ id của bài viết, get catgory id by post id 7/12/20
Mã nguồn mở Wordpress Get All Post of Category at add_shortcode function.php wodpress như thế nào? 20/4/20
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