I am trying to add a custom meta box to page-admin that shows all my post-categories as options. I already made the meta box appear but i can not figure out how to show the categories. The dropdown is empty.
Here is my code:
function cd_meta_box_page()
{
global $page;
$values = get_post_custom( $page->ID );
$selected = isset( $values['my_meta_box_page'] ) ? esc_attr( $values['my_meta_box_page'] [0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
Wählen Sie hier eine Kategore aus welcher die Beiträge in die Seite geladen werden sollen.
</p>
<p>
<label for="my_meta_box_page">Kategorien</label>
<select name="my_meta_box_page" id="my_meta_box_page">
<?php
$args = array(
'orderby' => 'id',
'hide_empty'=> 0,
'child_of' => 5, //Child From Boxes Category
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo '<option value="'.$cat->name; selected( $selected, $cat->name ); echo '">'.$cat->name; echo'</option>';
$args2= array("orderby"=>'name', "category" => $cat->cat_ID); // Get Post from each Sub-Category
$posts_in_category = get_posts($args2);
}
?>
</select>
</p>
<?php
}
I can not find my fault. Can anyone help, please?
Thank you!
EDIT:
I found the issue. Deleting the following line helped:
'child_of' => 5, //Child From Boxes Category
Now I have the problem that wordpress does not store my selection. Saving the value of my_meta_box_postvariante
works fine. But the value for my_meta_box_page
is always staying default. I can not make it work… My code:
function cd_meta_box_page()
{
// $post is already set, and contains an object: the WordPress post
global $post;
$values = get_post_custom( $post->ID );
$selected = isset( $values['my_meta_box_page'] ) ? esc_attr( $values['my_meta_box_page'] [0] ) : '';
// We'll use this nonce field later on when saving.
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
Wählen Sie hier eine Kategore aus welcher die Beiträge in die Seite geladen werden sollen.<br />
</p>
<p>
<label for="my_meta_box_page">Kategorien</label>
<select name="my_meta_box_page" id="my_meta_box_page">
<?php
$args = array(
'orderby' => 'id',
'hide_empty'=> 0,
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo '<option value="'.$cat->name; selected( $selected, $cat->name ); echo '">'.$cat->name; echo'</option>';
}
?>
</select>
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchors can only have href attribute
)
);
// Make sure your data is set before trying to save it
if( isset( $_POST['my_meta_box_postvariante'] ) )
update_post_meta( $post_id, 'my_meta_box_postvariante', esc_attr( $_POST['my_meta_box_postvariante'] ) );
if( isset( $_POST['my_meta_box_page'] ) )
update_post_meta( $post_id, 'my_meta_box_page', esc_attr( $_POST['my_meta_box_page'] ) );
}
?>