2
votes

I want to create an archive page that display's the posts in this order:

post_title | post_date | post_category

Offcourse this should be clickable that links to the post or if you click on a category than it should link to a category

What do I have so far:

    $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
      }
    }
    ?>

I know that "the_permalink()" gives me the link to post and "the_title" gives me the title.

This code only shows the last 10 posts instead of the 3000.

Another problem is that the date only shows up on 2 of the 10 posts.

The category just doesn't show up at all.

This is the first time i'm trying to really work with wordpress and perhaps i'm really doing it the wrong way so I hope you all can help me out. Thank in advance.

2

2 Answers

2
votes

This code only shows the last 10 posts instead of the 3000.

That's normal Wordpress behaviour. By default if you don't specify posts_per_page or numberposts parameters to get_posts it will use the value set in Settings > Reading. So change your args to this (-1 means it will display all your posts - change it to 3000 if you actually need it to be 3000):

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

Another problem is that the date only shows up on 2 of the 10 posts.

That, oddly, is too a default Wordpress behaviour (in my opinion a bit confusing). If you look at the special note on the_date documentation page, it tells you that:

When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post

If you want to get over this and display the date for all posts, you need to use get_the_date (it return the date so you'll need to echo it).

The category just doesn't show up at all.

You're miss-using get_the_category - it returns an array of categories objects associated to the post and doesn't echo anything. To display the link to the current post category, you need to use a combination of get_the_category and get_category_link:

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

You can control number of posts (say 50) like so:

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

For date and categories to show up Use this:

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

Hope that helps. If not feel free to ask!