$skin_type = "green";
$category_id = get_cat_ID( single_cat_title("", false));
if($category_id == 218){
$skin_type = "blue";
}
I have this code at the top of my header to select different backgrounds for my categories.
It works great when navigating between Categories with the menu, but it single_cat_title() does seem to work when I click a post to enter the single.php . What am I doing wrong and how can I get around it?
Thanks
single_cat_title() works on category pages because the category is included in the query vars. On a single page, there isn't a category name or id included in the query vars, just the post id.
to take a look at the query vars, include the following in your page, either in the header or on category.php and in single.php to see the difference.
<?php var_dump($wp_query->query_vars);?>
You'll want to use get_the_category($post_ID) to get the categories of the single post. The function returns an array of objects, one for each category assigned to the post. To grab the first, or only category, use the array index "0". If used outside the loop, pass the post id to the function. Inside the loop, it will default to the current post id. Since you are controlling your header with this function, I bet you'll be passing the post id.
$skin_type = "green";
$post_id = $_GET['p'];
$category = get_the_category($post_ID);
$category_id = $category[0]->**cat_ID**;
$if($category_id === 218){
$skin_type = "blue;
}
Edit:
I realize I was assigning the category name to $category_id above, I've changed the object property to cat_ID which is the proper property
Udate to answer question in comment
If post "SFSF" is listed in categories 1,2,3, and 4, and you want to change the color of category 3 when the post is viewed on single.php, then you must add the category ID to the link that you click, assuming your navigation is category based. When you click on category 3 in your navigation menu, you'll see in the query string
"www.example.com/cat=3"
Let's assume this is the title of the post.
<a href = "<?php the_permalink();?>/my_cat=<?php echo $_GET['cat'];?>">
<?php the_title();?>
</a>
Here, you've added the custom query var "my_cat" and assigned it the value of the current page's category id.
On single.php, Instead of getting the category ID from the category name like I show above, you'll retrieve it from the query string similar to the way you retrieve the post ID.
$skin_type = "green";
$category_id = $_GET['my_cat'];
$if($category_id === 218){
$skin_type = "blue;
}
Related
I want to get the value of the category and say that if I am in a specific category such a text will appear and if it is not then another text will appear
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
if($category_id == 83){
echo '123';
}else{
echo '1234';
}
This is what I tried to do and it doesn't work for me. Is there a way to make it work well?
editing:
It is important for me to note that I want to display within the post the number of the category to which the post is associated.
WordPress Get Category ID from Slug
Here we pass the $slug to WordPress’ get_category_by_slug() function. That gives us an object of useful category data, including the ID, which we then call directly:
$slug = 'sales'; // assume Sales is category name you can get cat id from slug name
$cat = get_category_by_slug($slug);
$catID = $cat->term_id;
My wordpress site has categories with a simple hierarchy –
Blog
Role Shift
Urod the Last Show
News
I'm using the if statement below to catch if the post is in category blog or any of the child categories - and it works - but it feels stupid not to be able to just check the parent of the current category (plus I might want to add categories later).
if ( is_category('blog') || in_category(array('role-shift', 'urod-the-last-show', 'news')) )
I've searched and tried every suggestion - including cat_is_ancestor_of - but nothing is working.
Please help!
Robert
$categories = get_the_category(); //returns categories
$thiscat = $categories[0];
$parent_id = $thiscat->parent; //the parent id
$parent = get_category($parent_id) //this returns the parent category as an object
//use id or slug of category you are searching for
if( $parent_id == 1 || $thiscat->slug == 'blog' ){
//this is a child of blog or the blog page
}
This should do the trick.
This will determine if the current category is a child of the blog page.
The first part, get_category, returns the current category.
Then you can get the parent id from the current category and use 'get_the_category_by_ID' to get the parent category object.
Then you can check if you are under the parent category you want.
The problem I have is as follows:
I have custom post type named 'X'.
It has few categories named 'Category_1', 'Category_2', 'Category_3'
etc.
By the logic of my website, one post can only have one category or no category at all.
On page single-x.php (single-posttypename.php) I want to check, if post has a category at all, if it has, I want to get all other posts with the same category, and provide them as a sub-navigation. If post does not have a category just do nothing.
<ul>
<li class="active">post1</li>
<li>post2</li>
...
</ul>
For the current post, I want to add class active so it would act as a navigation.
you can have a check like this
$x= $_POST['x'];
if(isset($x) && $x!='')
{
$customise_category = $x;
}
else
{
$customise_category = '';
}
you can use this $customise_category where it is needed.
I have to insert some condition if the current page is a category page or not.
you can use this for getting category
is_category();
is_category( '1' ); // where 1 is category id
is_category( 'test' ); // where test is category name
is_category( 'test-ing' ); // where test-ing is category slug
I have found the way to do it by checking if $cat_id is available or not on that page by the following.
$cat_id = get_query_var('cat');
Now we can check if $cat_id is available then it is a category page otherwise it is not.
You can use
is_category();
function.
refrence:
http://codex.wordpress.org/Function_Reference/is_category
Amit's answer will work but there is a simpler way as Wordpress sets a Global var for the category id of $cat. So just checking if this is set will tell you if you are on a category page.
if(!empty($cat)){ //do something just on a category archive page }
if been searching for hours :-( Hope anyone can help me.
I'm in a custom post type called "grhdogs". There I need to fetch the current page title of that page and search with that title in another custom post type called "slideshow" and echo (as string) the page ID of the matching pagetitle in "slideshow".
The background is that in both custom post types are always matching page titles. I have to bring them together because "grhdogs" is a post and "slideshow" is the matching slideshow that I want to display in the post.
You can try some thing like this.
<?php
//get the current title of the page
$postTitle = get_the_title();
//search for similer post in db table
$postID = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type='slideshow' AND post_title = %s",$postTitle));
//print id
echo $postID;
//or use get_post($post_id) to get whatever you want
$getCustomPost = get_post($postID);
echo $getCustomPost->id;
?>