-
AuthorPosts
-
May 20, 2021 at 9:30 pm #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
RichardMay 21, 2021 at 2:06 pm #1301635Hey 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,
IsmaelMay 21, 2021 at 4:53 pm #1301682Hey 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
RichardMay 23, 2021 at 7:56 pm #1301858Hi,
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,
MikeMay 24, 2021 at 11:32 am #1301966Hey 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
RichardMay 25, 2021 at 8:59 pm #1302290Hi,
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,
IsmaelMay 25, 2021 at 9:50 pm #1302321Thanks 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);
May 28, 2021 at 11:01 am #1302791 -
AuthorPosts
- The topic ‘Blog grid — title, excerpt and read more link with post type link’ is closed to new replies.