Support » Plugins » custom URL in Jetpack tile gallery

Viewing 12 replies - 1 through 12 (of 12 total)
  • I couldn’t find a solution to this so I modified the jetpack plugin to use the image alt-text (if it exists) instead of the image URL. There’s probably a more elegant solution but I was in a hurry and it works for my purposes.

    The file that I changed: ../wp-content/plugins/jetpack/modules/tiled-gallery/tiled-gallery.php

    There are different functions for different gallery sizes (rectangular_talavera, square_talavera, etc) that all use the link, so I created a new function for the link logic:

    private function get_link_url( $image ){
    	$alt_text = get_post_meta($image->ID, '_wp_attachment_image_alt', true);
    	if( $alt_text != ""){ // should probably check if it's a URL with regex...
    		return $alt_text;
    	}
    	$link = $this->get_attachment_link( $image->ID, $orig_file );
    	$search_text = "/project/";
    	$project_pos = strpos($link, $search_text);
    	if($project_pos !== FALSE){// this checks if the link is already a project and truncates the URL at the project itself instead of the project picture, since this is was the primary use case in my project
    		return substr($link, 0, strpos($link, "/", $project_pos + strlen($search_text)) + 1);
    	}
    	return $link;
    }

    This needs to be used in the two functions where the link is inserted. I found it in two places: rectangular_talavera and square_talavera, where I changed
    $link = $this->get_attachment_link( $image->ID, $orig_file );
    to
    $link = $this->get_link_url($image);
    which utilizes the new function above.

    Voila, using the gallery image alt-text (or parent project) as the link instead of the image URL.

    Thread Starter 水野史土

    (@ounziw)

    Thanks, ntrrobng.

    I will use your code in my test website.
    I will report the result in a few days.

    Thread Starter 水野史土

    (@ounziw)

    I applied this code in WP3.7.1.
    It seems that it works.

    Thanks, ntrrobng.

    Works perfectly!
    WP 3.8.1
    Many thanks.

    Hi,

    I tried this method, paste

    private function get_link_url( $image ){
    	$alt_text = get_post_meta($image->ID, '_wp_attachment_image_alt', true);
    	if( $alt_text != ""){ // should probably check if it's a URL with regex...
    		return $alt_text;
    	}
    	$link = $this->get_attachment_link( $image->ID, $orig_file );
    	$search_text = "/project/";
    	$project_pos = strpos($link, $search_text);
    	if($project_pos !== FALSE){// this checks if the link is already a project and truncates the URL at the project itself instead of the project picture, since this is was the primary use case in my project
    		return substr($link, 0, strpos($link, "/", $project_pos + strlen($search_text)) + 1);
    	}
    	return $link;
    }

    to tiled-gallery.php

    and ectangular_talavera and square_talavera where I changed
    $link = $this->get_attachment_link( $image->ID, $orig_file );
    to
    $link = $this->get_link_url($image);

    then my website went blank.

    Not sure which steps I did wrong or just this method not fit to my theme?

    http://hxxa.info

    THANKS A LOT

    Hey sorry,

    I just fixed the problem.. Some php error I think.

    Another issue for me is “how to open the url in new tab by coding in this?”

    THANKS!

    To open a link in a new tab, you need to add a target to the link anchor in the HTML (see stackoverflow answer below), which is built in the rectangular_talavera and square_talavera functions.

    http://stackoverflow.com/questions/6296013/how-can-i-open-a-link-in-new-tab-and-not-new-window

    @ntrrobng

    Many thanks!! I think I googled to this stackoverflow forum before, just I really don’t know how to add those code into the one you gave us. Hope you wouldn’t mind helping me how to open the custom url in new tab for tiled gallery!

    THANKS!

    If you want all of them to display in a new tab, look for the following in the same file:
    <a href="
    Replace with
    <a target="_blank" href="

    There will be more than one place that this occurs. If you only want it to open in a new tab if the alt text exists, it will take a bit more modifications…

    Oh missed some. Also change this:
    <a border="0" href="
    to this:
    <a border="0" target="_blank" href="

    Sorry this is horribly ugly.

    Hey,

    Really thanks but I tried to replace those parts (4 in total) still not working! maybe I should just code. But it’s very kind of you to reply me 🙂 Hope I could find out how soon! MANY THANKS!!!!

    It would be wonderful if you could see more methods for this issue!!

    Hi ntrrobng,

    Thanks for the code, it works perfectly! One question — Any idea on a work around if I want some links to open in a new tab, but not all?

    Appreciate the advice!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘custom URL in Jetpack tile gallery’ is closed to new replies.