Solved

Conditional based Wordpress functionality

Posted on 2010-11-15
6
298 Views
Last Modified: 2012-05-10
Hey Experts!!

We have a Wordpress blog that has hundreds of different categories.  What I need to do is based on one specific category and it's child, I need to display an image.  I'll try to illustrate it to make it a little easier.

•      Category Main
   o      Subcat 1
   o      Subcat 2 <---I need to show the conditional on this subcat
   o      Subcat 3
   o      Subcat 4  

I did dump the category using the code below and the category ID, I'm seeking is in the 2nd part of the array.  I'm guessing because it's the 2nd category in line.

Any help is appreciated with points! :)



<?php
global $post;
$categories = get_the_category($post->ID);
var_dump($categories);
?>

Open in new window

0
Comment
Question by:LZ1
  • 3
  • 2
6 Comments
 
LVL 10

Assisted Solution

by:Tyler Laczko
Tyler Laczko earned 150 total points
ID: 34138513
you would add code to say

if($categories == "cat2")
  $showImage = true;



on the page that you want the image to display add check which page you are on. If you are on the page where you want to show the image display it. if not display nothing.
0
 
LVL 30

Author Comment

by:LZ1
ID: 34138697
Still no joy.  

Here is the complete category dump results.  Maybe this will help
array(2){ 
[0]=> object(stdClass)#619 (15){ 
["term_id"]=> &string(4) "1883" 
["name"]=> &string(8) "Articles" 
["slug"]=> &string(8) "articles" 
["term_group"]=> string(1) "0" 
["term_taxonomy_id"]=> string(4) "1703" 
["taxonomy"]=> string(8) "category" 
["description"]=> &string(0) "" 
["parent"]=> &string(4) "1881" 
["count"]=> &string(2) "80" 
["cat_ID"]=> &string(4) "1883" 
["category_count"]=> &string(2) "80" 
["category_description"]=> &string(0) "" 
["cat_name"]=> &string(8) "Articles" 
["category_nicename"]=> &string(8) "articles" 
["category_parent"]=> &string(4) "1881" } 
[1]=> object(stdClass)#618 (15) { 

["term_id"]=> &string(4) "1116" 
["name"]=> &string(8) "MATCH THIS" 
["slug"]=> &string(8) "match-this" 
["term_group"]=> string(1) "0" 
["term_taxonomy_id"]=> string(3) "936" 
["taxonomy"]=> string(8) "category" 
["description"]=> &string(8) "match-this" 
["parent"]=> &string(4) "1115" 
["count"]=> &string(4) "2090" 
["cat_ID"]=> &string(4) "1116" <--NEED THIS ONE
["category_count"]=> &string(4) "2090" 
["category_description"]=> &string(8) "match-this" 
["cat_name"]=> &string(8) "skis.com" 
["category_nicename"]=> &string(8) "match-this" 
["category_parent"]=> &string(4) "1115" } }

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 350 total points
ID: 34139407
This gives you an array:
$categories = get_the_category($post->ID);

You want to access the object in array position #1
$obj = $categories[1];

The assuming the property is public, you can do this:
$cat_id = (string)$obj->cat_ID;
echo $cat_id;
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 30

Author Comment

by:LZ1
ID: 34139424
Got it.  

Final code is below:
<?php
			$category = get_the_category(); 
				echo $category[1]->cat_ID;
			?>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34139548
That works great for echo (echo does not care), but you might not want to depend on the data type being a string.  That is why I used explicit typecasting in my example.

Anyway, thanks for the points and good luck with the project, ~Ray
0
 
LVL 30

Author Comment

by:LZ1
ID: 34139620
Thanks Ray.  I didn't think of that.  I'm going to be opening a new question in a moment regarding what I'm trying to do exactly.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now