WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Trying to add images to front page with Custom Field (5 posts)

  1. martcol
    Member
    Posted 2 years ago #

    I want to put a couple of rows of thumbnails of post images on the front page.

    I have been following a tutorial on using custom fields for this and have got stuck.

    I use a child theme of Notes Blog Core Theme.

    Here is a link to the index page code: http://pastebin.com/ZKfQWDkz

    Line 39 and Line 85 show where I have added some code to that page.

    My function php looks like this

    <?php if ( function_exists( 'add_theme_support' ) ) {
    add_theme_support( 'post-thumbnails', array( 'home' ) ); // Add it for posts
    } 
    
    function get_custom_field_data($key, $echo = false) {
    	global $post;
    	$value = get_post_meta($post->ID, $key, true);
    	if($echo == false) {
    		return $value;
    	} else {
    		echo $value;
    	}
    }
    ?>

    Thank you.

    Martin

  2. Digital Raindrops
    Member
    Posted 2 years ago #

    You have not said what is in the custom field, you would need the full path to the images, something like:

    <img src="http://full/path/to/thumbnail-1.jpg" class="alignleft" width="size" height="size" alt="" />
    <img src="http://full/path/to/thumbnail-2.jpg" class="alignleft" width="size" height="size" alt="" />

    Then a simple echo in your page
    UNTESTED

    <?php //Section to add the custom field image ?>
    <?php $images = get_custom_field_data('post_thumb', true); ?>
    <?php if( $images ) : ?>
    	<div class="thumbnails">
    		<?php echo $images; ?>
    	</div>
    <?php endif; ?>
    <?php //end section to add custom field image ?>

    Confused as you say a couple of rows of images and using a custom field seems like it would need quite a bit of work, also you have not set the post image size, as it looks like you want to use the featured image or thumbnails?

    There are a number of (YARPP) Yet Another Related Post Plugins that might suit and these could be a better option to adding code.

    I wrote this simple footer image ribbon, a few months ago it might help with what you want, if you want to still go down the custom route?

    David

  3. martcol
    Member
    Posted 2 years ago #

    Thanks David, for taking the trouble to help.

    Your, "simple footer image ribbon" appears to be the solution I am looking for.

    The site I am working on is for a poet who produces small poems with the words shaped as the object of the poem subject. She posts regular posts and poem post. I set up the site with a home page and two category based post pages. Now the site owner wants to change the home page and have a small gallery of say, 8 recent thumbnails of the poem posts. So, she wants to reduce the amount of text on the home page and under that content have a small gallery (not in the footer).

    So, I need to somehow get that post thumb and get it on the home page.

    I tried that YARPP plugin and totally broke the site! Not the plugin fualt, it was me!

    Still, I thought that what I could do was get the featured image somehow. Then I thought that it might be better to use a custom field. Now I see your solution so there is lots to think about.

    Regards

    Martin

  4. Digital Raindrops
    Member
    Posted 2 years ago #

    Hi Martin,
    I adjusted the code for just the home or front page, added a condition to see if the image has a thumbnail, and put it up on pastebin.
    http://pastebin.com/G4DmHZwb

    Save the code in a file latest-posts.php

    Add a call in the home or footer.php file:
    <?php get_template_part('latest','posts'); ?>

    You could add a category and filter the post thumbnails on the category, to give the client more control.

    The only settings is adjusting the number of posts and thumbnail image sizes for your theme.

    HTH

    David

  5. martcol
    Member
    Posted 2 years ago #

    Thank you David,

    For your help with this.

    Your soulution proved to be exactly what I needed and seems like a very straightforwards way to do it.

    Regards

    Martin

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.