Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Wordpress archive.php

Featured Replies

Hi all,

 

This page uses a custom template to display only category 1 posts in the main content area and a custom sidebar to show only category 1 posts in the sidebar's 'recent articles'. I have also edited archive.php, so that when 'archives June 2010' is clicked, only category 1 posts archives are displayed.

 

So that all works fine but I have another page which displays only category 3 posts and has a corresponding sidebar3.php.

 

So I have added an 'archive3.php' which will display only category 3 archives but how do I associate the archives links in the sidebar3 of category 3 template page with 'archive3.php' instead of archive.php? I want the archives links in sidebar3 to display the archive for category 3 posts but at the moment, clicking 'archives June 2010' on my category 3 template page sidebar will display the archive for category 1. :mellow:

 

thanks for any suggestions :)

Would you be open to taking the user to a Category Page instead? If you think about it, a Category Page is an archive of posts from that specific category. Only thing you would miss out on, is having separate links for each month.

  • Author

Would you be open to taking the user to a Category Page instead? If you think about it, a Category Page is an archive of posts from that specific category. Only thing you would miss out on, is having separate links for each month.

 

 

Thanks for your reply. I am not bothered about having 'separate links for each month' in the sidebar if that is what you mean, but I think the archive page would need separate months. This is what a category page looks like at the moment so I think after a year or two of posts there would be a horrendously long list. Would it be possible for me to get at the template of the category page to change how it displays? I have heard of a file called category.php but this theme does not have this file. :mellow:

 

thanks

  • Author

PS I notice that when I hit the 'archives June 2010' link here with archive.php unaltered I get the page titled 'Archive for June 2010' listing the posts from all categories in June 2010. When I edit archive.php by adding...

 

 <?php query_posts('cat=1'); ?>

 

...the same link ('archives June 2010') gives the page titled 'Posts from the ‘Football’ Category' - displaying only cat 1 posts but of any date (ie changes from archive page to category page). :mellow:

  • Author

Ok I have a plan...

 

My theme has a page template (ie selectable in 'edit page') called 'archives.php' which displays like this. So I think all I need to do is to add 2 new pages, upload 2 copies of 'archives.php' then rename and edit to display cat 1 archive and cat 3 archive respectively, and attach template to the relevant page. Then remove these 2 new page links from the nav bar and place them instead in the relevant sidebar...

 

archives.php =

 

<?php
/*
Template Name: Archives
*/
?>
<?php get_header(); ?>
<?php query_posts('showposts=25'); ?>
<?php if (have_posts()) : ?>
	<h1 class="pagetitle"><?php bloginfo('title'); ?> <?php _e('Archives', 'titan'); ?></h1>
	<div class="entry no-indent">
		<h2><?php _e('Recent Posts', 'titan'); ?></h2>
	</div><!--end-entry-->
<?php while (have_posts()) : the_post(); ?>
	<div class="entries">
		<ul>
			<li><span><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php printf(__ ('%s on', 'titan'), get_the_title()); ?></a> <?php the_time(__ ('F jS, Y', 'titan')); ?></span></li>
		</ul>
	</div><!--end entries-->
<?php endwhile; endif; ?>
<div class="entry no-indent">
	<h2><?php _e('Monthly Archives', 'titan'); ?></h2>
	<ul>
		<?php wp_get_archives('type=monthly&show_post_count=1'); ?>
	</ul>
</div><!--end-entry-->
</div><!--end content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

That would be the hard way to do it, yes.

 

Do you have any experience with PHP? I ask because what I'm about to suggest will require you to know at least the basics.

  • Author

That would be the hard way to do it, yes.

 

Do you have any experience with PHP? I ask because what I'm about to suggest will require you to know at least the basics.

Well I am learning PHP. :unsure: So please make your suggestion and in the meantime I am working on my 'hard way' mentioned above.

 

thanks :)

  • Author

OK here is my new page with the custom archive template (archive1.php). It correctly displays...

 

'Monthly Archives

 

* July 2010 (1)

* June 2010 (10)'

 

 

When I click the link 'June 2010' the page says 'Archive for June, 2010' but it displays the archive for all categories.

 

Any suggestions please for how to change the code below (archive1.php) so the 'June' etc links display only cat 1 posts??

 

 

 

 <?php
/*
Template Name: archive1
*/
?>
<?php get_header(); ?>


<div class="entry no-indent">
	<h2><?php _e('Monthly Archives', 'titan'); ?></h2>
	<ul>
		<?php wp_get_archives('type=monthly&show_post_count=1'); ?>
	</ul>
</div><!--end-entry-->

Yea, this plugin sounds like exactly what you're after.

 

Now what you need to do is find out how you can input the category dynamically, so if you're in category 3, the archives will automatically know that, and show archives from category 3.

 

To do so you have to check what category you're in, and set it as a variable.

 

<?php

$categoryid = get_the_category();

wp_get_archives('cat=' . $categoryid[0]->cat_ID);

?>

 

On line 5, you can see the code form the plugin, so make sure it's activated.

 

As far as I know, this will only work if you have posts under no more than one category.

 

Hope this helps!

  • Author

OK I have installed the plugin. As mentioned above the archives.php page template has

 

<?php wp_get_archives('type=monthly&show_post_count=1'); ?>      

 

When I replace this line with

 

<?php wp_get_archives('cat=1'); ?>

 

the plugin works ok and the 'June 2010' link gives the archive for cat 1 only.

 

But if I instead replace it with...

 

<?php

$categoryid = get_the_category();

wp_get_archives('cat=' . $categoryid[0]->cat_ID);

?>

 

...this doesn't work and the the 'June 2010' link gives the archive for all cats.

 

Is this meant to save me the trouble of having two separate archive page templates? Please let me know any suggestions but in the meantime I will continue with the long way of 2 separate archive page templates.

 

thanks :)

  • Author

Ok I have done it the hard way with 2 different archive.php's. This page is the cat 1 page template and its sidebar archive link now gives only the cat 1 archive while this page is the cat 3 page template and its sidebar archive link now gives only the cat 3 archive.

 

One problem left...

 

When I hit the cat 1 archives link in the cat 1 template sidebar I get this page which is structured by archive1.php and displays correctly showing June 2010, July 2010 etc and with the correct sidebar ie my custom sidebar1.php. But when I click eg June 2010 I go to another page which shows the correct posts in the main content area but has sidebar.php (ie the original) instead of my custom sidebar1.php. So how can I change the sidebar for this page?? :unknw:

 

I have the same problem for a single post. This is a cat 1 post and so I want it to display with sidebar1.php not the default / original sidebar.php. I guess this is presently governed by the file 'single.php' which has the line

 

<?php get_sidebar(); ?> 

So I want cat 1 and cat 3 single posts to display together with sidebar1.php and sidebar3.php respectively.

 

:(

 

 

Thanks for any suggestions

where you have

 <?php get_sidebar(); ?>

 

change to

 <?php include bloginfo('template_directory'); ?>/filename.php ?>

  • Author

where you have

 <?php get_sidebar(); ?>

 

change to

 <?php include bloginfo('template_directory'); ?>/filename.php ?>

Thanks I changed but no difference :(

The code I have provided you with should go into sidebar.php

 

The links in the sidebar archives will only show links to the pages using the category you are inside of. For example:

 

I am looking at a page, which has posts from category ID 1. The sidebar archive list will automatically know, and will display yearly, monthly, weekly, or daily archives, only from category ID 1.

 

Read more about the wp_get_archives function here.

 

Hope that make's sense. You only need one sidebar file -> sidebar.php, in which you place the code I gave you earlier, to list the archives.

 

:)

  • Author

 

As far as I know, this will only work if you have posts under no more than one category.

OK thanks but I am not sure what you mean by this. Anyway I now changed to only one sidebar template instead of 2 = sidebar.php, in which I replaced

 

 <?php wp_get_archives( 'type=monthly'); ?> 

 

with...

 

<?php

$categoryid = get_the_category();

wp_get_archives('cat=' . $categoryid[0]->cat_ID);

?>

 

The archive links in the sidebar have changed in that they no longer display links from all cats. However instead of adjusting dynamically so that the cat 1 template page sidebar links to cat 1 archives and the cat 3 template page sidebar links to cat 3 archives, both the cat 1 template page and the cat 3 template page sidebars now link to cat 1 only archives. <_<

 

 

Any suggestions please??

  • Author

where you have

 <?php get_sidebar(); ?>

 

change to

 <?php include bloginfo('template_directory'); ?>/filename.php ?>

 

Suggestion for all - thanks for all your help but when giving code changes maybe mention the relevant template so we are all on the same page. B)

What I mean by that is that your post should only be in category 1. Not category 1 and 3.

 

@Juc1 - I'm really sorry, but at this point, I'm all out of ideas. My last solution should be working, since it's dynamic.

 

If you're wondering, what it does, is, it checks for the ID of the post you're reading, and the puts it into the cat of the archives list.

 

Last thing you can do, is see if it knows which category it's in, by swapping the code in your sidebar.php to this:

 

<?php

$categoryid = get_the_category();

echo $categoryid[0]->cat_ID);

?>

 

This will simply show the number ID of the category it's getting. Let me know if both pages show 1, or if it's 1 and 3.

 

Thanks.

  • Author

<?php

$categoryid = get_the_category();

echo $categoryid[0]->cat_ID);

?>

 

Sorry for being thick but can you please tell me where in sidebar.php I should put this code? I tried a few things but the result was no sidebar. My current sidebar.php =

 

<?php global $titan; ?>
<div id="sidebar">
<?php if ($titan->sideboxState() != 'true' ){ ?>
	<div id="sidebox">
		<?php if ($titan->sideboxCustom() == 'true' ): ?>
			<?php echo $titan->sideboxCode(); ?>
		<?php else : ?>
			<a href="/"><img src="<?php bloginfo( 'template_url'); ?>/images/sidebar/sidebox.jpg" width="236" height="236" alt="Titan WordPress Theme" /></a>
		<?php endif; ?>
	</div><!--end sidebox-->

<?php } ?>
<?php if ( is_active_sidebar( 'normal_sidebar' )) echo "<ul>" ?>
<?php if ( !function_exists( 'dynamic_sidebar')|| !dynamic_sidebar( 'normal_sidebar' )) : ?>
	<ul>
		<li class="widget widget_recent_entries">
			<h2 class="widgettitle"><?php _e( 'Recent Articles', 'titan'); ?></h2>
			<ul>
				<?php $side_posts = get_posts( 'numberposts=10'); foreach($side_posts as $post) : ?>
				<li><a href= "<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
				<?php endforeach; ?>
			</ul>
		</li>
		<li class="widget widget_categories">
			<h2 class="widgettitle"><?php _e( 'Categories', 'titan'); ?></h2>
			<ul>
				<?php wp_list_cats( 'sort_column=name&hierarchical=0'); ?>
			</ul>
		</li>
		<li class="widget widget_archive">
			<h2 class="widgettitle"><?php _e( 'Archives', 'titan'); ?></h2>
			<ul>

<?php

$categoryid = get_the_category();

wp_get_archives('cat=' . $categoryid[0]->cat_ID);

?>

		</ul>
		</li>
	</ul>
<?php endif; ?>
<?php if ( is_active_sidebar( 'normal_sidebar' )) echo "</ul>" ?>

</div><!--end sidebar-->

  • Author

What I mean by that is that your post should only be in category 1. Not category 1 and 3.

 

OK well all my posts are only one category = either 1 or 3.

 

 

@Juc1 - I'm really sorry, but at this point, I'm all out of ideas.

 

OK if it doesn't work, no problemo. I can still go back to the less cool way of having 2 sidebars.

 

thanks for your help:)

Yea, seems like the multiple sidebars will have to do <_<

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.