Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Getting a 404 error on uploaded images in custom fields section

Following the video, I created a public page on my test site for the custom field My Art. As far as I can tell, everything works fine in terms of the specific fields. The images are uploaded into my media library and show up fine on the back-end. It's only when I try to preview them prior to publishing that I get a 404 error that the page can't be found. When I publish everything on the front-end, the "My Art" page does work, but none of the images are loading.

I've tried using totally different images but that doesn't seem to fix anything.

If anyone has any suggestions as to why this might be happening, I'd be grateful.

(If anyone wants to see the example, it's live at http://wp.andrew-codes.com/?page_id=19)

There's something wrong with the way you're linking to your images. Look at the image source:

<img src="7, , example-1, , , image/png, http://wp.andrew-codes.com/wp-content/uploads/2014/10/example-1.png, 400, 400, Array" alt="Example image of Example 1">

Thanks. That's definitely weird. I'm not exactly sure why the image link is populating like that. I went into the custom field and changed the image size to see if I could fix it or see a change, but no luck so far. I'll probably need to take another look at it tomorrow with fresh eyes.

6 Answers

From what i see here (this is one of your posts)

<img src="23, example skate, Screen Shot 2013-10-09 at 12.41.36 AM, , , image/png, http://wp.andrew-codes.com/wp-content/uploads/2014/10/Screen-Shot-2013-10-09-at-12.41.36-AM.png, 666, 664, Array" alt="Example image of Example 2">

you have the field already, the only issue is how you echoing it back.

Now, go to your custom fields in your admin panel, then go to your field group associated with your art post type.

The image field ca return and object or just the url of the image

http://awesomescreenshot.com/0243qovjfb

In your case, you have it returning an object, and you have two solutions here:

  1. Change Return Value to Image URL and your current code will work as expected.
  2. Add this block before your img tag
<?php $image = get_field('image'); ?>

then in your img src

<p><img src="<?php echo $image['url']; ?>" alt="Example image of <?php the_title(); ?>"></p>
        <p><?php the_field('description'); ?></p>

That's happening because your echoing the while array (image object).

you need to specify the url key of that array.

<?php echo $image['url']; ?>

what function are you using to get the image?

Sorry, what do you mean "option rather than a URL" ? do you mean object? and not option? if so,

This is an image object

Array
(
    [id] => 363
    [alt] => 
    [title] => me
    [caption] => 
    [description] => 
    [mime_type] => image/png
    [url] => http://www.mo7sin.dev/wp-content/uploads/2011/10/me.png
    [width] => 250
    [height] => 300
    [sizes] => Array
        (
            [thumbnail] => http://www.mo7sin.dev/wp-content/uploads/2011/10/me-150x150.png
            [thumbnail-width] => 150
            [thumbnail-height] => 150
            [medium] => http://www.mo7sin.dev/wp-content/uploads/2011/10/me.png
            [medium-width] => 250
            [medium-height] => 300
            [large] => http://www.mo7sin.dev/wp-content/uploads/2011/10/me.png
            [large-width] => 250
            [large-height] => 300
        )

)

as you can see, you have alot of keys available to use, and you will in a future project.


Now if you choose Image ID to be returned, you will receive the first key only which is ID ** [id] => 363**

and you will get it as integer 363 , WordPress has many functions would receive that ID as its parameter

like http://codex.wordpress.org/Function_Reference/wp_get_attachment_url for example.

I'm not entirely sure if this is what you're looking for, but in my content-art file (the one that works on the page I linked to above), I have the following, image-related code:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

Then there's some different classes in the header portion like entry-header, entry-title, entry-meta, before the image code appears in a div class entitled "entry-content", like so:

<p><img src="<?php the_field('image'); ?>" alt="Example image of <?php the_title(); ?>"></p>
        <p><?php the_field('description'); ?></p>

That's the only type of image-related code that came in the downloadable material so if it's not there, it's possible I messed up creating the actual custom post type and 'art' field during the video tutorial and simply deleting it and starting over might work...since I'm not totally sure where I'd even include the specifications of the URL key (unless it's in a file like page.php).

Hey there,

Thanks so much for this detailed explanation. I tried the first option (switching from it returning an object to a URL) and it worked fine (which is weird, because I thought that's what I'd first chosen and then switched it over when it wasn't working initially).

If you don't mind my asking, what would be an example of a time when you'd want to return an option rather than a URL? What types of content would pop up if that option were selected and you published it live? I guess I'm unsure about that and the other option, which is Image ID. Clearly I still have a lot to learn.

Thanks again for the help with this. I literally just started trying to understand how custom page types and fields work last night. The customizability of a site with these features included is really great.

Yeah, that was what I was wondering (what the object and ID choices would look like and how they would perform if they were chosen instead of the image URL one).

I obviously still have a lot to learn, but this was a really good starting point, and the images work now on my demo site.

Thanks again for all your help and patience!

Your welcome , don't ever hesitate to ask :)