- This topic has 9 replies, 2 voices, and was last updated 5 years, 11 months ago by Tom.
-
AuthorPosts
-
May 2, 2018 at 2:01 pm #565873George
I have a custom post type category archive set to culumns. Is there an easy way of settings the archive blog items to open in a new browser tab without modifying template files?
May 2, 2018 at 9:39 pm #566101TomLead DeveloperLead DeveloperWithout modifying template files? Probably not.
You could maybe use javascript. You want the titles to open the single posts in a new window?
May 3, 2018 at 12:30 am #566208GeorgeYes archive images and titles to open the single items in a new window. Basically anything that links to the single item but it needs to be for that particularly custom post type category archive (“video”).
May 3, 2018 at 8:55 pm #566953TomLead DeveloperLead DeveloperCan you link me to one of those pages? I’ll see if I can write up some javascript for you.
May 7, 2018 at 2:57 pm #570110GeorgeIt’s not easy to link now, I am working locally but basically the page displays a custom post category archive of a custom post type “Video” that belongs to a category called “Videos”. I have already targeted it before using the
if ( is_category( 'videos' ) )
conditional. I am guessing the javascript would need to target the<a>
tagif ( is_category( 'videos' ) )
or something?I wrote this in wp_head hook:
<?php if ( is_category( 'videos' ) ) { ?> <script> (function($){ $(document).ready(function() { $('a').each(function() { var a = new RegExp('/' + window.location.host + '/'); if(!a.test(this.href)) { $(this).click(function(event) { event.preventDefault(); event.stopPropagation(); window.open(this.href, '_blank'); }); } }); }); })(jQuery); </script> <?php } ?>
Didn’t work. I also don’t like writing code in there, I think it’s unsafe and it’s not easy to format.
May 7, 2018 at 8:33 pm #570227TomLead DeveloperLead DeveloperIt’s not unsafe, but it’s not easy to format, that’s for sure.
Try this:
jQuery( document ).ready( function( $ ) { $( '.category-video h2 a' ).each( function() { $( this ).attr( 'target', '_blank' ); } ); } );
Let me know ๐
May 8, 2018 at 6:06 am #570498GeorgeWorks great, but when I am using the Load More button there seems to be a conflict with the javascript and the code doesn’t work anymore on items below where the Load More button was!
Here is the link:
https://www.audiotutorialvideos.comMay 8, 2018 at 9:16 am #570745TomLead DeveloperLead DeveloperAh yea, that’ll happen.
Let’s try this instead then:
jQuery( document ).ready( function( $ ) { $( document ).on( 'click', '.category-video h2 a', function( e ) { e.preventDefault(); var url = $( this ).attr( 'href' ); window.open( url, '_blank' ); }); } );
May 8, 2018 at 9:55 am #570771GeorgeYep, works great now, thank you Tom!
May 8, 2018 at 10:00 am #570772TomLead DeveloperLead DeveloperYou’re welcome ๐
-
AuthorPosts
- You must be logged in to reply to this topic.