?
Solved

Searching for a specific value in a Wordpress Category Array

Posted on 2010-11-16
9
Medium Priority
?
332 Views
Last Modified: 2012-05-10
Hey Experts!!

My overall goal is to be able to show a different image based on a certain category name in Wordpress.  I'm hard coding this since we're not allowed to use plugins.(I don't know why).

My code is below.  What I've been doing is getting the category array and then if the 2nd iteration of that array(2nd category)is equal to whatever I have set, then show an image.  The fundamental flaw with this is that the 2nd iteration of the categories array will not always contain the value I'm searching for.  

What I thought I could do is search the array, using array_search, to find the exact name I'm looking for.  I just have no idea how to set that up.

Any and all help is appreciated. Code that I have so far is below:
<?php 
				$category = get_the_category($id);
				if($category[1]->name == 'name1') //name1
					echo ('<a href="http://mydomain.com/">
							<img src="goes here" />
						</a>');
				elseif($category[1]->name == 'name2') //name2
					echo ('<h1>name2</h1>');			
				elseif($category[1]->name == 'name3') //name3
					echo ('<h1>name3</h1>');
				elseif($category[1]->name == 'name4') //name4
					echo ('<h1>name4</h1>')
			?>

Open in new window

0
Comment
Question by:LZ1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 14

Expert Comment

by:Dzynit
ID: 34146848
Have you tried using the is_category('id'); call instead?
You can refer to the reference page here for different ways to check (by id #, name, etc):
http://codex.wordpress.org/Function_Reference/is_category

0
 
LVL 30

Author Comment

by:LZ1
ID: 34146880
Doing that still only gives me the 1st array's id.  This is not always consistent.  I've found that the value I'm checking for can sometimes be 3 or 4 deep.
0
 
LVL 30

Author Comment

by:LZ1
ID: 34148640
After digging deeper on our end, I realized we cannot do what I'm asking with just the categories.  We have multiple sites under 1 WP install and several of our categories are overlapping other sites.

Suggestions?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Assisted Solution

by:Dzynit
Dzynit earned 2000 total points
ID: 34149373
I'm not sure in that case unless the tables use different prefixes per site. Maybe adding an extra conditional to check if it's the right site first then check the cat id?
0
 
LVL 30

Author Comment

by:LZ1
ID: 34149401
Unfortunately the tables are standard WP installs.  They have 1 wordpress install for 19 different websites.  Unfortunately I don't think this is going to be as easy as I once thought.
0
 
LVL 14

Expert Comment

by:Dzynit
ID: 34149651
Give it a try adding a second conditional:
if (strpos(get_bloginfo('url'),"domain.com") != false) && is_category('x')) {
echo 'your pic';
}

Open in new window

0
 
LVL 30

Author Comment

by:LZ1
ID: 34149752
Actually Dzynit, this ended up working.  Because I'm not always looking for the category, it's shorter and I think easier to search on the URL.

Let me know your thoughts.
<?php
	$url = $_SERVER['HTTP_HOST'];//gets current page url
	if (stristr($url, 'mydomain1.com') == true)
		echo ('<a href="http://mydomain.com/">
			<img src="http://content-1.s3.amazonaws.com/images/skis/tag-cloud.jpg" alt="Save On Packages" title="Save On Packages"  width="250px"/>
		</a>');
	elseif(stristr($url, 'mydomain2.com') == true)
		echo "Mydomain2.com "; 
	elseif(stristr($url, 'mydomain3.com') == true)
		echo "Mydomain3.com"; 
?>

Open in new window

0
 
LVL 14

Accepted Solution

by:
Dzynit earned 2000 total points
ID: 34149934
That sounds like an easier solution.
0
 
LVL 15

Expert Comment

by:AndyBeard
ID: 34154391
Modern themes effectively have this built in

<div id="post-3290" class="post-3290 post type-post hentry category-google tag-twitter tag-twitter-hacked tag-youtube tag-youtube-hacked">
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
Suggested Courses

770 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