Archived topic
Archive page - make link open in new post
13 replies · Started by Janani on May 14, 2020
Hi
In my archive page, how do I make the link to the post open in a new page?
Warm regards,
Hi there,
Try this solution:
https://generatepress.com/forums/topic/force-open-all-wordpress-blog-posts-in-new-tab-target_blank/#post-612849
Dear Leo
Thanks. Apologies, but I am a beginner, so wanted to check with you where I would add this code. This does not seem to be a normal CSS code.
Hi there,
that code is Javascript.
To add that to your site you would:
1. Create a new hook element.
https://docs.generatepress.com/article/hooks-element-overview/
2. Add the code to the hook content.
3. Select the WP_Footer hook
4. Set the Display Rules for the pages where you need this could to be fired.
Dear David
Thanks I tried it but it did not work. Pls let me know what I did wrong.
I chose wp_footer, ticked on execute shortcodes, execute PHP, priority 10.
Below is this is the script I used:
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function($) {
$('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>
You don't need to check to execute shortcodes and execute PHP but I do think you need to load jQuery like this:
https://generatepress.com/forums/topic/shortcode-does-not-show-in-front-end/#post-475755
Adding PHP: https://docs.generatepress.com/article/adding-php/
Dear Leo
Do you suggest I install a plugin for this purpose? Is there no way I can do this without a plugin?
Warm regards,
Hi there,
try this Javascript instead:
<script>
jQuery(document).ready(function($){
$('.entry-title a').attr('target', '_blank');
});
</script>
Dear David
This is perfect. It worked! Thank you very much.
You're welcome
Hi David,
This issue was resolved, but recently I noticed that the post does not open on a new page. Could there be any reason for this?
Warm regards,
I cannot see the code added to your site.
Where did you add it? If it is using a Hook - make sure it is still published and the display rules are correct
Thanks David. I guess the hook got modified accidentally. This issue is resolved.
Glad to hear that