Custom fields in WordPress help in adding information to a specific post or page. Here are some WordPress tips and tricks to use custom fields more easily.
What are Custom Fields?
The metadata used to add additional information to a page or post in WordPress are referred to as custom fields. All the data written for page or post is saved in two parts by WordPress. The body of the content which is added with the post editor is the first part and the information of the content which comprises of date, time, title etc. is the second part. This information part is known as metadata. The users can also save their custom metadata with the help of custom fields.
The option for custom field is hidden on the edit screen of the post by default and can be viewed on clicking ‘Screen Options’ button on top and then you can check the customs field option here.
As we discussed earlier, custom fields is a feature used to add relevant information to a content which can be displayed in the theme. You would have to edit the WordPress theme files to do so.
Here is How Custom Fields is Used:
To begin with edit the post or page where the custom field needs to be added and then go to meta box of custom fields. Add a name to this custom field and enter the value and to save it by clicking on the ‘Add Custom Field’. The field can be edited whenever required all you need to change or update it, the field will be changed with once you click on ‘update’. You also have the option to delete it. The post can now be saved to store the custom field settings.
Display the Custom Field in WordPress Themes
Editing the WordPress theme files will let you display the custom fields on the website. Locate the theme file that needs to be edited. The content-single.php file or single.php file has to be edited. The custom fields code needs to be entered in the WordPress loop. Look for the below lines:
The code should be added before this line
Replace the key with the actual name of the custom field and save the changes. Go to the page where you added the custom field to see the changes. The other posts in your WordPress website can have custom fields in the above way. You just need to navigate to the custom fields meta box and then select the custom field from the drop down and enter the value. ‘Add Custom Field’ will save the changes you have made and the post can be published or updated.
User Interface for Custom Fields
Next in the WordPress tips and tricks for customs fields is to create a user interface for it. Once the custom field is added the field needs to be selected and its value entered every time a post is written. In case there are several custom fields or multiple users who are writing on the website there could be confusion. This is where user interface comes in handy.
A plugin will make your job easy – Advanced Custom Fields is one plugin you can browse for others. You need to install and activate the plugin. Once it is activated it adds a new menu it adds a new menu item which says ‘Custom Fields’ to the WordPress Admin Bar. You will be directed to the custom fields page where you need to click on ‘Add New’ to proceed to the next step. A ‘Add New Field Group’ page will be displayed where you will have to add a title for your field group. This will be the title of the meta box. You need to fill in all the fields just click on ‘+Add Field’. You will see the field settings form for which a label has to be provided. Select the field type there is a variety of option you would get in this plugin. You will have to punch in the field instructions, the options need to be chosen according to your requirement. Once done click on the close field to collapse the field, you can click on ‘+Add Field’ to add the next field.
After this, you would have to scroll down the location and mention where the meta box would be displayed. The meta box options settings is the next thing and then comes the meta box position on page. You have the option to show it on the right column, before or after the content. You will see a list of fields displayed on the post edit screen which you can hide or leave them unchecked if not sure about it. You can click on publish to make the field go live.
Use of Specific Custom Key to Display Posts
If you are trying to create a custom archive page to display all posts with specific custom keys then WP_Query class can be used to query posts matching the fields. The below code can be used:
Make sure you change the meta_key and the meta_value with parameters of your own.
Display Custom Fields Outside the Loop
The next WordPress tips and tricks for customs fields is to display it outside the loop like the sidebar of a single post.
Use the code below and the custom fields will be displayed outside the WordPress loop.
Replace the ’lock’ with your custom field name.
Manipulate RSS Feed Content with Custom Fields
You can display additional content or metadata to RSS feed users with the help of custom fields by manipulating the WordPress RSS feed and adding your custom content to the feed. Add the code below in the theme’s function.php file or the site-specific plugin.
A custom field with the name of ‘mycustom’ can be created and values of your choice can be added. You can use any other name besides ‘my custom’. Use this to display images, text or whatever you want.
Hide Empty Custom Fields Using Conditional Statement
Another easy WordPress tips and tricks for customs fields is to check whether or not the custom field is empty before it can be displayed. The code will first have to be modified to check whether there is data in the field.
Replace ‘Climate’ with your custom field name.
Displaying Custom Sidebar, Header and Footer with Custom Fields
The sidebar, header, and footer can be displayed in different ways for each post or page in WordPress. Although there are different ways to do it one easy way is through custom fields. The post or page where the display needs to be changed need to be edited and then the sidebar should be added as a custom field. The WordPress theme files such as single.php where the custom sidebar has to be displayed should be edited. Look for the code below:
1 <?phpget_sidebar(); ?>
Replace the above line with the code below:
The code will look up the sidebar custom field and display it on the theme.
Manipulating RSS Feed Title with Custom Fields
At times there may be a situation where you would have to add an extra text to a post title for RSS feed users, this mostly happens when you would have to publish a guest or sponsored post.
Start with adding the below code to your theme’s functions.php file or site specific plugin:
Once you have added the above code you would have to edit the post where the extra text had to be displayed in the title field and in the custom fields add guest_post and sponsored_post. If either of the two custom fields is found with a value ‘true’ then the text will be added before the title.
Setting Expiry Date for Posts Using Custom Fields
Next, in the list of WordPress tips and tricks for customs fields is setting an expiry date for posts on your WordPress website. Using the custom fields the posts will expire automatically after the time you have specified. The theme files need to be edited and the WordPress loop needs to be modified as below:
The code will have to be edited according to the theme. Once the code is added the expiration custom field can be added to the post which you want to expire. Ensure that the date and time are added in this format only – mm/dd/yyy 00:00:00.
Add Multiple values to a Custom Field
Multiple values can be added to the existing custom fields in the same post. All you need to do is select it once again and add another value. If you wish to display all values of a custom field then the code needs to be modified so the data is returned in array. The below code should be added to the theme file:
Replace ‘Climate’ with your custom field name. The last parameter of get_post_metafunctionin the above code has been changed to false. The parameter states if the function has to return a single value or not. If it is set to false then the data will be returned as an array.
The above WordPress tips and tricks of custom fields enable you to exploit the feature to its fullest.