Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1301492

    Hi there

    I’m using the blog grid to show various posts, whose formats are Link (instead of standard). I’ve set the grid to show title, excerpt and read more link as I want the website visitor to have the option of clicking either the heading or link to open the external link.

    The Read More goes to the post only, is there any workaround to force it to open the link instead?

    Thanks
    Richard

    #1301635

    Hey Richard,

    Thank you for the inquiry.

    This should be possible using a custom script. Please try to add this code in the functions.php file.

    // a custom script
    // use external link
    function ava_custom_script_mod() {
        if ( wp_script_is( 'avia-default', 'registered' ) ) {
            wp_add_inline_script( 'avia-default', '
    		(function($) {
    			$(document).ready(function() {
    				$(".post-format-link .more-link").on("click", function(e) {
    					if(e.preventDefault) e.preventDefault();
    					var content = $(this).parents(".slide-content");
    					var header = content.find(".entry-content-header");
    					var external_link = header.find("a").attr("href");
    
    					window.location.href = external_link;
    				})
    			});
    		})(jQuery);
    	');
        }
     }
     add_action( 'wp_enqueue_scripts', 'ava_custom_script_mod', 9999);
    

    Best regards,
    Ismael

    #1301682

    Hey Ismael, there are many reasons why I love Enfold — an amazing support forum is definitely one of them and this works perfectly, thank you. One final question related to the link. I’ve added the following to the functions file to force links to open a new page:

    add_filter('wp_footer', 'avf_add_target_blank', 10);
    function avf_add_target_blank() { ?>
    <script>
    (function($){
        $(window).load(function() {
        	$( ".avia-content-slider .slide-entry-title a, .more-link" ).each(function() {
    			$(this).attr('target','_blank');  
    		});
        });
    	
    })(jQuery);
    </script>
    <?php
    }

    It correctly adds target=”_blank” to the code but for some reason doesn’t open a blank page.

    I’ve tried a.more-link, .more-link a, .read-more-link .more-link a, but still no luck. Am I missing something?

    Thanks
    Richard

    #1301858

    Hi,
    Thank you for your patience, I believe what is occurring is your script fires on page load adding the target attribute, but then Ismael’s script fires on click rewriting the href attribute and removing the target attribute.
    Try removing your script and adjusting Ismael’s for this one var:

    var external_link = header.find("a").attr("href").attr('target','_blank');

    Best regards,
    Mike

    #1301966

    Hey Mike, not a problem, we all need a weekend ;-)

    I’ve adjusted the code but then the site wouldn’t load. I tried changing the PHP version but no luck. I’m running 4.8.1 with PHP 7.4.16

    Thanks
    Richard

    #1302290

    Hi,

    Thank you for following up. :)

    Try to replace the window.location.href with the window.open function. Please look for this line..

    window.location.href = external_link;
    

    .. and replace it with:

    window.open( external_link, '_blank' );
    

    For more options, please check this link.

    // https://developer.mozilla.org/en-US/docs/Web/API/Window/open

    Best regards,
    Ismael

    #1302321

    Thanks Ismael. With a slight tweak it works perfectly, thank you.

    Final code (that works for blog grid) if anyone else needs it:

    // a custom script
    // use external link
    function ava_custom_script_mod() {
        if ( wp_script_is( 'avia-default', 'registered' ) ) {
            wp_add_inline_script( 'avia-default', '
    		(function($) {
    			$(document).ready(function() {
    				$(".post-format-link .more-link").on("click", function(e) {
    					if(e.preventDefault) e.preventDefault();
    					var content = $(this).parents(".slide-content");
    					var header = content.find(".entry-content-header");
    					var external_link = header.find("a").attr("href");
    
    					window.open(external_link, "_blank" );
    				})
    			});
    		})(jQuery);
    	');
        }
     }
     add_action( 'wp_enqueue_scripts', 'ava_custom_script_mod', 9999);
    #1302791

    Hi,

    No problem. Glad we could help. Please feel free to open another thread if you need anything else. We will close this one for now.

    Have a nice day.

    Best regards,
    Ismael

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Blog grid — title, excerpt and read more link with post type link’ is closed to new replies.