Tutorial: Wordpress, custom fields

Next to the body of the article, you can add other data under Wordpress and show it in the right place next to the text. You can actually associate this custom field with a script and create an automated view.

Custom fields are resources defined by the blog editor that he or she can use in an article.

The Administration Interface Record section defines the field.
A custom field definition can be used one or more times in the same part, and then one or more views will be created. It may not be used.

This article is based on Wordpress version 2.3.2 and below.

Custom Field Definition

It is from the letter page that a custom field is added to the post. A field definition is similar to a category definition, it can be local to an article but becomes global to a blog. At the bottom of the page is the Custom Fields section.

The field is defined by a key/value pair.
- The key is used to identify the field and
-Value contains the data displayed or used for display.

Thus, by creating a ticket or changing a ticket, you can:
- reuse the already created key or
- create a new key,
and in both cases set the value for the ticket.

The key created after its creation belongs to the database and can be used with any element.

Reuse field

From the moment you create a field when you edit an article or create a new one, the form at the bottom of the page contains a list of keywords for the custom fields you created. Therefore, we select the key and associate the value for the current product with it.

Thus, a custom field is an interface for navigating from values to a script, as shown below.

View the contents of a custom field

The field should not be automatically displayed because it depends on the selected template and the site administrator can choose where the field is displayed.

To do this, edit the main template, go through the Theme/Theme Editor menu and select the article body file (the "main index template" for the classic template), and insert the meta () function into the loop:

<?php the_meta(); ?> 

For a classic template, the source code might look like this:

<div class="storycontent">
<?php the_content(__('(more...)')); ?>
</div>

<?php the_meta(); ?> 

<div class="feedback">

This is a possible position among others.

Page code

Suppose that the key: "quote" and the value "there is no point in running."
The the_meta () function retrieves the key and value from the database and provides this information to the viewer, which integrates it into the HTML code of the page.
Check the source code of the page to find out how this integration is done, which has the following form:

<ul class='post-meta'>
<li>
    <span class='post-meta-key'>citation:</span> 
    Rien ne sert de courir
</li>
</ul>

The post-meta and post-meta-key classes have predefined descriptors in the CSS stylesheet.
Thus, you can change the style file (style.css for the classic template) to show the content as you like.

For example, a key with this handle may not be displayed:

.post-meta-key: { display:none; }

And make the text green as follows:

.post-meta: { color:green; }

But the display of custom fields is more effectively determined by the transition to programming, and the use of key data and values ​ ​ in the script.

Programming Custom Fields

Using the PHP language and internal functions allocated to fields, you can implement a personal implementation of field mapping.

This does not use the standard the_meta () function, but a PHP script written based on these dedicated functions, a list of which is given below:

  1. get_post_custom ()
    Returns a list of key/value pairs for a message.
  2. get_post_custom_keys ()
    Get the keys back.
  3. get_post_custom_values ($ key)
    Returns the value for this key.
  4. get_post_meta ($ id, $ key, $ format = false)
    An alternative function that returns a value from the ticket number and key.
    The ticket number $ ID will be received automatically with the predefined variable $ post-> ID.
    The $ key is a literal text or variable containing a string obtained by previous functions.
    A false value of $ format commands an array return, and true commands a string .

If there is a value entered from the editor, the processing defined by the PHP script is applied.
What we will see in the example below...

Example of a simple script associated with an arbitrary field

In the main file loop, the the_meta () function will be replaced by a script that displays only the value, not the key.

<?php 
   $value = get_post_meta($post->ID, "citation", true);
   echo "<ul class='post-meta' >";
   echo "<li>";
   echo $value;
   echo "</li>";
   echo "</ul>";  
?>

This demo-only script contains the default style attributes and simply shows the value set by the blog author in the editing interface.

Conclusion

This tutorial provides the necessary basics for using custom fields. How to achieve something more perfect for his blog, for example, a tag cloud centered around the topic of an article, can be found in a subsequent article.