Doku » Benutzer:Peti/Benutzerdefinierte Felder
Inhaltsverzeichnis |
Mit WordPress können Authoren einem Beitrag benutzerdefinierte Felder hinzufügen. Diese beliebige Information bezeichnet man als meta Daten. Diese meta Daten können eine Fülle an Informationen enthalten, sowie:
- Laune: Fröhlich
- Ich lese zurzeit: Das Parfüm
- Es läuft: Cesaria Evora - Carnaval de Sao Vicente
- Berliner Wetter: Mild, Bewölkt mit schwachem Wind
Mit ein bisschen mehr Aufwand kann man sogar ein Ablaufdatum für einen Beitrag als meta Daten festlegen.
Die Meta Daten werden mit Schlüssel/Wert Paaren verwaltet. Der Schlüssel ist der Name des Meta Daten Elements, und der Wert ist die Information die in der Meta Daten Liste in jedem einzelnen Beitrag erscheint.
Schlüssel können pro Beitrag mehrmals benutzt werden. Als Beispiel, du liest zwei verschiedene Bücher (ein technisch thematisiertes Buch in der Schule oder auf Arbeit und einen Roman zu Hause), du könntest also einen "Ich lese gerade" Schlüssel erstellen und ihn im Beitrag zwei mal verwenden.
Folglich ein Beispiel wie das Ganze in deinem Beitrag aussehen könnte:
Heutige Laune: Jolly and Happy
[bearbeiten] Verwendung
Basierend auf unserem vorherigen Beispiel, setzen wir das nun in die Tat um. Wir werden 2 benutzerdefinierte Felder hinzufügen. Einen nennen wir "Ich lese zurzeit" und den anderen "Heutige Laune".
- From the Write Post panel, choose Advanced Editing. If you are using the Simple Editing screen, look for a button with Advanced Editing » next to the Publish button. Click the button to go to the advanced editing screen.
- After you have written your post, scroll down to the bottom of the Advanced Editing screen and look for an area titled Custom Fields.
- To create a new Custom Field called "Currently Reading", enter the text "Currently Reading" (without the quotes) in the text entry field titled Key.
- The newly created Key should now be assigned a Value, which in our case is the name of the book currently being read, "Calvin and Hobbes". Type "Calvin and Hobbes" in the Value field, again without the quotes.
- Click Add Custom Field button to save this custom information for that post.
To add your "Today's Mood", repeat the process and add "Today's Mood" to the key and a description of your mood in the value text boxes and click SAVE to save this information with the post.
On your next post, you can add a new book and mood to your meta-data. In the Custom Fields section, the Key will now feature a pull down list with the previously entered Custom Fields. Choose "Currently Reading" and then enter the new book you are reading in the value. Click Add Custom Field and then repeat the process to add "Today's Mood".
You only need to create a new "KEY" once, after which you can assign a value to that key for every post, if you so desire. You can also assign more than one Value to a key, for a post. This will come in handy for people who read more than one book at a time.
[bearbeiten] Displaying Custom Fields
With a Custom Field added to the post, it's time to display your books and mood to the world. To display the Custom Fields for each post, use the the_meta() template tag. The tag must be put within The Loop in order to work. Many people add the_meta() template tag to the end of their post or in their Post Meta Data Section. Here is a basic example of using the tag:
<?php the_meta(); ?>
It might look like this in the source code:
<ul class='post-meta'> <li><span class='post-meta-key'>Curently Reading:</span> Calvin and Hobbes</li> <li><span class='post-meta-key'>Today's Mood:</span> Jolly and Happy</li> </ul>
The template tag automatically puts the entire meta-data into a CSS style called post-meta. The key is in a span called post-meta-key so you can style it in your style sheet. All of this is showcased in an unordered list.
To customize the look of the post-meta list, change the characteristics in your style sheet. For instance, let's add some style to our example from the top. The style sheet elements would look like this:
.post-meta {font-variant: small-caps; color: maroon; }
.post-meta-key {color: green; font-weight: bold; font-size: 110%; }
- Currently Reading: Calvin and Hobbes
- Today's Mood: Jolly and Happy
There are also a few plugins that add some nice features to the job of displaying meta tags. A search for Custom Field plugins at Google should help you find even more.
[bearbeiten] Advanced Techniques for Custom Fields
The following are more advanced techniques for getting and customizing meta-data and custom fields.
[bearbeiten] Getting Custom Fields
To fetch meta values use the get_post_meta() function:
get_post_meta($post_id, $key, $single);
- $post_id is the ID of the post you want the meta values for. Use $post->ID to get a post's ID.
- $key is a string containing the name of the meta value you want.
- $single can either be true or false. If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields.
[bearbeiten] Implementation Details
The PostMeta information is stored in a new table, $wpdb->postmeta. This table has four fields:
- meta_id: A unique id for each entry
- post_id: The ID of the post for this metadata
- meta_key: The name of the 'key'
- meta_value: The value associated with the key
The values from this table are pulled into a structured multi-dimensional array called $post_meta_cache, just after the $posts array is fetched in wp-blog-header.php. This variable will only contain values for the list of posts fetched for the current page build. The structure of the array will look something like this:
[
postid1 => [
[
key1 => [val1, val2, ...],
key2 => [val1, val2, ...],
...
],
postid2 => [ ... ],
...
]
So, if you wanted to fetch the "reading" values from post number 256, you use this PHP code:
// Fetch an array of values for what I'm reading: $readinglist = $post_meta_cache[256]['reading'];
- Don't forget that $readinglist will be an array, not a single value.
[bearbeiten] PostMeta Functions
[bearbeiten] Internal Functions
These functions are intended for use inside the wp-loop, and all return arrays.
- get_post_custom()
- Get all key/value data for the current post.
- get_post_custom_keys()
- Get a list of all key names for the current post.
- get_post_custom_values($key)
- Get the list of values for a particular key on the current post.
- get_post_meta($post_id, $key, $single = false)
- In WP 1.5 and beyond, this function returns the meta information without cache problems. The function requires the post id, the key, and if $single is set to TRUE, it returns only the first result (NOT as an array) for PHP use.
[bearbeiten] Template Functions
At the time of this writing, there is only one template function.
- the_meta()
- Echoes an unordered list containing the current post's meta-data with a class for the UL as post-meta and the LI as post-meta-key.
We expect that independent developers will come up with many interesting uses for post meta-data in the form of plugins. The the_meta() template function is just an extremely basic example.
At this time, you can only add and delete entries. The ability to modify existing entries will be implemented later.