WordPress.org

Ready to get started?Download WordPress

Forums

Trouble using get_post_meta to return custom field (3 posts)

  1. Carrie Dils
    Member
    Posted 2 years ago #

    Hi,
    I'm having trouble with a custom function I wrote and would like another pair of eyes to help me uncover the logic flaw.

    I'm converting a site that was on an old WordPress install (pre support for featured images); the original database included a custom field called 'previewphoto'. My function takes a Post ID and checks to see if there's a featured image (for new post moving forward) or, if not, check to see if there's an existing 'previewphoto'.

    FYI - the previewphoto custom field is located in the wp_postmeta table.

    function getimageURL($thePostID) {
    <em>        //if there is a post thumnbnail, get it!
            //This IF works</em>
    	if (has_post_thumbnail($thePostID)){
    		$image_array = wp_get_attachment_image_src( get_post_thumbnail_id($thePostID ), 'single-post-thumbnail' );
    		$image = $image_array[0];
    <em>        //Else if we're dealing with a single post and a previewphoto exists, get it!
            //This else works as expected</em>
    	} elseif (is_single($thePostID) && !is_null(get_post_meta($thePostID, 'previewphoto', true))) {
    		$image = get_post_meta($thePostID, 'previewphoto', true);
    <em>        //Else if we're dealing with an archive post and a previewphoto exists, get it!
            <strong>// THIS RETURNS NOTHING</strong></em>
    	} elseif (is_archive() && !is_null(get_post_meta($thePostID, 'previewphoto', true))) {
    		$image = get_post_meta($thePostID, 'previewphoto', true);
    	}
    <em>        //If all else fails, show a default image</em>
    else {
    		$image = "noimage.jpg";
    	}
        return $image;
    }

    Is there another function I should use instead of get_post_meta? The function works as expected everywhere except on archive pages.

    Any suggestions are appretiated!

  2. Carrie Dils
    Member
    Posted 2 years ago #

    p.s. Sorry for the random html markup in the code snippet. I know that's not where the problem is. ;)

  3. Carrie Dils
    Member
    Posted 2 years ago #

    Okay, I ended up taking a different tack. I'll post it here in case it helps somebody else.

    Instead of using get_post_meta, I went with a combo of get_post_custom and array_key_exists.

    function getimageURL($thePostID) {
    
    	$custom_fields = get_post_custom($thePostID); 
    
    	if (has_post_thumbnail($thePostID)){
    		$image_array = wp_get_attachment_image_src( get_post_thumbnail_id($thePostID ), 'single-post-thumbnail' );
    		$image = $image_array[0];
    	}
    	elseif (is_single($thePostID) && !is_null(get_post_meta($thePostID, 'previewphoto', true))) {
    		$image = get_post_meta($thePostID, 'previewphoto', true);
    	} elseif (is_archive() && !is_null(array_key_exists("previewphoto",$custom_fields))) {
    		$image = $custom_fields['previewphoto'][0];
    	} else {
    		$image = get_bloginfo('stylesheet_directory')."/images/categories/noimage.jpg";
    	}
        return $image;
    }

    This may not be elegant at all, but it finally works, so I'm happy. :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.