2015-09-16 72 views
2

我想創建一個存檔頁面,以此順序顯示帖子:使用wordpress創建存檔頁面

post_title | post_date | post_category

Offcourse這應該是點擊鏈接的帖子,或者如果你點擊某個類別比它應該鏈接到一個類別

什麼我到目前爲止有:

$args = array(
     'post_type' => 'post' 
    ); 

    $post_query = new WP_Query($args); 

    if($post_query->have_posts()) { 
     while($post_query->have_posts()) { 
     $post_query->the_post(); 
     ?> 
     <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Link naar <?php the_title_attribute(); ?>"><?php the_title(); ?></a> | <?php the_date();?> | <?php get_the_category;?></h2> 
     <?php 
     } 
    } 
    ?> 

我知道, 「the_permalink()」給了我發佈的鏈接,「the_title」給了我標題。

此代碼僅顯示最近10個職位,而不是3000

另一個問題是,日期只顯示了對10個職位2。

該類別根本沒有顯示出來。

這是我第一次嘗試真正與wordpress合作,也許我真的這樣做是錯誤的方式,所以我希望你們都能幫助我。預先感謝。

回答

1

您可以控制的職位數(比如50),像這樣:

$args = array(
    'post_type' => 'post', 
    'posts_per_page' => 50 
); 

日期和類別顯示了使用此:

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Link naar <?php the_title_attribute(); ?>"><?php the_title(); ?></a> | 
<?php 
    echo get_the_date(); 
    echo ' | '; 
    $category_list = get_the_category_list(', '); 
    if ($category_list) 
     echo $category_list; 
?> 
</h2> 

希望有所幫助。如果不是隨意問的話!

+1

感謝這個作品真的很好。通過這種方式,我可以繼續構建自定義存檔 – user3398922

2

此代碼僅顯示最近10個職位,而不是3000

這是正常的WordPress的行爲。默認情況下,如果您未指定posts_per_pagenumberposts參數至get_posts,它將使用設置>閱讀中設置的值。因此,改變你的參數傳遞給這個(-1意味着它會顯示你所有的訊息 - 將其更改爲3000,如果你確實需要它是3000):

$args = array(
    'post_type' => 'post', 
    'numberposts' => -1 
); 

的另一個問題是,日期只顯示了2的10個職位。

奇怪的是,這太默認的WordPress的行爲(在我看來有點混亂)。如果你看一下在the_date文檔頁面的特別注意,它會告訴你:

如果有, the_date()當天在發表一個頁面上的多個崗位只顯示後的第一個

日期

如果你想在這個和所有帖子顯示的日期,你需要使用get_the_date(它返回日期,所以你需要echo它)。

該分類根本沒有顯示出來。

你誤用get_the_category - 它返回類對象數組相關的帖子,並沒有什麼echo。要顯示鏈接到目前的職位類別,你需要使用的get_the_categoryget_category_link組合:

$category = get_the_category(); 
echo '<a href="' . get_category_link($category[0]->cat_ID) .'">' . $category[0]->cat_name . '</a>'; 
+0

請注意,如果您不介意使用列表標記獲取所有類別,則類別的@Karthik回答也可以使用(使用'get_the_category_list')。 – vard