[Support request] Make a specific blog post open in a new browser window

Home Forums Support Make a specific blog post open in a new browser window

  • This topic has 11 replies, 4 voices, and was last updated 1 year ago by David.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #987531
    Chelsea

    Aloha GP!

    I’ve been reading through your forums and I haven’t found the specific answer to my question.

    Can you please assist me with targeting a specific blog post to open in a new browser window. I do not want all of my post to open in new windows but need a specific post (in the blog post feed which I named “Info / Tips”) to open in a new browser tab. The reason is that it’s a post that contains a music link so people can listen to live Hawaiian radio while browsing the website… I need this post to open as a new tab so the music can continue to play while people browse the site on their original tab.

    Thank you!

    #987561
    Leo
    Staff
    Customer Support

    Hi there,

    Hmm not 100% sure if that’s possible but perhaps try the solution here:
    https://generatepress.com/forums/topic/force-open-all-wordpress-blog-posts-in-new-tab-target_blank/#post-612849

    But instead of $('h2.entry-title a').each(function() {

    Try $('.post-xxx h2.entry-title a').each(function() {

    and replace xxx with the ID of the post.

    #988336
    Chelsea

    Aloha Leo – thank you for instructions. I tried it but it did not make the post open in a new window. Here is what I did:

    I added the code to wp_footer in the Appearance / Elements / Add Hook setting. I executed shortcodes and I executed PHP – Priority is set to 10. Display rules is Location: Entire site.

    Here is the script I added:

    <script type=”text/javascript”>
    //<![CDATA[
    jQuery(document).ready(function($) {
    $(‘.post-599 h2.entry-title 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’);
    });
    }
    });
    });
    //]]>
    </script>

    #988390
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    I’m not seeing the code in your source. Can you try clearing your caching plugin?

    #988403
    Chelsea

    Aloha Tom,

    Since it wasn’t working I removed the hook from Elements : )

    I just added the hook back to Elements & also cleared my caching plugin.

    You should be able to see it now.

    Thank you!

    #989010
    Tom
    Lead Developer
    Lead Developer

    Where can I see a link to post 599?

    #989418
    Chelsea

    Hi Tom,

    The link to post 599 is on the https://mauioceanrentals.net/info-tips/ page which is houses the blog. Post 599 is the post named “Live Hawaiian Radio”. Is that what you are asking?

    #989566
    David
    Staff
    Customer Support

    Hi there,

    try this:

    jQuery( document ).ready( function( $ ) {
        $( '.post-599 a' ).click( function( event ){
            event.preventDefault();
            window.open(( this ).href, 'window name', 'features');
        });
    });

    The ‘window name’ and ‘features’ are two arguments of the .open() function – you can leave them as is, or look up the arguments to define specific settings. No need to check execute PHP in the hook

    #991134
    Chelsea

    Aloha David!

    That worked! Thank you!

    Quick question: I looked up the arguments of the .open()funtion but couldn’t find an adjustment for opening the post 599 in a new tab vs. new browser window. Curious what I would add to the code to make it open a new browser “tab” vs. an entire new window?

    Thank you so much!
    Aloha!

    #991348
    David
    Staff
    Customer Support

    You should be able to do this instead:

    jQuery(document).ready(function($) {
    $('.post-599 a').attr('target', '_blank');
    });
    #991922
    Chelsea

    Perfect!

    Thank you so much! You guys are da best!

    Aloha!

    #992125
    David
    Staff
    Customer Support

    Glad we could be of help

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