• Hi there, any help would be appreciated, I’m totally stuck :/

    I’m working on a site which has in some of the posts, featured images which have been added to the posts via custom fields, for use in archives / category pages. There are many of these posts, far too many to change by hand.

    The rest of the posts have featured images pulled in via the newer ‘featured image’ link in the post.

    In the archive / category pages, I need to tell it to look for one of the custom fields ‘custom_field_name_1’ and if that’s not got an image, look for ‘custom_field_name_2’ and if that’s not got an image, pull in the post thumbnail.

    And if possible, if there is no thumbnail, use a default image.

    Can anyone help?

    This seems to pull in the first custom_field_name_1:
    CODE 1:

    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php
    	$values = get_post_custom_values("custom_field_name_1"); echo $values[0]; ?>" alt="Featured Image" />
                        </a>

    I also found this piece of code which I’ve been trying to play around with, with no success:

    <?php
    //if custom field isn't blank
    if ($alternative_post_image !== '' ) { ?>
    <img src="<?php echo $alternative_post_image; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
    <?php } else { ?>
    <?php
    //show featured image
    the_post_thumbnail();
    ?>
    <?php } ?>

    The above code came from here: http://www.wpexplorer.com/custom-field-image-wordpress/
    What would I need to do to it to make that work?

    This is what I came up with but it doesn’t cut it:

    <?php
    //if custom field isn't blank
    if ($alternative_post_image !== 'custom_field_1' ) { ?>
    <img src="<?php echo $alternative_post_image; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
    <?php } else { ?>
    <?php
    //if custom field isn't blank
    if ($alternative_post_image !== 'custom_field_2' ) { ?>
    <img src="<?php echo $alternative_post_image; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
    <?php } else { ?>
    <?php
    the_post_thumbnail();
    ?></a>

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,

    I began to help you and then realized that I do not know what is not working.

    That is because you have not defined what is not working.

    It is very hard for someone to try and look at all of your code and determine what is not working.

    What I wpould suggest is this:

    Code one line at a time and see if it works, eg:

    <?php
    //if custom field isn't blank
    if ($alternative_post_image !== '' ) { ?>
    <img src="<?php echo $alternative_post_image; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
    <?php }

    Does this work? If so, try the next piece of code, if not, then determine what in it is not working, ie is it:

    img src="<?php echo $alternative_post_image; ?>

    or

    php the_title()

    or something else?

    Ie ask about one piece of code at a time.

    It is much easier to help you then.

    Thread Starter Kir 2012

    (@kir-2012)

    Hi thanks so much for trying to help, and yes sorry – the first part doesn’t work, I just pasted all of it to try to illustrate.

    The first part of the code doesn’t work.

    If it’s useful I can pull in custom fields using this:

    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php
    	$values = get_post_custom_values("custom_field_name_1"); echo $values[0]; ?>" alt="Featured Image" />
                        </a>

    But I can’t get the first part of the other code to work to pull it in – this bit:

    <?php
    //if custom field isn't blank
    if ($alternative_post_image !== '' ) { ?>
    <img src="<?php echo $alternative_post_image; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
    <?php }

    It pulls in a title but it doesn’t pull in the image.

    Ok, I am going to help you, but this is a lot of work, and I have coded all this type of thing before, so we are going to take it one step at a time ok?

    Does:

    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php
    	$values = get_post_custom_values("custom_field_name_1"); echo $values[0]; ?>" alt="Featured Image" />
                        </a>

    Show an image?

    Also, very important, how are you creating the custom field?

    Haven’t heard back, unsubscribing

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘If there is a CUSTOM_FIELD_1 in post use image in archive, if not use POST_THUMB’ is closed to new replies.