How do I get posts from category using the slug?

I have my own theme and I'd like to display posts on my home page from a specific category.

So far I've achieved it like this:

<?php
    global $post;
    $args = array( 'numberposts' => 10, 'category' => 6 );
    $posts = get_posts( $args );
    foreach( $posts as $post ): setup_postdata($post); 
?>

    <divs with the_title() the_excerpt() etc ></div>

<?php 
    endforeach; 
?>

But what if I want to get the category by a its slug? Or is it possible to simply make a category selection box in from within the admin panel?

Answers


Replace your category parameter with category_name

<?php
    global $post;
    $args = array( 'numberposts' => 10, 'category_name' => 'cat-slug' );
    $posts = get_posts( $args );
    foreach( $posts as $post ): setup_postdata($post); 
?>

<divs with the_title() the_excerpt() etc ></div>

<?php endforeach; ?>

For more info: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters


suppose you have category name 'ice cakes' and category slug as 'ice-cakes', then our code to retrieve post under category 'ice cakes' is as follows:

<?php
              $args = array( 'posts_per_page' => 3,
               'category_name' => 'ice-cakes' );

              $icecakes = get_posts( $args );
              foreach ( $icecakes as $post ) : setup_postdata( $post ); ?>
                  <li>
                      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                  </li>
              <?php endforeach; 
              wp_reset_postdata(); ?>

Need Your Help

How to align TH Header with TD in TBody

html css

I'm having problems trying to embed a table in an existing HTML page with some CSS.

VisualVM over ssh

ssh visualvm

I've read Visual VM remotely over ssh but I think I've not fully understood because it was not working for me :-( Please can someone give some example?