8 WordPress code snippets you can use in your themes

If you’re running a WordPress site, here are some handy functions to use in your theme that can help with both tasks and your user experience.

Of course, you’ll want to work with these on a testing environment first instead of doing so on your live site. In either case, make sure to back everything up first!

1. Display different content for logged in users

If you have subscribers on your site and wish to display specific content on a page for them whether or not they’re logged in, add the following to your desired theme template:

  if ( is_user_logged_in() ) {
    echo 'Content for your logged in users here...';
  } else {
    echo 'Content for your logged out users and everyone else here...';

Some uses for this includes:

2. Increase the files size limit for uploads

If you’re maxing out when trying to upload files to your WordPress media gallery or directly on a page, you may need to increase your upload size limit.

Add this to your functions.php theme file:

@ini_set( 'upload_max_size' , '1024M' );
@ini_set( 'post_max_size', '1024M');
@ini_set( 'max_execution_time', '1000' );

Alternatively, you can ad the following to your theme’s wp-config.php file:

@ini_set( 'upload_max_size' , '1024M' );
@ini_set( 'post_max_size', '1024M');
@ini_set( 'memory_limit', '1000M' );

Both of the max values may be a little higher than you need. This is just how I have mine set up. If neither works, contact your hosting provider.

3. Truncate post excerpt text

If you need to limit the text displayed in the excerpts of your post snippets on archive and search results pages, add the following to your functions.php file:

function custom_excerpt_length( $length ) {
  return 45;
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Change ’45’ to the number that fits your needs and it will automatically truncate the text to that value.

4. Display a list of all post categories

If you need to list all categories used in your posts, add the following to your desired theme template(s):

  <?php wp_list_categories( array(
    'title_li' => ''
  ) ); ?>

When rendered on pages, it will display as an unordered list of links to your archive of posts with that category.

5. Display a different title on your home page

If you’re not using a unique template for your home page and are instead using index.php, you can display a different header on your home page by adding the following to that theme file:

  if( is_home() ):
    echo '<h1>Your home page title here</h1>';
    echo '<h1>Articles</h1>';

This requires that you’re front page is set to display your recent posts under Settings > Reading in the WordPress dashboard:

wordpress settings homepage screenshot

6. Only display posts in search results

If you run a blog that has only a couple of supporting, static pages of content, you may wish to only serve results for posts on your search results pages. One way to do this is to add the following to your functions.php file:

add_filter( 'pre_get_posts', 'page_hate_search_filter' );
function page_hate_search_filter($query) {
     if ($query->is_search) {
          $query->set( 'post_type', 'post' );
     return $query;

7. Add author bio in posts

This solution is ideal for sites with multiple authors. It allows you to display their user information in the bio at the end of their posts by pulling in the information from their WordPress user account.

You can use the following in your single.php template or template part:

  <!-- displays the author's avatar, change 32 to your desired size -->
  <?php echo get_avatar( get_the_author_meta( 'ID' ), 32 ); ?>

  <!-- displays the author name -->
  <h2><?php the_author_link(); ?></h2>

  <!-- displays the author's bio -->
  <p><?php the_author_meta('description'); ?></p>

  <!-- links to their post archive -->
  <p><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>">Full bio</a></p>

The link will then load the archive of their posts using your author.php template.

8. Include author name in page title of author archives

When you have multiple authors on your site and are displaying the archive of posts by a single author, you can display their name on the page so users know whose articles they’re combing through.

This requires adding a bit of code just above the h1 on your author.php template file:

  // fetches the author name
  $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : 								

// displays the author name
<h1>Articles by <span><?php echo $curauth->nickname; ?></span></h1>

// begin loop

The reason to use this solution instead of get_the_author like you would in your single.php template is because here we want to display the author’s name outside the loop.

In this example, I’m using the author’s nickname, as set in their WordPress user settings.

You may also like…

Have fun!

Archived under:
This site uses cookies to collect basic info to help improve your experience. Read my Privacy Policy Accept