WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] custom fields for thumbnails? (5 posts)

  1. dangle2k
    Member
    Posted 3 years ago #

    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.

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    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>
  3. emilekott
    Member
    Posted 3 years ago #

    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

  4. dangle2k
    Member
    Posted 3 years ago #

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

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

  5. Murphy
    Member
    Posted 3 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic