Techdee

5 WordPress Easy Codes To Optimize Your Blog and Improve UX

You’re probably wondering about ways to upgrade your blog or website further. When you’ve tried plugins, themes, and other easily installable features, you might think of what else is there to help optimize the site performance. 

You can use code to add features to the website and refine its performance. It can help better organize your pages, introduce authors to the audience, set up page links, and more. This article will list helpful codes and teach you how to implement them on your website. 

Set Up Your WordPress Hosting

Before anything else, I highly recommend setting up WordPress hosting for the website. The code that I will explain in this article is specially tailored to tweak and better WordPress sites. 

Not only that, WordPress has plenty of plugins that you can install to improve the website. Organizing, page designing, and installing features on the page can be done with one simple click. 

Get a plan with solid customer support, so when you counter any difficulties while implementing the code, you can get help at the soonest. Make sure it also has WordPress optimization as well to make your website function better. A prime example of this is WordPress hosting Hostinger

Code Snippets and Codex Codes

There are two types of codes that you can use to improve the website. One of them is using contributor-made code snippets or widgets. These are PHP scripts that extend the functionality of existing features in a WordPress site.

Another one is the Codex code. This code is custom-made by WordPress developers while strictly referring to WordPress Codex. It is helpful to add functions and features that might not be available in the basic setup. 

You can easily add code snippets with a plugin like Code Snippets. This option is beginner-friendly, as you can copy and paste ready-made codes without having to edit the functions.php file. 

5 WordPress Easy Codes

You have learned the two types of code and how to add them. Now, let’s move on to learn the ready-made code that you can implement to add handy features to the website. 

1. Displaying Post URL 

You can use these settings to display the URL of a post where WordPress application files are accessible. In the single.php, page.php, or index.php template, enter this line of code: 

<a href=”<?php echo get_page_link();?>”><?php echo get_page_link(); ?></a>

2. Differentiate Sticky Posts

Sticky posts are set to place important posts on the front page so they don’t get pushed down by new ones. However, having many of them can get confusing, as the WordPress dashboard doesn’t categorize sticky posts. Checking them one by one can also be time-consuming. 

There are functions you can apply to organize sticky posts. It sorts them in a box that displays the title, the post date, and a small part of the article. All you need to do is enter the following code: 

<?php if is_sticky() {

  the_title();

  the_time(‘M, d, Y’);

  the_excerpt();

}

else {

  include ‘post-template.php’;

}

3. Display Post ID Number

WordPress assigns a unique ID number for each post published on a WordPress website. While post titles and links may change, these IDs are permanent and cannot be modified. 

The post ID number is useful to distinguish between similar content under the same category. It helps both blog owners. However, permalinks do not show post ID numbers by default setup. 

You can enter this code in the location you want to display a post’s ID number: 

<?php the_ID(); ?>

4. List Categories by ID

When you write about many different topics on the blog, you can sort them through categories in WordPress. However, the readers will only find these categories under posts, and they cannot access them otherwise. 

You can display these categories on your page by entering customized code. That way, visitors will navigate your website easier and know what other topics you write about. The code is as follows: 

<?php

 $category_ids = get_all_category_ids();

 foreach($category_ids as $cat_id) {

   $cat_name = get_cat_name($cat_id);

 $category_link = get_category_link($cat_id); //we need the cat link for the URL to work!

 echo “<a href=\”{$category_link}\”>{$cat_id}</a>: {$cat_name}<br/>”;

 }

 ?>

5. Display Author’s Page on Your Blog

When you collaborate with other authors to create content and publish posts, you may want to dedicate a page to introduce each writer who has contributed to the blog

You can apply lines of code, so you don’t have to input each profile manually and redesign the page from scratch. First, duplicate your page.php file and rename the copy to differentiate. 

Then, add the template tag so that WordPress recognizes it as part of the template, and go to your WordPress Dashboard. Click Pages > Add New, then create a page for the list of authors. In the Pages list, choose Quick Edit and select the new template from the Template drop-down menu. 

After saving changes, open the duplicated page.php and enter this code: 

 <?php

 $result = count_users();

 echo ‘There are ‘, $result[‘total_users’], ‘ total users’;

 foreach($result[‘avail_roles’] as $role => $count)

 echo ‘, ‘, $count, ‘ are ‘, $role, ‘s’;

 echo ‘.’;

 ?> 

To add details about the number of posts they have written, enter the following code: 

<?php printf( __( ‘Number of posts published by user “Leaders”: %d’, ‘text-dom-here’ ), count_user_posts(1) ); ?>

If the writers are using usernames that aren’t related to their real names, here’s code to let the audience know who’s who in the authors page without having to input them manually: 

<?php $user_info = get_userdata(1);

      $username = $user_info->user_login;

      $first_name = $user_info->first_name;

      $last_name = $user_info->last_name;

      echo “$first_name $last_name logs into their WordPress site with the username of $username.”;

?>

Conclusion

There are lots of other functions that are continuously being developed for WordPress. Discovering these features may help improve user experience and give your website a competitive edge. 

Luckily, there are plenty of websites that will help you add these features with ready-made code that you can directly copy and paste to implement. Just remember to test out these codes through a sub-directory or an unused domain before applying them to your live website. 

I hope this article helps. If there are any questions, make sure to leave them in the comments section below. Read more news from the right news network.

Follow Techdee for more informative articles.