||The relation field stores a reference to another post of some kind: post, page, or a media attachment like an image or video.|
|:--------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------|
Field Definition (Meta Data)
The field definition affects how instances of this field type will display in the WordPress manager while creating or posts that contain instances of this field type.
- label : the label identifies this field when you create or edit a post in the WordPress manager
- name : the name is a unique identifier used when saving values for instances of this field; it corresponds to the meta_key column wp_postmeta table. You will use this name when retrieving field values inside of your themes via the
print_custom_field()
orget_custom_field()
functions. - description : the description provides extra information to users as they create or edit a post in the WordPress manager.
- class : this affects the CSS class in the WordPress manager that is used to display instances of this field type.
- extra : this can be used to provide additional parameters to the text input, e.g.
size="10"
or custom javascript.. - default_value : This is used to determine the default value for new instances of this field type.
- output_filter : Output Filters control how data is filtered before being sent to your theme files. A recommended default output filter for an image field would be: to_image_src
Included Javascript/CSS files
media-upload
(built-in WordPress script)thickbox
(built-in WordPress script);/js/relation.js
Example of Use in Template File
Example 1: Retrieve a Link to the Related Post
Here we show how to use the to_image_src Output Filter:
<a href="<?php print_custom_field('my_relation: to_link_href'); ?>">Click me</a>
| | In the case of a relation field with no output filter,
print_custom_field('fieldname');
will return an integer number representing the ID of the related post. |
|:--------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
Example 2: Retrieving Multiple Related Items
Since image fields support the "repeatable" option, your field might store an array of images. Any time you use the "repeatable" option, you'll want to use the to_array Output Filter.
```
foreach ($array_of_posts as $post_id) { print CCTM::filter($post_id, 'to_link_href'); } ```
Note here that we are using a foreach loop to iterate over the array, and we're using the CCTM::filter() function to convert each value in the array into the source of the media items via the to_image_src Output Filter
Recommended Output Filters
- to_array (if you have "repeatable" checked).
- to_link_href for linking to the related item.
- to_link for generating a full link tag
- get_post for getting the entire related post.
Customizing Manager HTML
See CustomizingManagerHTML for more information on customizing the appearance of the manager HTML.
Field tplThe first of following tpls found will be used to format the field:
fields/elements/{fieldname}.tpl
fields/elements/_relation.tpl
fields/elements/_default.tpl
The first of the following tpls found will be used to wrap the output:
fields/wrappers/{fieldname}.tpl
fields/wrappers/_relation.tpl
fields/wrappers/_default.tpl
Multi
Relation fields can be repeatable. If the "repeatable" option is checked, the following tpls are used to format the output:
Field tplThe first of following tpls found will be used to format the field:
fields/elements/{fieldname}.tpl
fields/elements/_relation_multi.tpl
The first of the following tpls found will be used to wrap the output:
fields/wrappers/{fieldname}.tpl
fields/wrappers/_relation_multi.tpl