2

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.

2
  • Is all your bothered about is showing the blog tab being highlighted, when on an archive page? Jun 16, 2011 at 9:50
  • Yes. This is all I am bothered about. I suppose a simple way to rephrase this is that my custom-template page isn't recognized as an archive page. Jun 16, 2011 at 10:11

5 Answers 5

1

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.

1
  • I don't need the static page to appear in the menu. I need the menu item 'Blog' to be highlighted. Perhaps I need to hook into the menu code for this? I'll clarify in my question this point... I'm communicating poorly. :( Jun 16, 2011 at 13:10
1

As per the change in the question, the answer to this is:

  1. Look at the css classes that are applied to the menu-item of interest.
  2. The menu-item which this page should be a child of should have current-page-ancestor applied to it.
  3. Make the appropriate changes in your styling sheet to style the menu when this class appears.
0

Is all your bothered about is showing the blog tab being highlighted, when on an archive page? – Alex Thomas 38 mins ago


Yes. This is all I am bothered about. I suppose a simple way to rephrase this is that my custom-template page isn't recognized as an archive page. – Avry 18 mins ago

If this is the case you could be a little crude and just set the style of the blog tab in your archives.php template, using either <style>...</style> or give the blog tab the selected class using jquery:

$('#blogtabid').addClass('selectedclass');

How about that?

2
  • Umm. I suppose that works. I'm looking for a WP answer, though. This seems like something WP should be able to do. Further, if I change my menu this change won't stick. Jun 16, 2011 at 11:02
  • true, very true... Jun 16, 2011 at 11:11
0

You're right to go to the $wp_query object.

$wp_query->is_archive = true;

3
  • 1
    I still don't think this is a good approach. Overriding core conditionals like that can (and almost certainly will) have unintended consequences. Jun 16, 2011 at 19:36
  • @Avry: I would give it a whirl. Of course, no checkmark for me if there are consequences! Jun 16, 2011 at 19:43
  • Is $wp_query->is_archive = true; different from $wp_query->set('is_archive', 1);? I've tried this with no results. Where should I put this? (See the question content for details). Jun 16, 2011 at 21:45
0

I came across something like this recently for highlighting terms if single post is active and to be honest i had to use some jquery for it. Its not pretty but it works for now. Wordpress lacks this feature which i do agree sucks You can always use a function to load it for certain pages.

Example:

function wpse_20163_navstyle() {
    if(is_archive()) { ?>
        <script type="text/javascript">
        jQuery(document).ready(function(){
            jQuery('#blogtabid').addClass('selectedclass');
        });
        </script>
    <? }
add_action('wp_head','wpse_20163_navstyle')

Also if you are wanting to use the $wp_query make sure to have a global as well

global $wp_query; and then you can $wp_query->is_archive = true;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.