カスタム投稿タイプを設置してみる

記事内に広告が含まれていることがあります

カスタム投稿タイプ を設置するための備忘記録

《functions.php》
add_action(‘init’, ‘my_custom_stuff’);
function my_custom_stuff(){
register_post_type(
‘trial’,
array(
‘label’ => __(‘Trials’),
‘singular_label’ => __(‘Trial’),
‘public’ => true,
‘show_ui’ => true,
‘capability_type’ => ‘post’,
‘hierarchical’ => false,
‘rewrite’ => false,
‘query_var’ => false,
‘supports’ => array(
‘title’,
‘editor’,
‘thumbnail’
),
‘register_meta_box_cb’ => ‘my_trial_meta_box’
)
);

register_taxonomy(
‘trialtag’,
‘trial’,
array(
‘hierarchical’ => false,
‘update_count_callback’ => ‘_update_post_term_count’,
‘label’ => ‘トライアルの分類’,
‘singular_label’ => ‘トライアルの分類’,
‘public’ => true,
‘show_ui’ => true
)
);
}
//
function my_trial_meta_box($post){
add_meta_box(‘my_trial_meta’, ‘お気に入り度’, ‘my_trial_meta_html’, ‘trial’, ‘normal’, ‘high’);
}
function my_trial_meta_html($post, $box){
$rating = get_post_meta($post->ID, ‘rating’, true);
echo wp_nonce_field(‘my_trial_meta’, ‘my_meta_nonce’);
echo ‘このトライアルは… ‘;
echo ‘<input type=”radio” name=”rating” value=”試してみたい!”‘ .(‘試してみたい!’ == $rating ? ‘ checked=”checked”‘ : ”). ‘>めっちゃ好き ’;
echo ‘<input type=”radio” name=”rating” value=”どーでもいい!”‘ .(‘どーでもいい!’ == $rating ? ‘ checked=”checked”‘ : ”). ‘>嫌い! ’;
echo ‘<input type=”radio” name=”rating” value=”フツー”‘ .(‘フツー’ == $rating ? ‘ checked=”checked”‘ : ”). ‘>フツー’;
}
add_action(‘save_post’, ‘my_trial_meta_update’);
function my_trial_meta_update($post_id){
if(!wp_verify_nonce( $_POST[‘my_meta_nonce’], ‘my_trial_meta’))
return $post_id;

if(defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE)
return $post_id;

if(‘trial’ == $_POST[‘post_type’]){
if(!current_user_can(‘edit_post’, $post_id))
return $post_id;
}else{
return $post_id;
}

$rating = $_POST[‘rating’];
if($rating == ”)
delete_post_meta($post_id, ‘rating’);
else
update_post_meta($post_id, ‘rating’, $rating);
}
//
add_filter(‘manage_edit-trial_columns’, ‘my_trial_columns’);
function my_trial_columns($columns){
$columns = array(
‘cb’ => ‘<input type=”checkbox”/>’,
‘title’ => ‘トライアル’,
‘image’ => ‘画像’,
‘tag’ => ‘分類’,
‘rating’ => ‘お気に入り度’,
‘date’ => ‘日付’
);
return $columns;
}
//
add_action(‘manage_posts_custom_column’, ‘my_trial_column’);
function my_trial_column($column){
global $post;
if(‘image’ == $column) the_post_thumbnail(array(64, 64), ‘class=featured-image’);
elseif (“tag” == $column) the_terms(0, ‘trialtag’);
elseif (“rating” == $column) echo get_post_meta($post->ID, ‘rating’, true);
}
?>

 

今回はこれをサイドバーに表示させた

《sidebar.php》

<!– カスタム投稿タイプ設定 –>
<?php
$myQuery = new WP_Query(); // WP_Queryオブジェクト生成
$param = array( //パラメータ。
‘posts_per_page’ => ’10’, //(整数)- 1ページに表示する記事数。-1 ならすべての投稿を取得。
‘post_type’ => ‘trial’, //カスタム投稿タイプのみを指定。
‘post_status’ => ‘publish’, //取得するステータスを指定:publish(公開済み)
‘orderby’ => ‘ID’,
‘order’ => ‘DESC’ //降順。大きい値から小さい値の順。
);
$myQuery->query($param); // クエリにパラメータを渡す
?>

<!– カスタム投稿タイプ表示 –>
<ul class=”submenu”>
<?php if($myQuery->have_posts()): while($myQuery->have_posts()) : $myQuery->the_post(); ?>
<li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>の詳細へ”><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>
</ul>

コメント