0

Only two categories need to be showed in the homepage. Can anyone help.

1
  • 2
    link? post? image? May 30, 2017 at 9:07

5 Answers 5

1

You can use WP_Query to get your posts list, and display it with the loop

Example :

$the_query = new WP_Query( array( 'category_name' => 'staff,news' ) );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}
2
  • Thank for answering but actually I am novice. So can you please tell where to add this code and in detail.
    – Sarah
    May 31, 2017 at 8:21
  • In the body of your home page template file, like front-page.php, or index.php, or home.php, or anything you might use. Jun 1, 2017 at 8:00
0

In your functions.php file paste the below code:

I am assuming that you want to show categories from two categories which are having ids 5 and 9.

function kiran_home_category( $query ) {
 if ( $query->is_home() && $query->is_main_query() ) {
 $query->set( 'cat', '5,9');
 }
}
add_action( 'pre_get_posts', 'kiran_home_category' );

Explanation:

kiran_home_category is just a custom name for the function. That can be any name. The way it works is you attach a function to the action hook pre_get_posts. So before getting the posts the function kiran_home_category will be called. And then inside the function I am changing the query here to only load categories with ID 5 and 9

4
  • Thanks for helping but what is "kiran_home_category" ?....How will it work
    – Sarah
    Jun 1, 2017 at 13:36
  • This is the theme "demo.themegrill.com/demos/?theme=ColorNews", if you want to check
    – Sarah
    Jun 1, 2017 at 13:37
  • Oh. kiran_home_category is just a custom name for the function. That can be any name. The way it works is you attach a function to the action hook pre_get_posts. So before getting the posts the function kiran_home_category will be called. And then inside the function I am changing the query here to only load categories with ID 5 and 9
    – Kiran Dash
    Jun 2, 2017 at 4:17
  • You are welcome. I am glad that it worked. And if your issue is solved, please feel free to mark the correct answer. Have a great day :)
    – Kiran Dash
    Jun 2, 2017 at 6:14
0

In wordpress WP_query, category__in parameter used to select category with posts.

<?php 
      $query = new WP_Query( array( 'category__in' => array( 2, 6 ),'post_status'=>'publish','orderby'=>'menu_order','order'=>'Asc' ) );
     if($query->have_posts()):
        echo '<ul>';
        while ( $query->have_posts() ) : the_post();
            echo '<li>' . get_the_title() . '</li>'; 
         endwhile;
        echo '</ul>';
     endif;
   ?>

For more information about wordpress query click here , you can read more information.

4
  • yes, it show both categories. which category id is 2 and 6 . Jun 1, 2017 at 5:15
  • I really appreciate your help. But in WordPress editor where to make changes and how to find out the category number?
    – Sarah
    Jun 1, 2017 at 13:25
  • This is the theme "demo.themegrill.com/demos/?theme=ColorNews", if you want to check
    – Sarah
    Jun 1, 2017 at 13:38
  • You can add this code any where in template, where you want to display the posts of category like front-page.php, or index.php, or home.php. when you edit the category then it will show tag_ID=?(number) in url. that number is category number. for eg. http://localhost/example.com/wp-admin/term.php?taxonomy=category&tag_ID=1&post_type=post&wp_http_referer=%2Fpneumocare.com%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory in this example.you can see tag_ID=1 i.e category id=1. which is category number. Jun 2, 2017 at 6:47
0
   <?php 
        $args = array( 'post_type' => 'post', 'posts_per_page' => -1,'category_name' => array('Latest News','News')  );
        $loop = new WP_Query( $args );
        if($loop->have_posts()):
    ?><ul>

                <?php
                    while ( $loop->have_posts() ) : $loop->the_post();
                ?>
                    <li> <span class="date"><?php echo get_the_date( 'd F Y');?></span>
                    <h3><?php echo get_the_title();?></h3>
                    <?php echo $description = get_the_content(); ?>
                    </li>

                <?php endwhile;?>
            </ul>

    <?php endif;?>  
    <?php wp_reset_postdata(); ?>
4
  • Thank for answering but actually I am novice. So can you please tell where to add this code and in detail.
    – Sarah
    May 31, 2017 at 8:20
  • You can add this code any where in template, where you want to display the posts.
    – yogesh
    Jun 1, 2017 at 8:55
  • This is the theme "demo.themegrill.com/demos/?theme=ColorNews", if you can please see and tell
    – Sarah
    Jun 1, 2017 at 13:39
  • Please just copy and paste above code in any template in your theme, where you want to display, and replace category name with your category name.
    – yogesh
    Jun 3, 2017 at 3:51
0

Do the following, usually in page.php or single.php or if you want a custom page for a category, you can do, category-samplecat.php..

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
     $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category_name' => array('samplecat', 'anothercat'),
        'paged' => $paged
    );

    $arr_posts = new WP_Query($args);

Then do the usual if, while statement..

if($arr_posts->have_posts() ) :
        // Start the loop.
            while ( $arr_posts->have_posts() ) : 
                $arr_posts->the_post();?>

<?php endwhile; 
endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.