在post meta wordpress上显示帖子类型

时间:2013-11-29 01:04:52

标签: wordpress

我有自定义帖子类型和帖子元

的代码
add_action( 'init', 'create_sidebar_picture' );

function create_sidebar_picture() {
register_post_type( 'sidebar_picture',
array(
'labels' => array(
'name' => 'Sidebar Pictures',
'singular_name' => 'Sidebar Pictures',
'add_new' => 'Add New',
'add_new_item' => 'Add New Sidebar Picture',
'edit' => 'Edit',
'edit_item' => 'Edit Sidebar Picture',
'new_item' => 'New Sidebar Picture',
'view' => 'View Sidebar Picture',
'view_item' => 'View ',
'search_items' => 'Search Sidebar Picture',
'not_found' => 'No Sidebar Picture found',
'not_found_in_trash' =>
'No Sidebar Picture found in Trash',
'parent' => 'Parent Sidebar Picture'
),
'public' => true,
'menu_position' => 15,
'supports' =>
array( 'title', 'editor', 'comments',
'thumbnail',  ),
'taxonomies' => array( '' ),
'menu_icon' => get_template_directory_uri() . '/images/ggicon.png',
'has_archive' => true
)
);
}

add_action( 'admin_init', 'my_admin' );

function my_admin() {
add_meta_box( 'sidebar_picture_meta_box',
'Sidebar Picture Details',
'display_sidebar_picture_meta_box',
'sidebar_picture', 'normal', 'high' );
}

function display_sidebar_picture_meta_box( $sidebar_picture ) {
// Retrieve current name of the Director and Movie Rating based on review ID
$name_picture = esc_html( get_post_meta( $sidebar_picture->ID, 'name_picture', true ) );
$sidebar_group = intval( get_post_meta( $sidebar_picture->ID,'sidebar_group', true ) );
?>
<table>
<tr>
<td style="width: 100%">Picture Name</td>
<td><input type="text" size="80"
name="sidebar_picture_name_picture"
value="<?php echo $name_picture; ?>" /></td>
</tr>
<tr>
<td style="width: 150px">Which Sidebar You Want This To Be Displayed?</td>
<td>
<select style="width: 100px"
name="sidebar_picture_sidebar_group">
<?php
// Generate all items of drop-down list
for ( $rating = 2; $rating >= 1; $rating -- ) {
?>
<option value="<?php echo $rating; ?>"
<?php echo selected( $rating,
$sidebar_group ); ?>>
<?php echo $rating; ?> stars
<?php } ?>
</select>
</td>
</tr>
</table>
<?php }


add_action( 'save_post', 'add_sidebar_picture_fields', 10, 2 );


function add_sidebar_picture_fields( $sidebar_picture_id, $sidebar_picture ) {
// Check post type for movie reviews
if ( $sidebar_picture->post_type == 'sidebar_picture' ) {
// Store data in post meta table if present in post data
if ( isset( $_POST['sidebar_picture_name_picture'] ) &&
$_POST['sidebar_picture_name_picture'] != '' ) {
update_post_meta( $sidebar_picture_id, 'name_picture',
$_POST['sidebar_picture_name_picture'] );
}
if ( isset( $_POST['sidebar_picture_sidebar_group'] ) &&
$_POST['sidebar_picture_sidebar_group'] != '' ) {
update_post_meta( $sidebar_picture_id, 'sidebar_group',
$_POST['sidebar_picture_sidebar_group'] );
}
}
}

从上面的代码可以看出,它由post meta组成post类型。现在,我有这个代码(请参见下文),它显示了帖子类型侧边栏图片。

<?php query_posts(array('post_type'=>'sidebar_picture')); ?>
<?php $mypost = array( 'post_type' => 'sidebar_picture' );
$loop = new WP_Query( $mypost ); ?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php endwhile; ?>

以上代码显示帖子类型。现在,我想要的是显示所有帖子类型的帖子元组边栏​​组为1,这里的任何人都可以告诉我如何制作它吗?目前我在网上寻找解决方案,但到目前为止,没有发现任何好处。

我打开任何建议,建议和想法。提前谢谢。

1 个答案:

答案 0 :(得分:1)

使用meta_query arg:

$loop = new WP_Query( array(
    'post_type' => 'sidebar_picture',
    'meta_query' => array(
        array(
            'key' => 'sidebar_group',
            'value' => '1'
        )
    )
) );

更多信息:http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters