Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #1423400

    Masonary Gallery
    How do we have only one image open custom link in a new tab. All the rest are to stay within https://localcleaningservices-co-uk.stackstaging.com/xxx
    The ‘Bird and Pest Control’ link.

    #1423440

    Hi whdsolutions,

    Can you try the solution posted on this thread: https://kriesi.at/support/topic/portfolio-items-wont-open-in-a-new-window-from-the-masonry-grid/
    Hope it helps.

    Best regards,
    Nikko

    #1423519

    I tried this and it does not work.

    #1423520

    I have deleted the code now as it is uneccessary if not working.

    #1423522

    This is what is in Enfold Child: Theme Functions (functions.php)

    <?php
    function masonry_links () {
    ?>
    <script>
    window.addEventListener(‘DOMContentLoaded’, function() {
    (function ($) {
    $(document).ready (function () {
    var url = new RegExp(location.host);

    $(‘.av-masonry-entry’).each(function () {
    if( ! url.test($(this).attr(‘href’)) ) {
    $(this).attr(‘target’, ‘_blank’);
    }
    });
    });
    }) (jQuery);
    });
    </script>
    <?php
    }
    add_action (‘wp_footer’, ‘masonry_links’);

    /*
    * Add your own functions here. You can also copy some of the theme functions into this file.
    * WordPress will use those functions instead of the original functions then.
    */

    • This reply was modified 1 year, 2 months ago by whdsolutions.
    #1423662

    We can’t get this to work, we only need to make this open in a new tab:

    Screenshot-2023-10-25-102058

    Is there any way we can make this happen

    #1423686

    Hi whdsolutions,

    Can you give us temporary admin access on the staging site? so we can try to check on the issue further.

    Best regards,
    Nikko

    #1423693

    Of course

    #1423763

    Hi whdsolutions,

    Thanks for giving us admin access.
    I have made the changes on functions.php of your child theme, please review your website.

    Best regards,
    Nikko

    #1423968

    as far as I understood it, only the one link should open in a new tab. The others should open on the same page.

    so maybe use instead:

    function open_custodiapestcontrol_links(){
    ?>
    <script type="text/javascript">
    window.addEventListener("DOMContentLoaded", function () { 
        (function($) {     
            $('.av-masonry-entry[href*="custodiapestcontrol"]').attr('target', '_blank');
        })(jQuery);
    });
    </script>
    <?php
    }
    add_action('wp_footer', 'open_custodiapestcontrol_links');

    or – if you like to open all external links ( links that have a different domain ) in a new tab ( with some exceptions like mailto etc. )
    use:

    function open_external_links_in_newtab(){
    ?>
    <script type="text/javascript">
    window.addEventListener("DOMContentLoaded", function () { 
        (function($) {
          var url = window.location.origin;
          $('a').not('a[href*="'+url+'"], a[href*="mailto:"], a[href^="#"], a[href*="tel:"], a[href*="javascript:;"] ').attr({
            'target':'_blank',
            'rel': 'noopener',
          })
        })(jQuery);
    });
    </script>
    <?php
    }
    add_action('wp_footer', 'open_external_links_in_newtab');

    (remove rel attribute if you do not like to set)

    #1423969

    There is one small catch – both Nikko’s and my solution. Masonries that load further elements with the More Link are not affected by this. You would then have to add attributes, e.g. with MutationObserver, at the moment of entry into the DOM.

    f.e. ( in this case for all links in a Masonry )

    function open_masonry_entries_in_new_tab(){
    ?>
    <script>
    (function($) {
      MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
      var observer = new MutationObserver(function() {
        $("a.av-masonry-entry").attr("target", "_blank");
      });
      observer.observe(document, {
        subtree: true,
        childList: true
      });
    })(jQuery);
    </script>
    <?php
    }
    add_action('wp_footer', 'open_masonry_entries_in_new_tab');
    #1424132

    Works a treat – Thank you so much!

    #1424157

    Hi,

    Great, I’m glad that you got it working. Thanks @guenni007 for helping out :-)

    Best regards,
    Rikard

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