• Resolved dangle2k

    (@dangle2k)


    I’m working on one of my first custom templates for a client and set up the featured image/thumbnails just fine. I even have it set up to use a default image in case there is no featured image like so:

    <div class="post-thumbnail">
    	<?php
    	if(has_post_thumbnail()) {
    		the_post_thumbnail();
    	} else {
    		echo '<img src="http://mysitename.com/authors/default.jpg" />';
    	}
    	?>
    </div>

    (This is only for the category/archive type pages and not for single post pages)

    Quite proud of myself 😉 but now I would like to set up a custom field for the authors to use to a photo of themselves in place of the default image when writing their posts. After much searching I came up with this: I created a custom filed with a key of “author_photo” and in the value they would type in the url to the image. Then in the template I have this:

    <div class="post-thumbnail">
    	<?php
    	if(has_post_thumbnail()) {
    		the_post_thumbnail();
    	} else {
    		echo '<img src="<?php echo get_post_meta($post->ID, 'author_photo', true); ?>" />';
    	}
    	?>
    </div>

    Of course it’s not working, that’s why I’m asking for some help on this. This gives me the error “Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in (path to file) on line 28”

    Can someone please point out what I’m doing wrong here or if this is even possible or if perhaps there is a better way of doing what I’m attempting. Ultimately I would like to have it so that if the author of the post forgets or doesn’t use the custom field for their photo it would fallback to the default image again like what I have below (again doesn’t work of course but hopefully you get the idea!)

    <div class="post-thumbnail">
    	<?php
    	if(has_post_thumbnail()) {
    		the_post_thumbnail();
    	} elseif {
    		echo '<img src="<?php echo get_post_meta($post->ID, 'author_photo', true); ?>" />';
    	} else {
    		echo '<img src="http://mysitename.com/authors/default.jpg" />';
    	}
    	?>
    </div>

    I’ve spent many hours on this because I like to try to figure things out myself but it’s time for me to ask for help. Any assistance would be greatly appreciated.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • in this line:
    echo '<img src="<?php echo get_post_meta($post->ID, 'author_photo', true); ?>" />';
    you are using a php tag with more echo within an already open php tag, instead of concatenating the strings:

    this might work (also added the conditional check for the author image):

    <div class="post-thumbnail">
    	<?php
    	if(has_post_thumbnail()) {
    		the_post_thumbnail();
    	} elseif( get_post_meta($post->ID, 'author_photo', true) ) {
    		echo '<img src="' . get_post_meta($post->ID, 'author_photo', true) . '" />';
    	} else {
    		echo '<img src="http://mysitename.com/authors/default.jpg" />';
    	}
    	?>
    </div>

    Also would highly recommend this plugin: http://wordpress.org/extend/plugins/custom-field-template/

    It allows you to have a Browse… box and upload a file in a custom field

    Thread Starter dangle2k

    (@dangle2k)

    alchymyth, thank you so much! That works perfectly, exactly what I needed.

    emilekott, thank you as well. I’ll be checking out that plugin.

    Murphy

    (@danielpunchupproductionscom)

    Does anyone know how to add custom fields to the shutter or lightbox modal so the custom fields show just below the image? I’m not good with javascript. It’s for the nextgen gallery.

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘custom fields for thumbnails?’ is closed to new replies.