• I’m currently using custom fields to display image thumbnails (that link to posts) on the front page of a wordpress site.

    Now what I’m trying to do is use a custom field to display a large image within each single post. I’d prefer to code it into the template rather than use a plugin.

    I used the solution presented here to get the thumbnail thing working, however it doesn’t seem to work for the inpost image.

    I get this error:
    Parse error: parse error, unexpected ‘=’ in \themes\THEME\single.php on line 15

    <?php get_header(); ?>
    
    <?php get_sidebar(); ?>
    
    	<div id="content">
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    			<div id="sample-image">
    				<!-- THIS IS WHERE THE IMAGE SHOULD GO -->
    			</div>
    
    		<div class="post-single" id="post-single-<?php the_ID(); ?>">
    
    			<h2><?php the_title(); ?></h2>
    
    			<div class="entry">			
    
    				<?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>
    
    				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    				<?php the_tags( '<p>Tags: ', ', ', '</p>'); ?>
    
    				<p class="postmetadata alt">
    					<small>
    						Filed under <?php the_category(', ') ?>.
    					</small>
    				</p>
    
    			</div>
    		</div>
    
    		<div class="navigation">
    			<div class="alignleft"><?php previous_post_link('&laquo; %link') ?></div>
    			<div class="alignright"><?php next_post_link('%link &raquo;') ?></div>
    		</div>
    
    	<?php endwhile; else: ?>
    
    		<p>Sorry, no posts matched your criteria.</p>
    
    <?php endif; ?>
    
    	</div>
    
    <?php get_footer(); ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi there,

    Which line is line 15 of single.php?

    Thread Starter swollenpickles

    (@swollenpickles)

    Thanks, here’s the revised page code I’m using:

    <?php get_header(); ?>
    
    <?php get_sidebar(); ?>
    
    	<div id="content">
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<?php $sample-image = get_post_meta($post->ID, 'sample-image', $single = true); ?>
    			<div id="sample-image">
    
    				<?php // if there's a thumbnail
    				if($sample-image !== '') { ?>
    					<p>
    						<img src="<?php echo $sample-image; ?>" />
    					</p>
    				<?php } ?>
    			</div>
    
    		<div class="post-single" id="post-single-<?php the_ID(); ?>">
    
    			<h2><?php the_title(); ?></h2>
    
    			<div class="entry">
    
    				<?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>
    
    				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    				<?php the_tags( '<p>Tags: ', ', ', '</p>'); ?>
    
    				<p class="postmetadata alt">
    					<small>
    						Filed under <?php the_category(', ') ?>.
    					</small>
    				</p>
    
    			</div>
    		</div>
    
    		<div class="navigation">
    			<div class="alignleft"><?php previous_post_link('&laquo; %link') ?></div>
    			<div class="alignright"><?php next_post_link('%link &raquo;') ?></div>
    		</div>
    
    	<?php endwhile; else: ?>
    
    		<p>Sorry, no posts matched your criteria.</p>
    
    <?php endif; ?>
    
    	</div>
    
    <?php get_footer(); ?>

    I’m getting this error message when attempting to view a single post:
    Parse error: parse error, unexpected ‘=’ in \themes\template\single.php on line 8

    This is line 8:
    <?php $sample-image = get_post_meta($post->ID, 'sample-image', $single = true); ?>

    Scott Reilly

    (@coffee2code)

    WordPress & Plugin Developer

    You can’t have a dash as part of a variable’s name. Change $sample-image to $sample_image. Looks like you have to do that in three places.

    Also, your one line of code would be better as:

    <?php $sample_image = get_post_meta(the_ID(), 'sample-image', true); ?>

    What you are doing above is actually one of the reasons why I originally wrote the Get Custom Field Values plugin. All this code of yours:

    <?php $sample-image = get_post_meta($post->ID, 'sample-image', $single = true); ?>
    			<div id="sample-image">
    
    				<?php // if there's a thumbnail
    				if($sample-image !== '') { ?>
    					<p>
    						<img src="<?php echo $sample-image; ?>" />
    					</p>
    				<?php } ?>
    			</div>

    would just be this with the plugin:

    <div id='sample-image'>
       <?php echo c2c_get_custom('sample-image', '<p><img src="', '" /></p>'); ?>
       </div>

    (You could put the div tags in the call to the function too so that the sample-image div also doesn’t display if there is no sample image. But the way your original is coded, the div will always display, so my c2c_get_custom example behaves that same way.)

    Thread Starter swollenpickles

    (@swollenpickles)

    Thank you for the reply. I replaced the dashes with underscores and it works a treat now.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Using custom field to display images in post’ is closed to new replies.