QUICK TIP - WordPress tricks – The most useful tricks you should know

0 1,236,174
Most Useful .htaccess Tricks for WordPress | A&H Business Technology
Related posts

First, open your theme’s comments.php template and add the following code snippet in the (X)HTML element that contains the comment information:

After changing the email address to that of your own (i.e., the one used for your WP Admin user profile), style it up with a little CSS:

You probably don’t want your author comments to appear with yellow text on a red background, but you get the idea. Anything is possible.

For blogs with multiple authors, you can style each of their comments differently as well by using something like this:

Replace each of the email addresses with those of the various authors. Style to taste.

Easy Random Posts 

Newer versions of WordPress enable easy randomizing of posts by using the (relatively) new orderby=rand parameter in any query_posts loop:

<?php query_posts('orderby=rand"); ?>

But for older versions of WordPress, this randomizing functionality must be added manually. Here is a quick and painless plugin that will enable you to randomize post queries in older versions:

Once activated, invoke the randomness by adding the random=true parameter in your query_posts loop:

Here, we are specifying cat=11 for the category and showposts=11 for the number of posts to display. There are many other parameters available as well, so knock yourself out.

Credit: rembem

Display Dates for Groups of Posts 

In order to display the date that a post was published, you have two options:

  • <?php the_time(); ?> — displays the date for each and every post
  • <?php the_date(); ?> — displays the date only once for each group of posts published on a certain day

So, if you want to list the post date next to each post, use the the_time(). If, on the other hand, you have multiple posts on any given day, use the_date() to list the post date only once for each days’ posts. Something like this:

See the difference? Good, because I’m not going to explain the concept any further. Instead, I will move on by showing you how to use either template tag in your loop. This really doesn’t need explaining either, but for the sake of completeness, here it is. First, the_time() tag:

This loop will output the post title, date, and content for every post. Alternately, here is how to use the_date tag:

This loop will output the post title and content for every post, and also output the date for each group of posts published on any given day, as explained above. Notice we are adding the paragraph elements for the post date based on whether or not the date is actually output. If it is, then the date will be wrapped in <p> elements; if it’s not, no <p> elements will be output, thereby preventing repetitive sets of empty paragraph elements. Make sense? Good. Here are the available parameters for the nifty the_date() template tag:

<?php the_date('date format', 'before the date', 'after the date'); ?>

Display a Sticky Post in the Sidebar 

Here is a juicy little nugget that displays a “sticky” post in the sidebar of your theme:

This method employs a second loop to display the sticky post, so it may be placed just about anywhere in your theme’s template files. You can also change the number of days that the post will remain sticky by editing the expiration variable with a number other than "2". For example, to set a duration period of seven days, you would use this:

$goAwayDate = time() - (60 * 60 * 24 * 7);

Credit: The Closet Entrepreneur

Display Latest Comments without a Plugin 

Good rule of thumb when working with WordPress: don’t use a plugin if you can acheive the same functionality without one. Case in point: displaying latest comments on your blog. Sure, you could always install a plugin to do it for you, but you really don’t need to. In fact, it’s actually easier to display recent comments without a plugin. SImply add the following code to the desired location within your theme’s template files:

This code will display the 5 most recent comments in the following (X)HTML output format:

By editing the commented lines in the PHP script, you may specify alternate number of comments and characters.

Display Most Commented Posts without a Plugin 

Here is another trick that will enable you avoid yet another needless plugin. This code results in the display of your most-commented posts in list format:

This code may be modified to display any number of posts by changing the LIMIT from 10 to whatever you wish. You may also change the display order from DESC (descending order) to ASC (ascending order).

Once in place, this code will display a list of the 10 most-commented posts in the following (X)HTML output format:

Change Permalinks from Date-Based to Post-Date Only 

Here is an HTAccess method for switching your permalinks from the lengthy date-based format (i.e., http://domain.tld/2009/03/03/post-name/) to the concise post-name format (i.e., http://domain.tld/post-name/).

To do so, first login to the WordPress Admin and switch your permalink structure from this:

..to this:

This change will ensure that all future posts are located at the new post-name-only URL, but we still need to redirect all requests for existing posts via the old date-based URL format. This is easily accomplished with a single directive in your site’s root HTAccess file:

And that’s all there is to it! Remember to test vigorously to convince yourself that everything is working properly.

Source: AH Business Technology

Test for Sub-Pages 

Until WordPress includes an is_subpage() function, here is a manual method of testing for sub-pages:

Further, here are some other ways to use this method to test for different combinations of pages and sub-pages:

Note that the numbers 12, and 3 represent the IDs of the target parent pages.

Multiple Widgetizable Sidebars 

Instead of using multiple if statements to include multiple widgetizable areas, WPEngineer shows us how to do it a better way:

Source: wpengineer

Remove Fancy Quotes from Comments 

Prevent invalid markup and sloppy code by disabling WordPress’ automatic generation of “fancy” or “curly” quotes where they fail the most: your comments area.

Only one simple line of code is required, placed into your theme’s functions.php file:

remove_filter(‘comment_text’, ‘wptexturize’);

Display a List of All Untagged Posts 

Here is an easy way to display a list of all untagged posts. All that’s required is a custom loop and a quick check of the get_the_tags variable. Here is the code to make it work:

Place that snippet into the theme file of your choice and navigate to that page in a browser to see a permlink-linked list of all posts that have not yet been tagged. Even better, next to each post title will be a an easy-access “Edit” link that will make it easy to quickly edit each of your untagged posts and add some tags, if necessary. Nothing on this planet could be easier. Almost.

Easy Display of Custom Headers, Footers, and Sidebars 

WordPress 2.7 includes new functionality that makes it super-easy to include custom headers, footers, and sidebars into your theme. Normally, you include the default files with the following tags:

These will include the files named header.phpsidebar.php, and footer.php, respectively.

Now, to include alternate versions of these files, simply include the name of the file as a parameter in its associated tag. Here is an example that should illustrate the idea:

These will include the files named custom-header.phpcustom-sidebar.php, and custom-footer.php, respectively.

We can now use this new functionality to get fine-grained with includes. For example, if we wanted to display a custom footer for the “Bananaz” category, we could use the following code:

Source: WordPress Codex

A Better Way for Users to Logout 

The old way of displaying a “Logout” link for your users looks like this:

Now, since WordPress 2.7, we can simplify this hodgepodge with a sleeker, cleaner, built-in template tag:


Display a Custom Message on a Specific Date 

Using a snippet of PHP, we can display a custom message (or any code, markup, or content) on a specific date:

You could even use this technique to join in on CSS Naked Day by removing your stylesheet on that day. Simply wrap your CSS <link> as follows:

The possibilities are endless!

Display Three Columns of Posts 

Displaying content in multiple columns is a much sought-after WordPress technique. There are some good tutorials around explaining various ways of doing the job, but this one is perhaps the easiest.

To display your posts in three columns, begin by segmenting your post with some HTML comments:

The next step is to create columns based on the markup comments. Open your theme file and include the following code within the loop:

That’s essentially all there is to it. To get the columns to actually look like columns, add something similar to the following in your CSS file:

The cool thing about this method is that you have full control over the layout of each particular post. Each column may contain as much or as little content as desired, and adding or removing columns is straightforward. Even better is that you can easily remove the column functionality and display your content in a single column by simply removing the custom code from the loop. The post markup consists of HTML comments, so they will be ignored if not acted upon from within the loop.

Source: Enhanced version of krimsly’s technique (via the WP Codex)

Disable WordPress Search Functionality 

Disabling the WordPress search functionality is as simple as adding the following code to your functions.php file:

In place, this code will disable the search form for your theme while leaving search functionality intact for the Admin area. As is, the $error variable is set to TRUE, which causes the function to display the theme’s error page. Setting this variable to FALSE will prevent the error message and keep the user on the same page.

Source: WPengineer

Display Posts with Specific Custom Fields 

Displaying posts that are associated with a certain custom field is as easy as adding an if condition to your loop. Here is an example that checks for the presence of a custom field called “name-of-custom-field”. If such a custom field is associated with the post, the entire post is displayed; otherwise, only the excerpt is displayed.

To get more specific and display only posts associated with a custom-field that is set to a certain value, we simply add an additional parameter to the if condition:

<?php if ((isset($custom_field[0])) && ($custom_field[0] == "name-of-value")) { ?>

Using that line of code, we modify our previous loop as follows:

With this code, any post with a custom-field of “name-of-custom-field” that has a specific value of “name-of-value” will be displayed in its entirety. All other posts will be displayed as an excerpt.

Source: AH Business Technology

How to Number Your Comments, Pingbacks, and Trackbacks in 2.7+ 

Numbering comments, pingbacks, and trackbacks in WordPress versions 2.7 and greater requires two steps. First, you need to add a couple of parameters to your comments_template tag, which is used to display your comments template and normally located in your single.php file. Edit this tag so that it looks like this:

<?php comments_template('/comments.php',true); ?>

Once this is in place, you can display the number of your comments, pingbacks, trackbacks, and both using the following tags:

These tags can be placed anywhere in your comments loop. For more information on how to implement this, check out our in-depth article at Digging into WordPress.

How to Number Your Comments Using the Classic Loop 

Before, WordPress added the new comments API in version 2.7, the “classic” comment-loop mechanism was used. This loop still works in any version of WordPress, and is useful for extreme formatting of the comment display area. Numbering your comments in the classic loop is as easy as it is in the new-fangled loop. Here’s how:

In your comments.php file, add a counter variable ( <?php $i = 0; ?> ) just above the loop’s foreach statement, like so:

Then, to increment the counter variable with each iteration of the loop, we add another snippet just below the foreach line, like so:

Everything is now set. To display the number of comments, simply echo the value of the counter variable anywhere within your comment loop. Here is an example:

Source: Digging into WordPress

Invite Readers to Comment via Feed 

Nice little snippet showing how to invite your readers to leave a comment by clicking on a link within your feed. Just add the following code to your theme’s functions.php file:

In place, this function will output a link that says, “Comments are open! Add yours!” This message will be displayed at the end of each post for which comments are open. If comments are closed, no invite message is displayed.

TinyURL for this post: https://tinyurl.com/y3vlpp3o

Sorry, The Comment Form Is Temporarily Closed At This Time
You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More