Trouble using get_post_meta to return custom field
-
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!
- The topic ‘Trouble using get_post_meta to return custom field’ is closed to new replies.