2012-11-02 21 views
8

Ho il mio tema e mi piacerebbe visualizzare i messaggi sulla mia home page da una categoria specifica.Come posso ottenere i post dalla categoria usando lo slug?

Finora ho raggiunto in questo modo:

<?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; 
?> 

Ma cosa succede se voglio ottenere la categoria da un suo lumaca? Oppure è possibile semplicemente creare una casella di selezione di categoria all'interno del pannello di amministrazione?

risposta

24

sostituire il tuo parametro di categoria con CATEGORY_NAME

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; 

per ulteriori informazioni controllare questo link http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

+0

@IoQ come è possibile utilizzare l'impaginazione con questo codice? – Amin

+0

Grazie mille, mi hai fatto risparmiare tempo – Bellash

2

si supponga di avere categoria 'torte gelato' nome e la categoria lumaca come 'ghiaccio-cakes', allora il nostro codice per recuperare il post nella categoria "torte ghiacciate" è il seguente:

<?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(); ?> 
Problemi correlati