Introducing custom fields in wordpress basically means to add other data in WordPress and display them at the desired location beside the text. In fact this custom field can be associated with a script and so allow to develop an automated display. The definition of the field is similar to that of a category, it can be done locally in an article but becomes global for the blog. From the moment of the creation of a field, when an article is created or modified, a form at the bottom of the page shows a list of keywords created for custom fields. The custom field form can be considered an interface for passing values to a script.
It is not expected that the field appears automatically, since it depends on the chosen template, and also because the site administrator may want to select the location where to display this field. To do this we must edit the main template, through the menu Theme/Theme editor and select the file for the body of the article, and insert in the_meta() function.
For the classic template, the source code might look like this:
1 2 3 4 5 |
<div class="storycontent"> <?php the_content(__('(more...)')); ?> </div> <?php the_meta(); ?> <div class="feedback"> |
The the_meta() function retrieves the key and value from the database, and provides this information to the display engine that integrates them into the page in HTML. Watch the source code of the page to see how this integration is made. it has the following form:
1 2 3 4 5 |
<ul class='post-meta'> <li> <span class='post-meta-key'>quotation:</ span> Hello World!</ li> </ul> |
Finally, Using PHP and some internal functions dedicated to custom fields, it is possible to achieve a personal implementation. Consider the following example of a simple script associated with a custom field:
1 2 3 4 5 6 7 8 |
<?php $value = get_post_meta($post->ID, "quotation”, true); echo "<ul class='post-meta' >"; echo "<li>"; echo $value; echo "</li>"; echo "</ul>"; ?> |
In the future articles, we would discuss some more custom programming tips for WordPress utilizng CSS, Php and HTML.