skip to Main Content

I don’t know how to explain my problem but I like to echo the specific category/ slug inside the specified section (sorting off by categories). I don’t know which WordPress function should be use? is it get_category or get_post?
so far I have this code to echo all the categories in one section:

         <?php
         $args = array(
            'post_type' => 'allproducts',
            'posts_per_page' => 9,
         );
            $posts = get_posts( $args );
            foreach ( $posts as $post ):
            setup_postdata( $post );
        ?>   

basically my goal is to get all the post from specific category:
I have these list of categories with specific slugs and ID:

Category Name | Slug | ID
Shoes         | cat01|  2
Pants         | cat02|  3
Tops          | cat03|  4

2

Answers


  1. Chosen as BEST ANSWER

    I figure it out. here's my code:

            <?php 
                $args1 = array( 'posts_per_page' => 9, 'offset'=> 0, 'category' => array(1));
                $myposts1 = get_posts( $args1 );
            
                foreach ( $myposts1 as $post ) : setup_postdata( $post ); 
            ?>    
    

    array (1) --> is the id of my category

    closed it with this code

                    <?php endforeach; 
                    wp_reset_postdata();?>   
    

  2. $categories = get_categories( array(
      'orderby' => 'name',
    ) );
    
    foreach($categories as $category)
    {
      $cateid = $category->term_id;
      $args = array(
            'post_type' => 'allproducts',
            'category' => $cateid,
            'posts_per_page' => 9,
         );
      $posts = get_posts( $args );
      foreach ( $posts as $post ){
    // here rest of code of posts.
      }      
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search