Why Error: Parse error: syntax error, unexpected T_STRING ...../index.php on line 15

I am trying to get the categories of a post in WordPress and call a specific image.

this function looks correct to me:

       <?php
                            $categories = the_category('/');   # this is line 15 of the index.php
                            $categoryArr = explode('/', $categories);
                            $last_category = end($categoryArr);
                            $last_category = preg_replace(" ","-",$last_category);
                         ?>
                                             <img class="img-feature" src="/blog/wp-content/themes/taka/category-circles/category_<?php echo $last_category ?>.png" />


but I get this error:  


Parse error: syntax error, unexpected T_STRING in /home/devhap/public_html/blog/wp-content/themes/taka/index.php on line 15
SivakatirswamiAsked:
Who is Participating?
 
Chris StanyonConnect With a Mentor Commented:
OK. You're doing a couple of things wrong here. First  the_category('/') outputs (echoes) the category list - you need to return the value of it using get_the_category(). That will return an array of objects so you don't then need to explode it. To get the name of the category you need to get the cat_name property of the object. Also, preg_replace is for doing a regular expression search and replace - you look like you are just doing a string replace so need to use str_replace. Something like this:

<?php 
$lastCategory = end(get_the_category());
$lastCategoryName = $lastCategory->cat_name;
$imageName = str_replace(" ", "-", $lastCategoryName);

printf("<img class='img-feature' src='/blog/wp-content/themes/taka/category-circles/category_%s.png' />", $imageName);
?>

Open in new window

0
 
Loganathan NatarajanLAMP DeveloperCommented:
just try like this,

$categories = the_category(', ');
0
 
Terry WoodsIT GuruCommented:
is "the_category" a function?

If so, then it's possibly fine. What comes before line 15?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Terry WoodsIT GuruCommented:
Maybe:
<?php echo $last_category ?>
Should be this?
<?php echo $last_category; ?>
0
 
Terry WoodsIT GuruCommented:
Also, where are the delimiters for your preg_replace? Shouldn't it be:
                            $last_category = preg_replace("/ /","-",$last_category);

Open in new window

0
 
SivakatirswamiAuthor Commented:
Aloha, Chris:

Fantastic, thank you I have been coding xTalk (I present incarnation is LiveCode) all my life and just getting my feet wet with PHP. This works:

dev.himalayanacademy.com/blog/taka/

I put this into the functions.php of our theme
function get_category_image () {
$lastCategory = end(get_the_category());
$lastCategoryName = $lastCategory->cat_name;
$imageName = str_replace(" ", "-", $lastCategoryName);
printf("<img class='img-feature' src='/blog/wp-content/themes/taka/img/category-circles/category_%s.png' />", $imageName);
}

Open in new window

then in the theme index.php all we need is:
<div class="article-wrapper">
	<aside class="sidebar" role="complementary">
						
	<?php get_category_image (); ?>
                                            
	<h3 class="h3 header topics">
		Categories
	</h3>

Open in new window


It works (using the same circle presently for all categories. Our design department has to catch up now...)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.