Forums

[resolved] Custom field image duplication on index.php (5 posts)

  1. shakingpaper
    Member
    Posted 2 years ago #

    Hi all,

    I am working on a custom template for my studio's blog. The designer wants a custom image to show up as a header for each post (you can see what I mean here BTP

    I am using a custom field to make this happen, the probelm being that when I want to show multiple posts on the index.php the most recent image gets duplicated and shows up as the header image for each post. This makes sense because their isn't a custom id attached to each image/post but am wondering if there is a way to achieve my desitred outcome (that being, the correct image for each post) without having to create a new field for each one. The code I am using is below:

    In the header.php:

    <?php
    	    $image = get_post_meta($post->ID, 'bg', true);
    	?>

    and in the index.php:

    <?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    			<div class="post" id="post-<?php the_ID(); ?>">
    
    				<div class="post_header"><a href="<?php the_permalink() ?>"></a></div>
    
    				<p class="small">Posted by <?php the_author(); ?> on <?php the_time('j F, Y') ?></p>
    
    				<h2 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    				<?php the_content('Read the rest of this entry &raquo;'); ?>
    
    				<p class="postmetadata"><?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></p>
    			    <?php
    					$comment_array = array_reverse(get_approved_comments($wp_query->post->ID));
    					$count = 1;
    				?>
    
    				<?php if ($comment_array) { ?>
    
    				<?php foreach($comment_array as $comment){ ?>
    				<?php if ($count++ <= 1) { ?>
    					<p class="comment_excerpt"><?php comment_excerpt(); ?></p>
    
    				<?php } ?>
    				<?php } ?>
    
    				<?php } ?>
    			</div>
    
    		<?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    	<?php endif; ?>
    
    	</div> <!-- end of content -->

    Where the div class post_header shows the image.

    Any help would be appreciated.

  2. stvwlf
    Member
    Posted 2 years ago #

    When you put this code

    <?php
    	    $image = get_post_meta($post->ID, 'bg', true);
    	?>

    in header.php, it is only executing once per PAGE, which is why its repeating. You would be displaying the image for the 1st post on all the posts. The code needs to be in the WP loop in index.php so it will run on each post and pull the image associated with that post.

    If you want a default image to display on posts that don't have a specific image assigned then change your code to something like this
    <?php
    $image = get_post_meta($post->ID, 'bg', true);
    if (!$image) {
    $image = ... assign default image;
    }
    ?>
    You could, for example, save the url from the 1st post & use that as the default when a post has no custom image field created. The code I suggested tests to see if a value is assigned to $image from the custom field - if not assign the default.

  3. shakingpaper
    Member
    Posted 2 years ago #

    Thanks for your reply!

    I tried putting the snippet I had in the header within the loop but it didn't seem to work - any clues as to where I should place it?

    The concept of a default image could be handy but for the minute I think we are pretty keen to have a different one for each post.

  4. stvwlf
    Member
    Posted 2 years ago #

    I don't see anywhere in index.php where you are displaying the image, and your header code excerpt didn't include the display either.

    You not only need to move the get_post_meta, you need to move the code that displays $image, into the loop in index.php

    Where you put it depends on where you are displaying the image. If its to be above all the text, as in the page you linked to, then probably after this line

    <div class="post" id="post-<?php the_ID(); ?>">

    You also need see that CSS applied to the image when the code is in header.php is also applied when you move the code into index.php - there are likely to be different ancestors when in the header than in the content area.

  5. shakingpaper
    Member
    Posted 2 years ago #

    It worked! Thanks so much for your response. It makes much more sense now.

Topic Closed

This topic has been closed to new replies.

About this Topic