WordPress.org

Forums

Twenty Twelve
Force Hyperlinks to open in new tab. (2 posts)

  1. jstevenot
    Member
    Posted 1 year ago #

    Does anyone know what the code would be (or what code to edit) to make my hyperlinks open in a new tab - this is different from the "add link" option within a post/page editor. I am trying to do it with the post title.

    If you click on my post title, I want it to open in a new tab instead of moving forward into my site.

  2. Maria Antonietta Perna
    Member
    Posted 1 year ago #

    Hi jstevenot,

    To force a link to open in a new tab you need to add the target="_blank" attribute to the link. As far as I know, it's recommended that you let users decide whether they want to open a link in a new tab or not, however.

    In any case, to do it, you could create a child theme for Twenty Twelve and duplicate the content.php file from the parent. Find this line of code:

    <h1 class="entry-title">
    <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    </h1>

    Modify it by adding the target attribute to the link, like so:

    <h1 class="entry-title">
    <a href="<?php the_permalink(); ?>" target="_blank" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    </h1>

    This theme has other content templates. Check if any of these need the same treatment. For instance, content-aside.php has a linked title.

    I hope this helps.

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic

Tags