WordPress Custom Fields 101: Tips, Tricks And Hacks

WordPress Custom Fields 101: Tips, Tricks and Hacks

by Vyshnavi — 4 years ago in Review 5 min. read
3027

Do you want to learn more about the custom fields of WordPress? Are you looking for valuable tips, tricks, and hack when using this feature? If yes, this post is for you. 

A lot of people find custom fields as a cool feature of WordPress. Why not? It lets you add more information not only to your WordPress posts but your pages as well. Besides, you can use this feature for storing important information and use it on your site later on.

WordPress Custom Fields Overview

WordPress custom fields refer to the metadata used for adding additional post- or page-related information to what you are editing.

With WP custom fields, you can repeatedly add the same data or information to multiple posts, even in content-heavy sites.

Using this feature makes it possible for you to create an area in the editor to add a particular type of data or information to every post or page.

For example, you can incorporate a disclaimer for an affiliate content or a rating for product reviews.

This WordPress feature, as a default, is hidden. You can view it by clicking on the three-dot menu that is located at the screen’s top right corner.

After that, click “Options” from the menu and check the “Custom fields” option under the Advanced Panels. Next is to click the “Enable & Reload” button. The post editor reloads, and you will see the custom fields panel.

Now, let’s get into the tips, tricks, and hacks in using WordPress custom fields.

Add Custom Fields in WordPress

Edit the post or page that you wish to add the custom field before you proceed to the custom meta box. Make sure to make a name for the custom field. After that, enter the value and save it by clicking the “Add Custom Field.” 

Find Custom Field in Dropdown on Post Edit Screen

If you cannot see the newly created custom field, then you can fix this issue by adding the following code to the site-specific plugin or functions.php of your theme:

WordPress Custom Fields 101: Tips, Tricks and Hacks 1

Add Custom Fields with the Use of Advanced Custom Fields

First, install and activate the Advanced custom field plugin. Once activated, visit the Custom Fields »Field Groups page, then click on the button, “Add New”. Make sure to provide a title for the field group before clicking on the “Add Field” button.

Next is to provide a name for the custom field. Then, select a field type. As you scroll down, other options will appear. Feel free to change them ad add multiple fields to the field group. To save the changes, click on the “Publish button.

Display Content Only

Mind some specific custom-field keys that do not exist in all posts. You can use the following code to prevent incomplete, empty, or unwanted markup from destroying your page validity:

WordPress Custom Fields 101: Tips, Tricks and Hacks 2

Display A List of Key Values

You can use the following basic code template to display a list of existing key values:

WordPress Custom Fields 101: Tips, Tricks and Hacks 3

Check If the Custom Field Is Not Empty

Before displaying the custom field, it’s essential to check if it is not empty. You can do it by modifying the following code to see if data exists in the field:

WordPress Custom Fields 101: Tips, Tricks and Hacks 4

Make sure to replace the “Mood” with your desired field name.

Add Guest Author Name with the Use of Custom Fields

First, add the following code in the site-specific plugin or the functions.php file of your theme:

WordPress Custom Fields 101: Tips, Tricks and Hacks 5

If an author’s name exists, then you need to replace it with the guest author’s name. Next is to edit the post you wish to display it. Lastly, go to the custom fields meta box to add the guest author name.

Display Custom Fields Outside the Look

In order for you to display the custom fields outside the loop, the following code will help you:

WordPress Custom Fields 101: Tips, Tricks and Hacks 6

Make sure to replace the key with your chosen field name.

Display Contributors to the Article

Edit the post you wish to display the contributors or co-contributors. Next is to scroll down to the custom fields meta box. After that, add the author names as co-author custom fields. 

Add the following code to the theme files you want to show the co-authors:

WordPress Custom Fields 101: Tips, Tricks and Hacks 7

Add the following custom CSS in order for you to display the author names that are separated by commas:

WordPress Custom Fields 101: Tips, Tricks and Hacks 8

Determine the Type of Content Appears On A Page

You can use custom field values in determining which content type appears on a page. For example, let’s check the custom field key-value called “hobbies.” You can use the following codes:

WordPress Custom Fields 101: Tips, Tricks and Hacks 9

Display Custom Header, Footer, and Sidebar

Edit the post or page you wish to show a different sidebar. Next is to add the sidebar as a custom field. After that, edit the files like single.php. Look for the following code:

WordPress Custom Fields 101: Tips, Tricks and Hacks 10

Make sure to replace it with the following code:

WordPress Custom Fields 101: Tips, Tricks and Hacks 11

In your theme folder, create a sidebar-wpbpage.php file. As a starting point, feel free to copy the code from the sidebar.php file of your theme.

Manipulate RRS Feed Title

First, add the following code in the site-specific plugin or functions.php file of your theme:

WordPress Custom Fields 101: Tips, Tricks and Hacks 12

After that, edit the post you wish to display the extra text. Next is to add the guest_post and sponsored_post in custom fields.

Manipulate RRS Feed Content

First, add the following code in the site-specific plugin or functions.php file of your theme:

WordPress Custom Fields 101: Tips, Tricks and Hacks 13

Next is to create a custom field known as “cool custom.” After that, add any value and use it to display text, images, advertisements, and anything you want.

Set Expiration Date for Posts 

First, edit the theme files. After that, add and modify the loop like this:

WordPress Custom Fields 101: Tips, Tricks and Hacks 14

After that, add the expiration custom field to the post. Add the time in mm/dd/yyyy 00:00:00 format. 

Conclusion

In a nutshell, WordPress custom fields allow you to customize your posts or pages by adding additional information to them.

By assigning different types of content to multiple custom fields, you can completely control when, where, and how to display important information.

Vyshnavi

Vyshnavi is a content marketing specialist having 3+ years of experience in content writing, SEO, and social media optimization. She has been offering her services independently. Vyshnavi enjoys writing and knows the basics of digital marketing & web development too.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Copyright © 2018 – The Next Tech. All Rights Reserved.