我正在尝试向页面管理员添加一个自定义元框,将所有后期类别显示为选项 . 我已经使元框出现但我无法弄清楚如何显示类别 . 下拉列表是空的 .
这是我的代码:
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
}
我找不到自己的错 . 有人可以帮忙吗?谢谢!
编辑:
我发现了这个问题 . 删除以下行有助于: 'child_of' => 5, //Child From Boxes Category
现在我遇到的问题是wordpress没有存储我的选择 . 保存 my_meta_box_postvariante
的值可以正常工作 . 但 my_meta_box_page
的值始终保持默认值 . 我不能让它工作......我的代码:
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.
</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'] ) );
}
?>