Tips & Tricks
How to: Open external links in a new window
Over the weekend, I attended WordCamp Denver, and I was asked by John Hawkins how to force links to open in a new tab with out editing the source code. So, today lets learn a simple jQuery trick to open all external links in your site in a new tab or window. We are going to make sure you have jQuery active on your site, you can do this easily in WordPress, since it’s bundled with the latest installations. Use this code in your header: <?php wp_enqueue_script('jquery'); ?>
then, below the wp_head
add the following:
<script type="text/javacript"> var $j = jQuery.noConflict(); $j(document).ready(function() { //external attribute $j("a:not([@href*=http://YOURSITE.com/])").not("[href^=#]") .addClass("external") .attr({ target: "_blank" }); } ); </script>
That’s it, just make sure you change the http://YOURSITE.com to your website.
Update
If you like you can remove the var $j =
and replace all $j
with simply just $
Tips & Tricks
Build a classic MyBlogLog widget
In this post I will show you how I created my custom widget showcasing mybloglog readers.
In your function.php
file add this simple line of code:
<?php
// MyBlogLog
function MyBlogLogWidget()
{
?>
<script type="text/javascript" src="http://pub.mybloglog.com/comm2.php?mblID=ReplaceWithYourID
&c_width=220&c_sn_opt=n&c_rows=6&c_img_size=h&c_heading_text=&c_color_heading_bg=e0e0d4&c_color_heading=E8A02C&c_color_link_bg=e0e0d4&c_color_link=d54e21&c&c_color_bottom_bg=e0e0d4"></script>
<?php }
//register the sidebar 'the widget name', 'the widget function'//
register_sidebar_widget('MyBlogLog Widget', 'MyBlogLogWidget');
?>
That’s it, just replace the “ReplaceWithYourID
” with your mybloglog ID. You can also change the width at c_width=220
to what ever you like.
If you look closely at the code you may also see color codes, you may change them to corresponding hex codes, for example: c_color_bottom_bg=e0e0d4
is the color for the bottom background. e0e0d4
is a light gray, we can change it to ffffff
and make it white.
That’s it, enjoy!
Plugins
How to: Creating an Archive page
Many weblog sites these days have some form of an archive page. In WordPress it might be the built in auto generation of permalinks by: /Month/
and /date/
.
But in some blog’s, for instance this one here (as I said in a previous post) you can’t get the post to paginate or link to http://youblog.com/page/2
if you have nice permalinks active or http://youblog.com/?paged=2
.
By any means, In this theme I have to have an archive link at the bottom. So..
Archive WordPress Plugins
There are many plugins which allows you to automatically create an archive page. The good thing is that you’ll (almost) have nothing to do, and the bad thing is that you may not be able to customize your archives much.
On this blog I have decided to use Clean Archives Reloaded by Viper007Bond. Which you can now see active in my Archive’s page.
If you want to use a plugin, here is a small list:
And of coarse you can always..
..do it your self!
First you would have to create a template file by writing this:
<?php /* Template Name: Archive page */ //please add you necessary code here //ie. your hooks and html code ?> <ol id="archive"> <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts(array( 'showposts' => 100, 'offset' => 10, 'order' =>'DES', 'paged' => $paged, )); $wp_query->is_archive = true; $wp_query->is_home = false; if( have_posts() ) : while( have_posts() ) : the_post(); ?> <li><a href="<?php the_permalink() ?>"><?php $tit = the_title('','',FALSE); echo substr($tit, 0, 25); ?>...</a> - Posted on <?php the_time('j F Y') ?> - Comments (<?php echo $post->comment_count ?>)</li> <?php endwhile; endif; ?> </ol> ?>
You may notice this code:
<?php $tit = the_title('','',FALSE); echo substr($tit, 0, 25); ?>
from a previous post!
Once saved and pasted it to your archives.php
or template-archives.php
file, upload it on your wp-content/themes/yourtheme/
directory.
Then, in WordPress dashboard, create a new page, name it “Archives” (or whatever you want) and select Archive page as your page template.
enjoy!
Tips & Tricks
Limit the characters that display on the_title
Ever wanted to display the title of a post somewhere but limit the amount of characters that are shown? For instance, this post has a very long title, and if I were to use <?php echo the_title() ?>
it would show as follows: Limit the characters that display on the_title.
That may not fit well on one line in lets say a widget or small width div
. So here is a neat trick you can use:
<?php $title = the_title('','',FALSE); echo substr($title, 0, 11); ?>
Pretty simple huh, just note the bold numbers, in this case 11 character would output like this: Limit the c.
Thanks to Tattershall Way for this snippet.
-
Tips & Tricks3 months ago
WordPress Security Hacks
-
Pages3 days ago
Write For Us – Guest Post
-
Showcase2 weeks ago
StylizedWeb.com
-
News5 months ago
How to: Show/Hide any div box with jQuery in WordPress
-
Tips & Tricks4 months ago
Remove the title attribute using jQuery
-
Tips & Tricks2 months ago
How to: show/hide a widget in WordPress with jQuery
-
Plugins2 months ago
Top Membership plugins
-
Tips & Tricks2 weeks ago
Limit the characters that display on the_title