?
Solved

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

Posted on 2012-09-19
6
Medium Priority
?
655 Views
Last Modified: 2012-09-20
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
0
Comment
Question by:Sivakatirswami
6 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 38416395
just try like this,

$categories = the_category(', ');
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38416397
is "the_category" a function?

If so, then it's possibly fine. What comes before line 15?
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38416400
Maybe:
<?php echo $last_category ?>
Should be this?
<?php echo $last_category; ?>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Expert Comment

by:Terry Woods
ID: 38416402
Also, where are the delimiters for your preg_replace? Shouldn't it be:
                            $last_category = preg_replace("/ /","-",$last_category);

Open in new window

0
 
LVL 45

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 38416811
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
 

Author Comment

by:Sivakatirswami
ID: 38418977
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
While Plesk offers many potential benefits to website administrators, including compatibility with Windows Server and other leading technologies, the company has also been working to differentiate it from other control panels for content management…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 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