Build Your Own Business Website header image

Welcome to our Members Only Forum

You are welcome to view the content on our Forum, just scroll down to view this forum post.
If you like what you find and want to be able to post a question, please check out our subscription options.
Please feel free to search the forum using either the Google Site Search above or the forum search below.

Avatar
Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Finding categories of custom post type
Avatar
Azmat
Member
Members
Forum Posts: 41
Member Since:
November 9, 2011
sp_UserOfflineSmall Offline
1
March 7, 2013 - 8:53 pm
sp_Permalink sp_Print

I am interested in displaying something when a user visits a specific category page from a custom post type.  The custom post type could also have multiple categories but I want to test if it includes of the desired categories.  Here is what I was testing:

 

if(in_category('Free Stuff')){

echo "<script type='text/javascript'>alert('Free Category');</script>";  

}

 

However, the alert box never shows.  I am thinking it's because I am using a custom post type and taxonomy.  Does in_category work with custom post types?  Or is there another way to find if a custom post type has a specific category.  Appreciate the help.  Thank you.

Avatar
Rick Anderson
Desert Hot Springs, CA
Admin
Forum Posts: 13637
Member Since:
November 8, 2009
sp_UserOfflineSmall Offline
2
March 8, 2013 - 6:50 am
sp_Permalink sp_Print

in_category will work fine for categories, regardless of the post type.  However, it does need to be called within the loop, otherwise it won't know which post to check,.

If you are checking outside of the loop then you need to specify the post ID - here is an article on using in_category.

I would test this first without the java script just to make sure that the problem doesn't lie there.

Avatar
Azmat
Member
Members
Forum Posts: 41
Member Since:
November 9, 2011
sp_UserOfflineSmall Offline
3
March 8, 2013 - 9:42 am
sp_Permalink sp_Print

Thanks Rick.  I created a box and placed it inside the WP Loop.  It works fine when I put the box on the Single template for posts.  I am able to get an alert box to show up if the post is in a certain category.

 

However, I don't see the same thing when I place the box inside the custom post types WP loop.  Any tips?  Thanks!

Avatar
Rick Anderson
Desert Hot Springs, CA
Admin
Forum Posts: 13637
Member Since:
November 8, 2009
sp_UserOfflineSmall Offline
4
March 9, 2013 - 6:45 am
sp_Permalink sp_Print

Let me see if I understand the situation. 

- You have a custom post type (we'll call it products) and

- you have extended categories so that they apply to products

- You have assigned specific categories to specific products

- Your box works fine in the Single template

- The same box doesn't work in the Products template

Is this the case?  It doesn't seem possible.  It seems like one of the above must not be right.

Forum Timezone: America/Los_Angeles

Most Users Ever Online: 228

Currently Online:
16 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

Keith W Jones: 1431

Brian Tozer: 1004

Carolin: 982

Heidi Hafner: 509

John Cunningham: 423

Reece Morrel: 353

Newest Members:

emma62755

sharonmarks

emma80233

Tijs de Jong

Daniel Frey

mutek

Forum Stats:

Groups: 8

Forums: 46

Topics: 6998

Posts: 34804

 

Member Stats:

Guest Posters: 6

Members: 6761

Moderators: 2

Admins: 2

Administrators: Rick Anderson, Rick Anderson

Moderators: Pierre Cote, Laura Nugent