Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating menu in wordpress and I would like add class="active" to link when its page is active. I'm not using inbuilt wordpress menu feature.

I'm making menu hardcoded in header.php because this menus are linked to custom posttype archive pages. So I would like to keep class active if one is inside single or archive page of that posttype.

Here is what i would like to do:

header.php

<?php if ($post_type == "custom_post_type1") : ?>
<li><a href="link_to_post_type1_story" class="active">Post type 1</a><li>
<?php else: ?>
<li><a href="link_to_post_type1_story">Post type 1</a><li>
<?php endif; ?>

<?php if ($post_type == "custom_post_type2") : ?>
<li><a href="link_to_post_type2_story" class="active">Post type 2</a><li>
<?php else: ?>
<li><a href="link_to_post_type2_story">Post type 2</a><li>
<?php endif; ?>

I hope I explained what I'm looking for well.

Thanks.

share|improve this question

4 Answers 4

you can use get_queried_object, as get_post_type does not work on custom post type (I don't know yet why, more info here)

 $obj = get_queried_object();
 $custom_post_type = $obj->post_type;

then you can proceed with

 if($custom_post_type == "type1"){
    // do something
 } else {
    // etc..
 }

I've tested this in one of the sites I'm working on, I hope it work on yours too..

EDIT:

here is the new code that you can use for archives too:

global $wp_query;

if (is_archive()):  
    $custom_post_type = get_query_var('post_type');
    $custom_taxonomy = get_query_var('taxonomy');
else:
    $obj = get_queried_object();
    $custom_post_type = $obj->post_type;
endif;

I've added a taxonomy type too in case you would need one yourself.

share|improve this answer
    
hi Sorry for replying back late. I tried using get_queried_object, works greatly in single page but not in archive page. Do you know any workaround for that? Thanks. –  Kuldeep Daftary Aug 5 '13 at 10:49
    
lemme check.. i'll test and cook something up for ya. –  reikyoushin Aug 5 '13 at 12:56
    
i've updated the answer.. please tell me if it works. thanks! –  reikyoushin Aug 5 '13 at 13:11

You can still use the built in menu, even when linking to custom types archive... You just need to insert a custom link with the URL of the custom archive page (in the menu creation page, at the left, click on "link". Like this you don't need to worry about creating the classes manually, and the menu will be easier to maintain :)

share|improve this answer
    
Or even use this plugin to add the menu items. wordpress.org/plugins/post-type-archive-links –  mrwweb Jul 24 at 17:20

You can use the function get_post_type

<? get_post_type( $post ) ?>

Where $post is the ID or the object of the post you want to check.

share|improve this answer

If no post, get_post_type() may return false.

share|improve this answer
2  
Can you please expand your answer with some example and explanations? –  Nilambar Sep 7 at 2:52
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  Mayeenul Islam Sep 7 at 4:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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