开发者 | coffee2code |
---|---|
更新时间 | 2021年11月6日 05:33 |
捐献地址: | 去捐款 |
PHP版本: | 3.6 及以上 |
WordPress版本: | 5.8 |
版权: | GPLv2 or later |
版权网址: | 版权信息 |
[custom_field]
shortcode (which has a post editor sidebar widget to help you build the shortcode), or one of six template tags to retrieve and display custom fields. Find a custom field for the current post, a specified post, a recent post, or randomly. And for the custom field values found by the plugin, you may optionally specify text or HTML to appear before and after the results. If nothing is found, then nothing is display (unless you define text to appear when no results are found). If multiple results are found, only the first will be displayed unless you specify a string to be used to join the results together (such as ","), in which case all will be returned. Visit the Examples section to see how this plugin can be cast in dozens of different ways.
There are six template tags provided by this plugin. Here they are, with an explanation of when they are appropriate for use:
c2c_get_custom()
: Use this inside "the loop" to retrieve a custom field value for a postc2c_get_current_custom()
: This is only available on the permalink post template (single.php) and page template (page.php). Can be used inside or outside "the loop". Useful for using custom field to define text you want to include on a post or page's header, footer, or sidebar.c2c_get_post_custom()
: Useful when you know the ID of the post whose custom field value you want.c2c_get_random_custom()
: Retrieve the value of a random instance of the specified custom field key, as long as the field is associated with a published posted, non-passworded post (you can modify a setting in the plugin file to search passworded posts as well).c2c_get_random_post_custom()
: Retrieves the value of random custom field(s) from a post when you know the ID of the post you're interested in.c2c_get_recent_custom()
: Retrieves the most recent (according to the associated post's publish date) value of the specified custom field.the_meta
' to filter custom field data (see the end of the code file for commented out samples you may wish to include). You can also add per-meta filters by hooking 'the_meta_$sanitized_field
'. $sanitized_field
is a clean version of the value of $field
where everything but alphanumeric and underscore characters have been removed. So to filter the value of the "Related Posts" custom field, you would need to add a filter for 'the_meta_RelatedPosts
'.
Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage
/wp-content/plugins/
).Did you echo
the return value of the function, e.g. <?php echo c2c_get_custom('mood', 'My mood: '); ?>
Except for c2c_get_custom()
(which is only available inside "the loop"), yes, by carefully setting the appropriate settings for the widget.
c2c_get_custom()
: not achievable via the widgetc2c_get_current_custom()
: set the "Post ID" field to 0
, leave "Pick random value?" unchecked, and set other values as desired.c2c_get_post_custom()
: set the "Post ID" field to the ID of the post you want to reference and set other values as desired.c2c_get_random_custom()
: leave "Post ID" blank, check "Pick random value?", and set other values as desired.c2c_get_random_post_custom()
: set the "Post ID" field to the ID of the post you want to reference, check "Pick random value?", and set other values as desired.c2c_get_recent_custom()
: leave "Post ID" blank, leave "Pick random value?" unchecked, and set other values as desired.If you use the block editor (aka Gutenberg, which is the default editing experience as of WordPress 5.0), then the shortcode builder is not available yet so this situation would be moot for you.
For the classic editor, when on the write or edit admin pages for a page or post, find the "Screen Options" link near the upper right-hand corner of the page. Clicking it slides down a panel of options. In the "Show on screen" section, uncheck the checkbox labeled "Get Custom Field Values - Shortcode". This must be done separately for posts and for pages if you want the shortcode builder disabled for both sections.
Programmatically, see the developer documentation for the get_custom_field_values/show_metabox
filter for how to completely (or selectively) disable the shortcode builder.
If you use the block editor (aka Gutenberg, which is the default editing experience as of WordPress 5.0), then the shortcode builder is not available yet. If you aren't able to include scripts in your posts (i.e. you don't have the 'unfiltered_html' capability), then the shortcode builder is not available to you. Only those with the editor or administrator role (except on Multisite) or the super administrator role can make use of this plugin's shortcode. For the classic editor, the shortcode builder/wizard is available in the admin when writing or editing a page or post. On the edit/create page, it'll be a sidebar widget (in this context, also known as a metabox) labeled "Get Custom Field Values - Shortcode". If you don't see it there (which may be the case since it is hidden by default), find the "Screen Options" link near the upper righthand corner of the page. Clicking it slides down a panel of options. In the "Show on screen" section, check the checkbox labeled "Get Custom Field Values - Shortcode". This must be done separately for posts and for pages if you want the shortcode builder enabled for both sections.
Yes, any of the boxes on the page when creating/editing posts can be rearranged by dragging and dropping the box name. At the very top of the shortcode builder box the cursor will turn into a four-way array indicating you can click to drag that box. You can move it under the post content box, or higher up on the right side.
Sometimes you have to ensure the text editor has focus. Click within the text editor and make sure the cursor is positioned at the location you want the shortcode to be inserted. Then click the button and the shortcode should get inserted there.
Yes, except that the shortcode builder (a custom tool to facilitate making use of the plugin's shortcode when creating a post) has not been ported over yet. The template tags, widget, and shortcode itself all function properly.
Yes.
can_author_use_shortcodes()
'get_custom_field_values/can_author_use_shortcodes'
'get_custom_field_values/show_metabox'
to customize if shortcode builder metabox is showncreate_user()
for creating a user and optionally making them the current userunset_current_user()
for unsetting the current usertearDown()
to ensure current user gets unset after each testshow_metabox()
$make_global
(defaulted to false) to create_post_with_meta()
to facilitate making the created post globalphpunit/bin/
to tests/bin/
phpunit/bootstrap.php
into tests/phpunit/
phpunit/tests/
to tests/phpunit/tests/
phpunit/
to house all files related to unit testingbin/
to phpunit/bin/
tests/bootstrap.php
to phpunit/
tests/
to phpunit/tests/
phpunit.xml
to phpunit.xml.dist
per best practicesregister()
c2c_get_custom_field_values_post_types
filter as an array