• What I’m hoping to achieve is have a post with categories ‘one’ through ‘ten’ and have custom fields that contain image URLs where the name corresponds to the category (‘one’ through ‘ten’). When you view the posts in a specific category, say ‘two’, then an image from each post with the custom field name ‘two’ will show up.

    When you view the specific post’s page, you’ll see all images (ideally through NextGEN Gallery) from the custom fields, instead of the single image associated with the post and corresponding to the category you’re viewing.

    Hope this makes sense, I’m fairly new to WordPress, and could do this with another CMS that I’m more used to, but need to achieve something similar on WP. Thanks for the help, and let me know if I need to provide any further detail.

Viewing 4 replies - 1 through 4 (of 4 total)
  • If you’ve already got the script set up to load the custom image then you just need to add the category name in there.

    <?php
    // first: determine the category
    $category = get_the_category();
    $catname = $category->cat_name;
    // now get the image from the post meta, using the category name as our custom field name
    $img_src = get_post_meta($post->ID, $catname, true);
    // if there's no entry we'll use a default image
    if ( $img_src == '' ) $img_src = 'blank';
    // so compile our image resource
    $img_src = '/wp-content/uploads/images/'.$img_src.'.png';
    
    // write it out
    echo '<img src="https://example.com' . $img_src ." />';
    // finished. now we can do whatever else we want before we close the loop.
    ?>
    Thread Starter bccarlso

    (@bccarlso)

    Thanks for the help, bsutcliffe. The logic behind that makes perfect sense to me, but the implementation hasn’t been working, at least with me toying around with it. I don’t understand WP PHP architecture as much as I’d like to. Here’s the code I’m working with, and I’ve tried placing your code into various parts of this, also fixing a double quote issue after “example.com” I think.

    <?php while (have_posts()) : the_post(); // check for thumbnail
    $thumb = get_post_meta($post->ID, 'Thumbnail', $single = true);
    // check for thumbnail class
    $thumb_class = get_post_meta($post->ID, 'Thumbnail Class', $single = true);
    ?>					
    
    	<div <?php post_class() ?> class="block" id="post-<?php the_ID(); ?> articles">
    
    		<?php // if there's a thumbnail
    			if($thumb !== '') { ?>
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    				<img src="<?php echo $thumb; ?>" class="listing-image" alt="<?php if($thumb_alt !== '') { echo $thumb_alt; } else { echo the_title(); } ?>" />
    			</a>
    		<?php } // end if statement
    		// if there's not a thumbnail
    		else { echo ''; } ?>
    
    		<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
    		<div class="entry">
    			<?php the_content('Read the rest of this entry &raquo;'); ?>
    		</div>
    
    	</div>
    
    <?php endwhile; ?>

    If you can suggest where I put that code in, to essentially replace the image that’s there, I’d be super grateful. Thanks.

    I understood your first post to mean that for, say, a post you add to three categories you add three custom fields

    ‘cat name 1’ -> ‘img1234.jpg’
    ‘cat name 2’ -> ‘img5678.jpg’
    ‘cat name 3’ -> ‘img910.jpg’

    Then, when I see your post when looking at the ‘cat name 1’ archive I see img1234.jpg as the thumb, and when I look at your post in the ‘cat name 2’ archive I see img5678.jpg.

    Am I right in my thinking? I don’t know what you’re trying with “Thumb” and “Thumbnail Class”?

    Thread Starter bccarlso

    (@bccarlso)

    You’re right in understanding what I was trying to say. Each post currently has one image that gets displayed no matter which category you view the post from, so that’s what the Thumb is for, and the Thumbnail Class has to do with styling the image I believe. I’m hoping to figure out the logic to display the thumbnails just as you described, but couldn’t get your previous code to work.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Field Image Displayed Only in its Category’ is closed to new replies.