custom gields in wordpress

How to add custom fields to posts without using a plugin

What are custom fields?

Custom fields are inputs that, once created, can display new sections in posts. Whether it’s text, image, video fields. They can be displayed on the posting page in two ways: either we assign them a hook from the article editing page and they will be displayed automatically, or we use the get_post_meta () function to insert them into the single.php file.

Why do I need custom fields?

Sometimes you will need to add certain fields or sections with various information about the content of your posts. For example, if you write a blog about movies, you will need to use details such as: the year of release, the names of the actors who play in the movie, the name of the producer, etc. By default, WordPress offers this possibility for free and easy to use.

How to create a custom field!

To start creating custom fields, you’ll need to open the page to create or edit a post. At the top right of the page you will need to click on the 3 vertically aligned dots to open the options menu, as shown in the image below.

From this menu we will select the last option below, respectively “preferences”. It will automatically open a pop-up that looks like this:

Next we will select “panels” from the menu on the left, then we will activate the “custom fields” option. After doing this we will have to refresh the page so that we can now see the section for creating / editing custom fields. The section will look like this:

On the left we will have to choose one of the following options:

  1. If we want to assign these fields to WordPress hooks, then we can select from that drop-down.
  2. If we want to create a new name, then we will click on the option “insert a new one”. This will replace the selection input above with a text entry in which we can enter the name.

On the right side of the section we will have to write a value.

How do I display custom fields on the article page?

In order to display the created fields, we will need to open the theme’s single.php page for editing. Here we will enter the following code inside the while loop as shown below.

<?php while ( have_posts() ) : the_post(); ?>

        <?php echo get_post_meta($post->ID, 'nume', true); ?>

<?php endwhile;?>

You will need to replace the word “name” in the code with the name you gave to that field.

One of the disadvantages of using custom fields in WordPress is that you can only add up to 30 such fields by default. In order to add more, we will need to enter the following code in the functions.php file.

add_filter( 'postmeta_form_limit', 'nume_functie_pt_limita_campurilor_personalizate' );
function nume_functie_pt_limita_campurilor_personalizate( $limit ) {
    return 200;
}

In this code we will call a hook (postmeta_form_limit) to which we will assign the value of a function created by us later, respectively name_function_pt_limita_campurilor_personalizados (). In the example above we set the limit to 200, but you can change it as needed.

Share: