Celebrating 7 Years! Get up to $30 off GeneratePress Premium!

[Resolved] Custom post type category archive links to open in a new browser window

Home Forums Support [Resolved] Custom post type category archive links to open in a new browser window

Home Forums Support Custom post type category archive links to open in a new browser window

  • This topic has 9 replies, 2 voices, and was last updated 3 years ago by Tom.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #565873
    George

    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?

    GeneratePress 2.1
    GP Premium 1.6.2
    #566101
    Tom
    Lead Developer
    Lead Developer

    Without modifying template files? Probably not.

    You could maybe use javascript. You want the titles to open the single posts in a new window?

    #566208
    George

    Yes 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”).

    #566953
    Tom
    Lead Developer
    Lead Developer

    Can you link me to one of those pages? I’ll see if I can write up some javascript for you.

    #570110
    George

    It’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> tag if ( 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.

    #570227
    Tom
    Lead Developer
    Lead Developer

    It’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 ๐Ÿ™‚

    #570498
    George

    Works 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.com

    #570745
    Tom
    Lead Developer
    Lead Developer

    Ah 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' );
        });
    } );
    #570771
    George

    Yep, works great now, thank you Tom!

    #570772
    Tom
    Lead Developer
    Lead Developer
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.