Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need to make a list of posts and archive for 1 category WordPress

  • SOLVED

I have built a site for a client who wanted a site built in a specific way and they have now come back looking for some changes that I'm a bit stuck on.

As the site was built in wordpress 2.9 I used Flutter to create some custom write panels and used post categories to allow different types of content to be added. One of the the write panels was for news articles. Currently the site only lists 5 articles on the homepage that the they can manually order.

Current site : [[LINK href="http://www.thisismoda.com"]][[/LINK]]

What the client wants to do now is to have a news page and archive that acts pretty much like the wordpress index.php out the box. So when you click on "news" in the top bar you will get a date archive down the left hand side and the list of all the latest articles in the main content area. Clicking on a date like like "August 2010" will give you articles posted in that month etc.

I have started trying to tackle the problem myself by creating a category page e.g. category-4.php with the code below. I used flutter to allow the admin to upload a thumbnail image and also order items, although the manual ordering will not be used here just the homepage.

You can see the code for this page here :

<?php get_header(); ?>
<div id="side">
<h2>Archives</h2>
<ul>
<?php wp_get_archives(); ?>
</ul>
<div id="slideshow">
<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/anniemac.jpg" alt="Annie Mac DJing image" />
<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/crowdshot.jpg" alt="Crowd Shot image" />
</div>
</div>

<div id="central">
<?php
//Revised query string
$querystr2 = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wposts.post_status = 'publish'
AND wpostmeta.meta_key = 'order'
AND wpostmeta.meta_value > 0
ORDER BY wpostmeta.meta_value ASC";

$postslist = $wpdb->get_results($querystr2);


// $postslist = get_posts('numberposts=3&order=DESC&orderby=ID&category=-3');
foreach ($postslist as $post) :
setup_postdata($post);
?>

<h3><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h3>
<? the_time('d/m/y');?>
<? if(get_image('Thumbnail') != NULL): ?>
<a href="<?php the_permalink();?>"><? echo "<img src='" . jt_get_thumb("Thumbnail",1,1,false,"w=100&h=100&zc=1",false) . "' class='Thumb' />"; ?></a>
<? endif; ?>
<p><?php the_excerpt(); ?></p>
<p><em><a href="<?php the_permalink();?>">Read More...</a></em></p>
<br class="clear" />
<?php endforeach; ?></div>

<br class="clear" />
<?php get_footer(); ?>


As you will see the problem I am having is that the archive is not sorting properly, I know the archive.php will need edited in someway too.

<strong>So here is my question...</strong>

How can I create a latest news index page for the news category (id 4) and have it display the latest articles and also filtered by months ?

Answers (1)

2010-09-08

Denzel Chia answers:

Hi,

Is this what you are looking for?

<?php
//Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
//posts for March 1 to March 15, 2009
$where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";
return $where;
}
// Register the filtering function
add_filter('posts_where', 'filter_where');
// Perform the query, the filter will be applied automatically
query_posts("cat=4&order=ASC");
while(have_posts()) { the_post();
<!-- put your loop here -->
}
?>


You can add in other query variables such as number of posts display.
Please take a look http://codex.wordpress.org/Function_Reference/query_posts for more details.

Thanks.


Andrew Cetnarskyj comments:

Ok this is a start, this would pull all the content for the items in the category, but what I need is to be able do is filter the archive for the category also.


Andrew Cetnarskyj comments:

I tried your code but found that as I am using category-4.php as my page the list is already filtered.

Again the main thing I'm stuck with is creating a month based date archived for that single category, category 4.

I am open to using plugins, I am using WP 2.9.


Denzel Chia comments:

Hi,

1) you can use this plugin http://scribu.net/wordpress/smart-archives-reloaded
2) Install and activate this plugin
3) To generate a list of categories filtered by archive month,
Use category name instead of id

4) remove the filter function from my above example.

5) create a template called home.php (if you want his to be the homepage)
and put the following code in, and of course with your other HTML elements if you found this code to be working.


<div id='sidebar'>
<?php smart_archives('format=list', 'category_name=your category name'); ?>
</div>

<?php
query_posts("cat=4&order=ASC");
while(have_posts()) { the_post();
<!-- put your loop here -->
}
?>


Andrew Cetnarskyj comments:

Hi Denzel,

We are almost there and you are in the running for the cash. There is just one final thing.

I have installed the smart archive and the wp-navi plugin from same dev to page the results.

There is just one final thing that isn't working from your suggestion and that is the archive page.

I have this from my code in archive.php


<?php get_header(); ?>

<div id="side">
<h2>Article Archives</h2>
<ul>
<?php smart_archives('format=list', 'category_name=general_news'); ?>
</ul>

<div id="slideshow">
<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/anniemac.jpg" alt="Annie Mac DJing image" />
<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/crowdshot.jpg" alt="Crowd Shot image" />
</div>
</div>

<div id="central">
<?php if (have_posts()) : ?>

<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h1><?php single_cat_title(); ?>&#8217; Articles</h1>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<h1>Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;</h1>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h1>Archive for <?php the_time('F jS, Y'); ?></h1>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h1>Archive for <?php the_time('F, Y'); ?></h1>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h1>Archive for <?php the_time('Y'); ?></h1>
<?php /* If this is an author archive */ } elseif (is_author()) { ?>
<h1>Author Archive</h1>
<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
<h1>Blog Archives</h1>
<?php } ?>

<?php
query_posts("cat=4&order=ASC");
while (have_posts()) : the_post(); ?>
<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<p>Posted on <?php the_time('l, F jS, Y') ?></p>
<?php the_excerpt() ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">Read more...</a>
<p><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?></p>

<?php endwhile; ?>

<hr />
<?php wp_pagenavi(); ?>

<?php else : ?>

<h2>Not Found</h2>
<?php include (TEMPLATEPATH . '/searchform.php'); ?>

<?php endif; ?>
</div>
<br class="clear" />
<?php get_footer(); ?>


Here is the page for march 2010 archive : [[LINK href="http://www.thisismoda.com/2010/03"]][[/LINK]]

I tried looking up the codex and can't see what I'm missing.

Thanks for your help so far.


Denzel Chia comments:

Hi,

If you want the archive.php list of posts to reflect from the smart_archives list, remove the query_posts for category. I had also changed your WordPress Loop. I tested in my localhost, with different categories.

Please try the below code;


<?php get_header(); ?>

<div id="side">

<h2>Article Archives</h2>

<ul>

<?php smart_archives('format=list', 'category_name=general_news'); ?>

</ul>


<div id="slideshow">

<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/anniemac.jpg" alt="Annie Mac DJing image" />

<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/crowdshot.jpg" alt="Crowd Shot image" />

</div>

</div>


<div id="central">


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>

<?php /* If this is a category archive */ if (is_category()) { ?>

<h1><?php single_cat_title(); ?>&#8217; Articles</h1>

<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>

<h1>Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;</h1>

<?php /* If this is a daily archive */ } elseif (is_day()) { ?>

<h1>Archive for <?php the_time('F jS, Y'); ?></h1>

<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>

<h1>Archive for <?php the_time('F, Y'); ?></h1>

<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>

<h1>Archive for <?php the_time('Y'); ?></h1>

<?php /* If this is an author archive */ } elseif (is_author()) { ?>

<h1>Author Archive</h1>

<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>

<h1>Blog Archives</h1>

<?php } ?>




<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

<p>Posted on <?php the_time('l, F jS, Y') ?></p>

<?php the_excerpt() ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">Read more...</a>

<p><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?></p>



<?php endwhile; ?>



<hr />

<?php wp_pagenavi(); ?>



<?php else : ?>



<h2>Not Found</h2>

<?php include (TEMPLATEPATH . '/searchform.php'); ?>



<?php endif; ?>

</div>

<br class="clear" />

<?php get_footer(); ?>


Just make sure that the archive links on other templates, such as category, single, any template that you list the archive links are using smart_archive links.

Clicking on this smart_archive link will lead to the above archive.php and a list of archive will should accordingly. The smart_archive links on top will serve as something like a menu.


Denzel Chia comments:

One suggestion,

If you are only showing the archive title of the page once, you should remove it from the WordPress Loop.




<?php get_header(); ?>

<div id="side">

<h2>Article Archives</h2>

<ul>
<?php smart_archives('format=list', 'category_name=general_news'); ?>

</ul>


<div id="slideshow">

<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/anniemac.jpg" alt="Annie Mac DJing image" />

<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/crowdshot.jpg" alt="Crowd Shot image" />

</div>

</div>


<div id="central">


<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>

<?php /* If this is a category archive */ if (is_category()) { ?>

<h1><?php single_cat_title(); ?>&#8217; Articles</h1>

<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>

<h1>Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;</h1>

<?php /* If this is a daily archive */ } elseif (is_day()) { ?>

<h1>Archive for <?php the_time('F jS, Y'); ?></h1>

<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>

<h1>Archive for <?php the_time('F, Y'); ?></h1>

<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>

<h1>Archive for <?php the_time('Y'); ?></h1>

<?php /* If this is an author archive */ } elseif (is_author()) { ?>

<h1>Author Archive</h1>

<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>

<h1>Blog Archives</h1>


<?php } ?>


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>



<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>



<p>Posted on <?php the_time('l, F jS, Y') ?></p>



<?php the_excerpt() ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">Read more...</a>



<p><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?></p>




<?php endwhile; ?>



<hr />


<?php wp_pagenavi(); ?>


<?php else : ?>


<h2>Not Found</h2>

<?php include (TEMPLATEPATH . '/searchform.php'); ?>


<?php endif; ?>

</div>

<br class="clear" />

<?php get_footer(); ?>




Andrew Cetnarskyj comments:

Hi,

I tried your second suggestion and it is almost there however all of the categories are listed now on the archive page not just my news category (id 4).

Page : [[LINK href="http://www.thisismoda.com/2010/08"]][[/LINK]]

This is what I have as the code :

<?php get_header(); ?>

<div id="side">
<h2>Article Archives</h2>
<ul>
<?php smart_archives('format=list', 'category_name=general_news'); ?>
</ul>

<div id="slideshow">
<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/anniemac.jpg" alt="Annie Mac DJing image" />
<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/crowdshot.jpg" alt="Crowd Shot image" />
</div>
</div>






<div id="central">
<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h1><?php single_cat_title(); ?>&#8217; Articles</h1>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<h1>Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;</h1>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h1>Archive for <?php the_time('F jS, Y'); ?></h1>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h1>Archive for <?php the_time('F, Y'); ?></h1>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h1>Archive for <?php the_time('Y'); ?></h1>
<?php /* If this is an author archive */ } elseif (is_author()) { ?>
<h1>Author Archive</h1>
<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
<h1>Blog Archives</h1>
<?php } ?>
<?php
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<p>Posted on <?php the_time('l, F jS, Y') ?></p>
<?php the_excerpt() ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">Read more...</a>
<p><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?></p>
<?php endwhile; ?>

<hr />
<?php wp_pagenavi(); ?>

<?php else : ?>

<h2>Not Found</h2>
<?php include (TEMPLATEPATH . '/searchform.php'); ?>

<?php endif; ?>
</div>
<br class="clear" />
<?php get_footer(); ?>


Denzel Chia comments:

Hi,

I assume that cat 4's category_name is general_news.

I updated the code to filter the date and month before querying database for posts and display on archive.php

Therefore clicking on the smart_archive links will show the list of post corresponding to this link.
For example, clicking on smart_archive link for September 2010 will show a list of post in category general news for September 2010.



<?php get_header(); ?>

<div id="side">

<h2>Article Archives</h2>

<ul>

<?php smart_archives('format=list', 'category_name=general_news'); ?>

</ul>

<div id="slideshow">

<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/anniemac.jpg" alt="Annie Mac DJing image" />

<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/crowdshot.jpg" alt="Crowd Shot image" />

</div>

</div>

<div id="central">

<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>

<?php /* If this is a category archive */ if (is_category()) { ?>

<h1><?php single_cat_title(); ?>&#8217; Articles</h1>

<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>

<h1>Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;</h1>

<?php /* If this is a daily archive */ } elseif (is_day()) { ?>

<h1>Archive for <?php the_time('F jS, Y'); ?></h1>

<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>

<h1>Archive for <?php the_time('F, Y'); ?></h1>

<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>

<h1>Archive for <?php the_time('Y'); ?></h1>

<?php /* If this is an author archive */ } elseif (is_author()) { ?>

<h1>Author Archive</h1>

<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>

<h1>Blog Archives</h1>

<?php } ?>

<?php

function post_filter_where($where = '') {
$year = get_the_time('Y');
$month = get_the_time('n');
//filter according to year and month pass into template
$where .= " AND post_date >= '$year-$month-01' AND post_date < '$year-$month-32'";
return $where;
}

// Register the filtering function
add_filter('posts_where', 'post_filter_where');

// Perform the query, the filter will be applied automatically
query_posts('category_name=general_news');

if (have_posts()) : while (have_posts()) : the_post(); ?>

<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

<p>Posted on <?php the_time('l, F jS, Y') ?></p>

<?php the_excerpt() ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">Read more...</a>

<p><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?></p>

<?php endwhile; ?>


<hr />

<?php wp_pagenavi(); ?>



<?php else : ?>


<h2>Not Found</h2>

<?php include (TEMPLATEPATH . '/searchform.php'); ?>



<?php endif; ?>

</div>

<br class="clear" />

<?php get_footer(); ?>




Hope this is what you want.

Thanks.


Andrew Cetnarskyj comments:

Hi,

I implemented your code and it works for articles posted in 2009 as below :

http://www.thisismoda.com/2009/11

However, when you try it on the 2010 pages it pulls back everything :

http://www.thisismoda.com/2010/05

Is it something in the filter code ?

Sorry I haven't delved into WP custom queries too much.


Denzel Chia comments:

Hi,

I added reset query function and changed the date to <= year-month-31 instead of < year month 32.

Should be my mistake in the date.

Posted the changes here.


<?php get_header(); ?>

<div id="side">

<h2>Article Archives</h2>

<ul>

<?php smart_archives('format=list', 'category_name=general_news'); ?>

</ul>

<div id="slideshow">

<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/anniemac.jpg" alt="Annie Mac DJing image" />

<img class="bottomspacer" src="<?php bloginfo('stylesheet_directory'); ?>/layoutimages/crowdshot.jpg" alt="Crowd Shot image" />

</div>

</div>

<div id="central">

<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>

<?php /* If this is a category archive */ if (is_category()) { ?>

<h1><?php single_cat_title(); ?>&#8217; Articles</h1>

<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>

<h1>Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;</h1>

<?php /* If this is a daily archive */ } elseif (is_day()) { ?>

<h1>Archive for <?php the_time('F jS, Y'); ?></h1>

<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>

<h1>Archive for <?php the_time('F, Y'); ?></h1>

<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>

<h1>Archive for <?php the_time('Y'); ?></h1>

<?php /* If this is an author archive */ } elseif (is_author()) { ?>

<h1>Author Archive</h1>

<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>

<h1>Blog Archives</h1>

<?php } ?>

<?php

function post_filter_where($where = '') {

$year = get_the_time('Y');

$month = get_the_time('n');

//filter according to year and month pass into template

$where .= " AND post_date >= '$year-$month-01' AND post_date <= '$year-$month-31'";

return $where;

}



// Register the filtering function

add_filter('posts_where', 'post_filter_where');


// Perform the query, the filter will be applied automatically

query_posts('category_name=general_news');

if (have_posts()) : while (have_posts()) : the_post(); ?>

<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

<p>Posted on <?php the_time('l, F jS, Y') ?></p>


<?php the_excerpt() ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">Read more...</a>

<p><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?></p>

<?php endwhile; ?>


<hr />


<?php wp_pagenavi(); ?>


<?php else : ?>


<h2>Not Found</h2>

<?php include (TEMPLATEPATH . '/searchform.php'); ?>


<?php endif; ?>

//Reset Query
<?php wp_reset_query(); ?>


</div>


<br class="clear" />


<?php get_footer(); ?>


Sorry for inconveniences.

Thanks.


Andrew Cetnarskyj comments:

Great thanks for your help that worked.

Could you just explain your query in case I have to tweak it and what the reset does ?