Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Tips And Tricks WordPress Check If Post Is In Category

WordPress Check If Post Is In Category

Many cases in WordPress, you would like to change a little bit your design or turn on/off some features in a particular category that posts belong to. Meaning display different things on different categories.

For example, in my Timeline category, almost articles there don’t have a long introduce so the adverting banner doesn’t fix the banner (float:left). Therefore, I decide to replace that banner with a bigger banner at center position and put all content under it.

WordPress supports a function called in_category() for this. We can both check if a particular post and current post is in specified categories. The usage is very simple as below:

A particular post is in one or more categories

<?php
	$post_id = 766;
	$category = "timeline"; // or could be category id: 634
 
	if(in_category( $category, $post_id ))
	{
		// do something
	}
	else
	{
		// do some different things
	}
?>

<?php $post_id = 766; $category = "timeline"; // or could be category id: 634 if(in_category( $category, $post_id )) { // do something } else { // do some different things } ?>

Current post is in one or more categories

<?php
	$category = "timeline"; // or could be category id: 634
 
	if(in_category( $category ))
	{
		// do something
	}
	else
	{
		// do some different things
	}
?>

<?php $category = "timeline"; // or could be category id: 634 if(in_category( $category )) { // do something } else { // do some different things } ?>

In both cases, categories specified by ID (integer), name or slug (string), or an array of these. Or check out this function on WordPress Function Reference to get more information.

In addition, we may need to use WordPress custom field to make it more flexible and fully control.

Dec 26, 2011Hoan Huynh
The date of 4rapiddev launchingFacebook Removed View App Profile Page link For New Apps
You Might Also Like:
  • Manage Or Organize WordPress Media Library Structure In Categories
  • Keep Posting When You’re On Vacation With WordPress Post TimeStamp
  • Get WordPress Short Link And Full Link To A Post
  • Add And Get Custom Field In WordPress
  • WordPress Get All Custom Fields
  • WordPress Delete Unused Post Tags By SQL Command
  • How To Change Default Home Page Of WordPress
  • How To Get WordPress Tags By Categories
  • How To Track Website With Multiple Google Analytisc Accounts
  • Delete Bulk Draft And Trash WordPress Posts Includes All Tags, Comments, Meta Fields And Terms Associated
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago Tips And Tricksin_category, Wordpress241
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,460 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,839 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,656 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,998 views
C# Read Json From URL And Parse/Deserialize Json
11,723 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development