WordPress has many ways to hack and tweak to get your desired result, some are simple functions within the loop, whilst some can be hacks right into the core.
I use WordPress frequently in personal and client projects so have built up a list of hacks I use in day-to-day WordPress development.
And today I’m going to share a few of my favorite and most used tricks.
Automatic Change the Date
This is more to save time but is more of a simple PHP function rather than a WordPress specific snippet:
Insert that into your footer.php and your Copyright date will always be correct.
© Copyright - <?php echo date('Y'); ?>
Easy Random Posts
You can get plugins to display random posts, but this way is quick and easy, you could for example style it up and use in your sidebar.php file:
<h1>Random Posts</h1> <?php $rand_posts = get_posts('numberposts=5&orderby=rand'); foreach( $rand_posts as $post ) : ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php endforeach; ?>
Get the First Image & display it
Fetch the first image from the blog posts and display it in the loop. You can use it, for displaying thumbnails in sidebar, footer, related posts, etc.
Paste this code in your functions.php file.
function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_img = $matches [1] [0]; if(empty($first_img)){ //Defines a default image $first_img = "/images/default.jpg"; } return $first_img; }
Next, paste this code in the loop to fetch the first image from the blog posts
<?php echo catch_that_image() ?>
Recent Comments First
WordPress by default displays comments with the newest at the bottom, this is most cases isn’t an issue, but if you run a popular blog new comments could end up a few pages deep in no time.
To combat this you can alter some code in the comment.php page which will show newest at the top.
Find:
foreach ($comments as $comment)
and above it add:
$comments = array_reverse($comments);
Create a Login Form Shortcode
Easily add a custom login form anywhere in your WordPress blog with the below code.
Paste the below code in the functions.php file
function devpress_login_form_shortcode() { if ( is_user_logged_in() ) return ''; return wp_login_form( array( 'echo' => false ) ); } function devpress_add_shortcodes() { add_shortcode( 'devpress-login-form', 'devpress_login_form_shortcode' ); } add_action( 'init', 'devpress_add_shortcodes' );
Next, use the below code where you want to display the login form
[devpress-login-form]
Source: DevPress & WP Recipes
Allow Users To Subscribe To Your Twitter Feed
Another method of getting people to read your tweets and perhaps even follow you on Twitter is to allow them to subscribe to your Twitter feed directly from your blog.
So somewhere in header.php between the <head>tags insert:
<link rel="alternate" type="application/rss+xml" title="BLOG NAME Twitter Feed" href="http://twitter.com/statuses/user_timeline/TWIITER USER ID.rss" />
Related Posts Without A Plugin
Sometimes you might not want to use a plugin for this and might well just opt to have it built in to the loop.
Open single.php and paste in the following snippet:
<?php $tags = wp_get_post_tags($post->ID); if ($tags) { echo 'Related Posts'; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>5, 'caller_get_posts'=>1 ); $rel_query = new WP_Query($args); if( $rel_query->have_posts() ) { while ($rel_query->have_posts()) : $rel_query->the_post(); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php endwhile; } } ?>
The post A Collection Of Useful Hacks for your WordPress Blog appeared first on WPKube.