admin管理员组文章数量:1415467
I have been stuck on this for days, reading articles and articles and nothing works. I am trying to display the value of my metabox on a template page.
I successfully added the metabox, and I see the select box in my page admin, it also seems to be saving fine.
I just can't seem to get the selected category in the metabox to echo on the template page.
Everywhere I have read said to use the following which isn't working for me.
<?php
echo get_post_meta( $post->ID, 'sm_taxonomy', true );
?>
The rest of my code is below
add_filter( 'rwmb_meta_boxes', 'sm_register_meta_boxes' );
function sm_register_meta_boxes( $meta_boxes )
{
/**
* Prefix of meta keys (optional)
* Use underscore (_) at the beginning to make keys hidden
* Alt.: You also can make prefix empty to disable it
*/
// Better has an underscore as last sign
$prefix = 'sm_';
$meta_boxes = array();
$meta_boxes[] = array(
'id' => 'my-meta-box-2',
'title' => 'Portfolio Category',
'pages' => array('page', ''), // custom post type
'show_on' => array( 'key' => 'page-template', 'value' => 'grid-gallery.php' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => __( 'Choose a category', 'rwmb' ),
'id' => "{$prefix}taxonomy",
'type' => 'taxonomy',
'options' => array(
// Taxonomy name
'taxonomy' => 'portfolio_category',
// How to show taxonomy: 'checkbox_list' (default) or 'checkbox_tree', 'select_tree', select_advanced or 'select'. Optional
'type' => 'select',
// Additional arguments for get_terms() function. Optional
'args' => array()
),
),
)
);
return $meta_boxes;
foreach ($meta_boxes as $meta_box) {
$my_box = new My_meta_box($meta_box);
}
add_action('save_post', 'save');
}
and..
class My_meta_box {
protected $_meta_box;
// create meta box based on given data
function __construct($meta_box) {
$this->_meta_box = $meta_box;
add_action('admin_menu', array(&$this, 'add'));
add_action('save_post', array(&$this, 'save'));
}
/// Add meta box for multiple post types
function add() {
foreach ($this->_meta_box['pages'] as $page) {
add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
}
}
// Callback function to show fields in meta box
function show() {
global $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($this->_meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
'<br />', $field['desc'];
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
'<br />', $field['desc'];
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '<td>',
'</tr>';
}
echo '</table>';
}
// Save data from meta box
function save($post_id) {
// verify nonce
if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($this->_meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
}
I have been stuck on this for days, reading articles and articles and nothing works. I am trying to display the value of my metabox on a template page.
I successfully added the metabox, and I see the select box in my page admin, it also seems to be saving fine.
I just can't seem to get the selected category in the metabox to echo on the template page.
Everywhere I have read said to use the following which isn't working for me.
<?php
echo get_post_meta( $post->ID, 'sm_taxonomy', true );
?>
The rest of my code is below
add_filter( 'rwmb_meta_boxes', 'sm_register_meta_boxes' );
function sm_register_meta_boxes( $meta_boxes )
{
/**
* Prefix of meta keys (optional)
* Use underscore (_) at the beginning to make keys hidden
* Alt.: You also can make prefix empty to disable it
*/
// Better has an underscore as last sign
$prefix = 'sm_';
$meta_boxes = array();
$meta_boxes[] = array(
'id' => 'my-meta-box-2',
'title' => 'Portfolio Category',
'pages' => array('page', ''), // custom post type
'show_on' => array( 'key' => 'page-template', 'value' => 'grid-gallery.php' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => __( 'Choose a category', 'rwmb' ),
'id' => "{$prefix}taxonomy",
'type' => 'taxonomy',
'options' => array(
// Taxonomy name
'taxonomy' => 'portfolio_category',
// How to show taxonomy: 'checkbox_list' (default) or 'checkbox_tree', 'select_tree', select_advanced or 'select'. Optional
'type' => 'select',
// Additional arguments for get_terms() function. Optional
'args' => array()
),
),
)
);
return $meta_boxes;
foreach ($meta_boxes as $meta_box) {
$my_box = new My_meta_box($meta_box);
}
add_action('save_post', 'save');
}
and..
class My_meta_box {
protected $_meta_box;
// create meta box based on given data
function __construct($meta_box) {
$this->_meta_box = $meta_box;
add_action('admin_menu', array(&$this, 'add'));
add_action('save_post', array(&$this, 'save'));
}
/// Add meta box for multiple post types
function add() {
foreach ($this->_meta_box['pages'] as $page) {
add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
}
}
// Callback function to show fields in meta box
function show() {
global $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($this->_meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
'<br />', $field['desc'];
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
'<br />', $field['desc'];
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '<td>',
'</tr>';
}
echo '</table>';
}
// Save data from meta box
function save($post_id) {
// verify nonce
if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($this->_meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
}
Share
Improve this question
asked May 18, 2014 at 0:13
MistyMisty
1211 silver badge2 bronze badges
3
|
2 Answers
Reset to default 1Try this
- For
single
template
global $post;
echo get_post_meta( $post->ID , 'sm_taxonomy', true );
OR
- For
archive
template
if ( have_posts() ) : while ( have_posts() ) : the_post();
echo get_post_meta( get_the_ID() , 'sm_taxonomy', true );
endif;
display by ID: if 'id' => 'my-meta-box-2', then ID, 'my-meta-box-2', true ); ?> if you are using the rwmb plugin view the resource to echo the plugin correctly.
本文标签: Displaying Metabox value (custom post type taxonomy)
版权声明:本文标题:Displaying Metabox value (custom post type taxonomy) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745151731a2644952.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var_dump( get_post_meta( $post->ID ) )
returns? – тнє Sufi Commented May 18, 2014 at 6:43$post
is either not yet set (at the time that you're trying to access it) or something else overwrites its value (like a customWP_Query
running before your code and populating the global$post
object, but not resetting it back to the original page object when it finishes). – Mateusz Hajdziony Commented Sep 5, 2016 at 23:15