Support » Developing with WordPress » image.php and video.php problems

  • Resolved kernix

    (@kernix)


    I could have sworn I had this fixed. I created video and image.php which displays the video\image title, posted date, author link, link to view the image or video, and a large thumbnail of the image. Below that, I pulled in the description and caption and a couple of other fields.

    The View Image or Watch Video link should take you to the URL where ‘uploads’ is in the address and the image is on a black field. But instead, that link returns you to the same page. For example, the image of a compass brings me to http://localhost/desndev/wordpress/compass/ when I click view attachment page and the template being loaded is image.php. But when I click View Image I want the address to change to http://localhost/desndev/wordpress/wp-content/uploads/2020/11/compass.jpg.

    That’s the first problem I’m having in local dev.

    On my live site when I click view attachment page I go directly to the address with uploads in the address, my image.php is not loading even though my live site and local dev have the same files.

    I thought I was done but something is out of sync. Why would my image.php and video.php pages load in local dev showing all the fields * have on the page, but on the live site there is no imgae.php page being shown? What am I missing? And why can’t I get the link to go to the actual image of video uploads link page? My attachment.php file is fine for audio, text and pdf files – I see the attachment page with all the details then the link takes you to the upload link.

    Oh, I forgot – something else was working and now doesn’t work – the previous and next image links. Only previous or net link show, not both of them??? Here is the code I have:

    image.php for the attachment link:
    <span class="full-size-link"><?php esc_html_e('| ', 'tower') ?><a href="<?php esc_url(wp_get_attachment_url()); ?>"><?php echo esc_html( $metadata['width'] ); ?><?php echo esc_html( $metadata['height'] ); ?>View Image</a></span>

    Here is what I am using for the open \ download link for all other attachments (and I did try $post->guide in image.php):

    <a href="<?php echo esc_url($post->guid); ?>">Open / Download</a>
    

    image navigation:

    <nav id="image-navigation" class="navigation image-navigation">
        <div class="nav-links">
        <?php previous_image_link( false, '<span class="previous-image">' . __( 'Previous image', 'tower' ) . '</span>' ); ?>
        <?php next_image_link( false, '<span class="next-image">' . __( 'Next image', 'tower' ) . '</span>' ); ?>
        </div>
    </nav>
    • This topic was modified 1 month, 3 weeks ago by kernix.
Viewing 9 replies - 1 through 9 (of 9 total)
  • You had trouble before about your site running the wrong theme. Is that affecting you still?
    You should use esc_attr for attributes, not esc_html.
    You shouldn’t use the guid at all. That is not for output. It is for RSS feeds knowing what the original unique ID of the post is.
    Depending on the query, there might not be a previous or next. If you got to the attachment from a gallery, there is. But from most anywhere else, there is not.

    Moderator bcworkz

    (@bcworkz)

    Inappropriately using the GUID could be the source of your problem. While the attachment GUID typically is the URL of the media file, there’s nothing that says it has to be. The only requirement is it be globally unique for that attachment. Get the media URL from the right place with something like
    wp_get_attachment_image_src( $post->ID, 'full' )[0]

    • This reply was modified 1 month, 2 weeks ago by bcworkz. Reason: added 'full' parm
    Thread Starter kernix

    (@kernix)

    I never had trouble with my local or live site running the wrong theme. I should have used esc_attr instead – although, that is on the width and height and I don’t know where I got that or why it is there. I removed those and it made no difference that I could see. I’m only viewing the attachment page from the media library. I still have to figure out why attachment pages are used but I thought it was for SEO reasons.

    I got the guid thing from doing a var_dump or var_print, can’t remember which one. I’m also using post_type and post_mime_type to get that on the attachment page. I don’t know what else you are supposed to put on an attachment page.

    Now the next AND previous image links are back?????

    I replaced $post->guid with wp_get_attachment_image_src( $post->ID, ‘full’ )[0] and I don’t see a difference but I’ll use that if you say it is better. But I’m still not getting the link to the media library file, it’s linking to the attachment page – it’s linking back to itself.

    The only thing I changed after I had everything working was creating editor-style.css. I can’t see how that would affect anything but thought I’d mention it.

    Here is the original and a simpler one that I got from a course I took – both link back to the attachment page and not the upload file:

    Original
    <a href="<?php esc_url(wp_get_attachment_url()); ?>"><?php echo esc_html( $metadata['width'] ); ?><?php echo esc_html( $metadata['height'] ); ?>View Image</a>

    Simple one:
    <a href="<?php esc_attr(wp_get_attachment_image( $attachment->ID, 'full' ) ); ?>">View Image</a>

    I don’t understand and am confused – I know I had this working. I have no job, I’m barely making any money and I would like to get back to designing my site and then try to get some free clients. I don’t understand why I can’t get a link to the media file. I’ll just remove the link to view it since I guess that is redundant.

    Thread Starter kernix

    (@kernix)

    GOT IT – SORRY!!!!

    I used…

    <?php echo esc_url( wp_get_attachment_image_src( $post->ID, 'full' )[0] ); ?>

    for the View Image link as well as for displaying the image on the image.php attachment page. I’ll mark this solved and thanks for the replies!

    Thread Starter kernix

    (@kernix)

    No, there is still a disconnect between local host and live site. I added an image, video, audio file, and an .rtf file (.txt wouldn’t upload) thinking that the files needed to be attached. From the media library, when I click view attachment page it takes me to the uploads link – not to my attachment pages (attachment.php, image.php, video.php).

    Could it be the PHP version? I am using XAMPP which I downloaded about a year ago.

    Moderator bcworkz

    (@bcworkz)

    I doubt it’s the PHP version, though it wouldn’t hurt to update XAMPP to the latest stable version. The image src code of course only works for images. For other media it returns false. To get the URL to the original uploaded file, regardless of type, this should work:
    content_url( get_post_meta( $post->ID, '_wp_attached_file', true ))

    Be sure $post is an attachment post and not a blog post or page. If the URL returned by this code still leads to an attachment page and not the media file, I would suspect the meta data itself is incorrect.

    BTW, neither of the snippets you got from that course would get you the media file URL. The first obviously gives you the attachment page. The “simpler” one is plain incorrect because wp_get_attachment_image() returns the entire HTML <img> tag, not just an URL.

    Thread Starter kernix

    (@kernix)

    Updating XAMPP is a lot of work. I’ll get to that eventually. Let me try that code and I’ll get back to you. Hopefully, that will work for the video template as well.

    You really don’t need a separate template for attachments and for each media type. The default works pretty well.
    In my theme, I use single.php which handles them all the same. I add this one filter for attachments:

    function twenty8teen_attachment( $p ) {
    	$apost = get_post();
    	if ( ! wp_attachment_is( 'video', $apost ) && ! wp_attachment_is( 'audio', $apost ) ) {
    		// non-video and non-audio - use large size or icon
    		$p = '<p class="attachment">';
    		$p .= wp_get_attachment_link( $apost->ID, 'large', false, true);
    		$p .= '</p>';
    	}
    	$p .= $apost->post_excerpt ? ( '<p>'. wptexturize( $apost->post_excerpt ) . '</p>' ) : '';
    	return $p;
    }
    add_filter('prepend_attachment', 'twenty8teen_attachment');

    And I simply use the_post_navigation(); for the previous and next links. It all works great.

    Thread Starter kernix

    (@kernix)

    Ok, let me try making some of those changes – thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.