• Resolved gumatera

    (@gumatera)


    I am trying to change the gallery link to file, instead of attachment page, and it keeps linking to attachment page.

    WordPress 6.1.1 version.

    Is there any way I can force the links of all existing galleries to open their picture files? Maybe extra some code in functions.php?

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello,

    Try this, in your theme’s function file, add:

    function modify_gallery_link( $link, $post_id ) {
        $post = get_post( $post_id );
        if ( $post->post_type == 'attachment' ) {
            $link = wp_get_attachment_url( $post_id );
        }
        return $link;
    }
    add_filter( 'wp_get_attachment_link', 'modify_gallery_link', 10, 2 );
    

    What this code does is it adds a filter to the wp_get_attachment_link function, which is called when WordPress generates the link for an attachment page. The filter callback function, modify_gallery_link, checks if the attachment is an image and if so, replaces the link with the image file URL.

    Once you add this code to your functions.php file, all existing galleries should now link directly to the image files instead of the attachment pages.

    Thread Starter gumatera

    (@gumatera)

    Thanks @abretado1985, but when I updated functions.php with your code, all galleries were turned into lists of SRCs of media files, as you can see on https://sh-pro42.teste.website/~hgma4011/devsindifisco.org/eventos-passados/entrega-carteiras-funcionais-2020

    I need to keep the galleries structure on frontend (list of thumbnails) and force thumbnails link to the sources of media files.

    Maybe we could change your code to something like:

    <ul> <!–Before the loop of items in gallery–>
    <Start loop>
    <li><a href=”LINK-TO-FILE-[ID-OR-ARRAY]”><img src=”GALLERY-THUMBNAIL-[ID-OR-ARRAY]”></a></li>
    <Finish loop>
    </ul>

    I Just dont know the syntax, but I think It would work.

    Can you help me with this?

    Real quick, are you using a shortcode for the gallery such as [gallery] ? If so, have your tried adding link="file" within the shortcode?

    Please remove the functions code I provide and let me take a look at your page again.

    Thread Starter gumatera

    (@gumatera)

    @abretado1985 I really appreciate your help.

    I am glad I found the solution below:

    I created a single-my-category-name.php, and put the following code:

    <?php
    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'medium' );
    $imagem = $thumb['0'];
    $atributos = parse_atts(get_the_content());	
    ?>
    
    ((Some page content here, before the gallery....))
    
    <?php
    
    if ($attriif ($attributes) {
       $ids = explode(',',$attributes['ids']);
          if ($ids) {
             echo '<ul class="gallery-class">';
                foreach ($ids as $id_attachment) {
                $img_thumb = wp_get_attachment_image_src( $id_attachment );
                $img_attachment = wp_get_attachment_image_src( $id_attachment, 'large' );
    ?>
    
    <li class="gallery-li">
    <a href="<?= $img_attachment[0]; ?>" rel="prettyPhoto[<?= get_the_ID(); ?>]">
    <img src="<?= $img_thumb[0]; ?>" class="gallery-img">
    </a>
    </li>
    																
    <?php
                } 
             echo '</ul>';
          }
    }													
     ?>

    Note: I used
    rel=”prettyPhoto[<?= get_the_ID(); ?>]”
    to open the images in light box of Pretty Photo plugin.
    The code above inserts the ID of each gallery picture between the brackets.
    If you don’t use this plugin, take this part off your code.

    Here is a simpler way (I think), using the code you provided as reference, add the following code to functions file:

    function custom_gallery_shortcode( $atts ) {
        $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'medium');
        $imagem = $thumb['0'];
        $attributes = shortcode_atts( array(
            'ids' => ''
        ), $atts );
    
        if ($attributes) {
            $ids = explode(',', $attributes['ids']);
            if ($ids) {
                $output = '<ul class="gallery-class">';
                foreach ($ids as $id_attachment) {
                    $img_thumb = wp_get_attachment_image_src($id_attachment);
                    $img_attachment = wp_get_attachment_image_src($id_attachment, 'large');
                    $output .= '<li class="gallery-li"><a href="' . $img_attachment[0] . '" rel="prettyPhoto[' . get_the_ID() . ']"><img src="' . $img_thumb[0] . '" class="gallery-img"></a></li>';
                }
                $output .= '</ul>';
                return $output;
            }
        }
    }
    add_shortcode( 'custom_gallery', 'custom_gallery_shortcode' );
    

    Now you can use the shortcode [custom_gallery ids="123,456,789"] in your WordPress pages or posts to display the gallery, where 123,456,789 are the IDs of the images you want to display in the gallery.

    Thread Starter gumatera

    (@gumatera)

    Wow, Thank You very much!!!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WordPress Gallery not linking to file’ is closed to new replies.