2012-09-26 58 views
0

有人可以幫我下面的代碼。它工作正常,但我不想爲每個類別編寫一段代碼。所以基本上,我希望有父類別顯示一次,以便列出它們下面的所有帖子。WordPress的羣組按類別發佈

我知道我需要一個for循環,但不知道它是如何寫的。

<h4>2012</h4> 
    <ul class="news-coverage"> 
    <?php $cat_id = get_cat_ID('2012-media'); 
    $args=array(
     'cat' => $cat_id, 
     'post_type' => 'press', 
     'post_status' => 'publish', 
     'posts_per_page' => 100, 
     'caller_get_posts'=> 1, 
     'exclude' => 28, 
     'offset' => 9 

    ); 
    $new = new WP_Query($args); 
    $loop = new WP_Query($args); 
    while ($loop->have_posts()) : $loop->the_post(); 
    echo '<li><i class="foundicon-page"></i><strong>'; 
     the_title();echo '</strong><span>'; 
     the_date(); echo '</span>'; 
     the_content(); 
    echo '</li>'; 
    endwhile; ?> 

    </ul> 

    <h4>2011</h4> 
        <hr> 
    <ul class="news-coverage"> 
    <?php $cat_id = get_cat_ID('2011-media'); 
    $args=array(
     'cat' => $cat_id, 
     'post_type' => 'press', 
     'post_status' => 'publish', 
     'posts_per_page' => -1, 
     'caller_get_posts'=> 1, 
     'exclude' => 28 
    ); 
    $new = new WP_Query($args); 
    $loop = new WP_Query($args); 
    while ($loop->have_posts()) : $loop->the_post(); 
    echo '<li><i class="foundicon-page"></i><strong>'; 
     the_title();echo '</strong><span>'; 
     the_date(); echo '</span>'; 
     the_content(); 
    echo '</li>'; 
    endwhile; ?> 

    </ul> 
+0

您可能會發現在[wordpress.stackexchange.com]更多的幫助(http://wordpress.stackexchange.com)。 –

+0

無需簽署您的問題。所以每次發佈時都會自動進行。 – Stephan

+1

@Stephan這是維基百科條件 – feeela

回答

0

你去那裏(未測試)

<?php 
/* add category prefixes here */ 
$years = array('2011', '2012'); 

$output = ''; 

/* iterate over categories */ 
foreach($years as $year) 
{ 
    /* add the heading */ 
    $output .= sprintf('<h4>%s</h4>', $year); 

    /* setup current query/loop */ 
    $args = array(
     'cat' => get_cat_ID($year . '-media'), 
     'post_type' => 'press', 
     'post_status' => 'publish', 
     'posts_per_page' => 100, 
     'caller_get_posts' => 1, 
     'exclude' => 28, 
     'offset' => 9 
    ); 
    $loop = new WP_Query($args); 

    /* iterate over articles */ 
    $listItems = ''; 
    while($loop->have_posts()) 
    { 
     $loop->the_post(); 
     $listItems .= sprintf('<li><i class="foundicon-page"></i><strong>%s</strong><span>%s</span>%s</li>', 
      get_the_title(), 
      get_the_date(), 
      get_the_content() 
     ); 
    } 

    $output .= sprintf('<ul class="news-coverage">%s</ul>', $listItems); 
} 

echo $output; 
?> 
+0

嘿謝謝你的幫助! :) – Jenny

+0

你介意如果我問另一個問題,我將如何列出類別名稱和年份從自定義帖子類型所有類別的帖子? – Jenny

相關問題