0
votes

Pretty basic problem, so sorry but have been banging my head off the wall for well over an hour.

basically writing a script to set a default Wordpress featured image for a category if there is no featured set.

            if ( get_the_post_thumbnail( $projects->post->ID ) ) { 
                $imagetag = get_the_post_thumbnail( $projects->post->ID, 'portfolio-thumb', array( 'class' => 'img-square', 'alt' => get_the_title($projects->post->ID) ) ).''; }
            else {

            $imagetag .='<img src="ThisIsMySite\img\"'.$tag->slug .'"image.jpg" />';                
            } 

All works, except I can't get the else image to work, the closest I can get is to output :

img src="ThisIsMySite\img\"maths"image.jpg"

(Where maths is the current category) It must be something stupid / really obvious I'm missing / doing wrong but I can't work it out.

2
um you have to many " use this '<img src="ThisIsMySite\img\'.$tag->slug .'image.jpg" />' - cmorrissey
You're concatenating inside your else statement with .=, shouldn't it just be = without the period? - AndyWarren
@AndyWarren that may be valid but impossible to say without seeing more of the code. - Martin
They'd have had had to set the $imagetag variable outside the conditional, but if that is the case then the if value would also need to be concatenatred. Right? - AndyWarren
If $imagetag doesn't exist then the .= will create it by concatenating the string onto a null value. - Martin

2 Answers

2
votes

Clearshot66 almost had the right idea, but had flipped the quotes. You want to retain the single quotes and remove the excess double quotes.

single quotes here are PHP quotes, double quotes are HTML entity quotes.

   $imagetag .='<img src="ThisIsMySite/img/'.$tag->slug .'image.jpg" />';    

aka:

 $imagetag = '<string 
                      HTML ELEMENT = 
                      "HTML URL STING>' 
             CONCAT $tag->slug CONCAT 
             '< HTML ELEMENT "
              string>'; 

Also replace your \ with / because it's online, it uses forward slashes as seperators rather than backward slashes.

0
votes

this should do it for you

$imagetag .='<img src="ThisIsMySite/img/'.$tag->slug .'image.jpg" />'; 

hope this helps