Skip to content

Instantly share code, notes, and snippets.

@aldolat
Last active May 2, 2020 07:38
Show Gist options
  • Save aldolat/5108b338155f71c19e6b6d5f55abbdf4 to your computer and use it in GitHub Desktop.
Save aldolat/5108b338155f71c19e6b6d5f55abbdf4 to your computer and use it in GitHub Desktop.
Add attribute target="_blank" to all links children of "posts-in-sidebar" class.
<?php
/**
* Register scripts.
* Add this function to your functions.php file or to your functions plugin.
*
* @since 1.0.0
*/
function posts_in_sidebar_hacks() {
wp_register_script(
'pis_scripts',
get_stylesheet_directory_uri() . '/scripts/pis-hacks.js',
array( 'jquery' ),
'1.0.0',
true
);
wp_enqueue_script( 'pis_scripts' );
}
add_action( 'wp_enqueue_scripts', 'posts_in_sidebar_hacks' );
/**
* Add the `target="_blank"` attribute to Posts in Sidebar links.
* Activate one of them only (LINE A or LINE B), according to your needs!
*/
jQuery(document).ready(function(){
// Add the `target="_blank"` attribute to all links of Posts in Sidebar.
// Targets any link child of .posts-in-sidebar class.
// Deactivate this line, in you want line B, prepending a double slash (//) in front.
// Line A:
jQuery('.posts-in-sidebar a').attr('target','_blank');
// Add the `target="_blank"` attribute only to Posts in Sidebar title links.
// Targets any link with .pis-title-link class.
// Deactivate this line, in you want line A, prepending a double slash (//) in front.
// Line B:
jQuery('a.pis-title-link').attr('target','_blank');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment