Understanding Meta Box (wordpress plugin). I'm trying to create meta boxes for specific pages/posts. I have created 2 meta boxes with a simple text field in them. I want one meta box to be for page/post id # 4 and another to be for page/post id # 22 I have the following code producing the meta boxes in the admin, however i can only get them to show on page/post id # 4. I want one to only show for page/post id #4 and another to only show on page/post id #22
Little help would be greatly appreciated. here is my code so far which i have placed in the functions.php file.
add_filter( 'rwmb_meta_boxes', 'KNMS_register_meta_boxes' );
/**
* Register meta boxes
* @param $meta_boxes
* @return array
*/
function KNMS_register_meta_boxes( $meta_boxes )
{
// Check before register meta boxes
if ( ! rw_maybe_include() )
{
return $meta_boxes;
}
// Register meta boxes
// @see http://ift.tt/1RM9MOE
$prefix = 'rw_';
$meta_boxes[0] = array(
'id' => 'any_id1',
'title' => __( 'Meta Box For POST ID 4', 'knms' ),
'post_types' => 'page',
'fields' => array(
array(
'name' => __( 'My Field For POST ID 4', 'knms' ),
'id' => "{$prefix}title1",
'type' => 'text',
),
),
);
$meta_boxes[1] = array(
'id' => 'any_id2',
'title' => __( 'META BOX FOR POST ID 22', 'knms' ),
'post_types' => 'page',
'fields' => array(
array(
'name' => __( 'My Field For POST ID 22', 'knms' ),
'id' => "{$prefix}title2",
'type' => 'text',
),
),
);
return $meta_boxes;
}
/**
* Check if meta boxes is included
*
* @return bool
*/
function rw_maybe_include()
{
// Always include in the frontend to make helper function work
if ( ! is_admin() )
return true;
// Always include for ajax
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return true;
// Check for post IDs
$checked_post_IDs = array( 4 );
if ( isset( $_GET['post'] ) )
$post_id = intval( $_GET['post'] );
elseif ( isset( $_POST['post_ID'] ) )
$post_id = intval( $_POST['post_ID'] );
else
$post_id = false;
$post_id = (int) $post_id;
if ( in_array( $post_id, $checked_post_IDs ) )
return true;
// Check for page template
$checked_templates = array( 'full-width.php', 'sidebar-page.php' );
$template = get_post_meta( $post_id, '_wp_page_template', true );
if ( in_array( $template, $checked_templates ) )
return true;
// If no condition matched
return false;
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire