EDIT: This used to be

How do I force a page to be
is_archive == true?

I was trying to solve the wrong problem. Instead of forcing the post/page to have a particular attribute, I should have looked at the classes applied to the menu item. The ‘Blog’ menu-item has the current-page-ancestor class applied to it. I can style the menu based on this.

ORIGINAL QUESTION:

I have my own theme where I created an archives.php template in order to have a page that tries to list the different ways to view archives and to give a place for users to browse the archives if they want to.

I created a page using the WP admin page and I made it’s parent the ‘Blog’ page. The ‘Blog’ page is the page where all my posts get emitted. I have a menu at the top and right now, every sub-page under the ‘Blog’ page causes the menu to highlight ‘Blog’ (this is the correct behavior). Specifically, the menu item has the style current_page_* applied to it. This doesn’t happen when you load blog/archives (the template/page I created in the first paragraph).

So when I click on the date of a blog post, it brings me to an archived post page it looks like this (note that the Blog menu item is highlighted):

Archived Date Posts

When I go to my archives, it looks like this (note that the menu item is NOT highlighted):

Archives Page

I looked at $wp_query to see what the difference was. I saw that is_archive or is_home was true in the cases where the menu-item was correctly styled.

How do I force my page to be is_archive == true? I tried to insert a $wp_query->set('is_archive', 1); before the call to get_header(); in my template but that didn’t work. I also looked in the WP admin dashboard to see if there was something I could click to force the page to be considered an archive.

Edit: To be clear, I’m looking for a WP solution. I know that I can hard-code the CSS to get the menu item to appear the way I want it to be, but that doesn’t seem to be the right thing to do.

5 Answers
5

The archives.php template file is a custom page template file for a static Page. The is_archive() and is_home() conditionals apply to a blog posts archive and blog posts index, respectively.So, you’re never going to get is_archive() to return true while displaying the output of archives.php.

To have your archives.php static Page appear in the menu:

  1. Create a new static Page, named whatever, e.g. “Archives” (but it is completely arbitrary)
  2. Assign the archives.php custom Page template to this static Page
  3. Add this static Page to your custom Nav Menu

Then, you should see it highlighting properly in the menu.

Leave a Reply

Your email address will not be published. Required fields are marked *