megane9988
8/12/2013 - 4:06 AM

カスタム投稿タイプにメールマガジンを登録し、パーマネントリンクの設定及び、月別のarchiveの設定を行う。

カスタム投稿タイプにメールマガジンを登録し、パーマネントリンクの設定及び、月別のarchiveの設定を行う。

// メールマガジンのカスタム投稿タイプセット-----------------------------------
<?php
function mailmagazine_custom_init() {
  /**
	* カスタム投稿タイプ mailmagazine
	*/
	$mailmagazine_labels = array(
		'name' => "メールマガジン",
		'singular_name' => "メールマガジン",
		'add_new' => "新規追加",
		'add_new_item' => "新規メールマガジン追加",
		'edit_item' => "メールマガジンを編集",
		'new_item' => "新規メールマガジン",
		'view_item' => "表示",
		'search_items' => "メールマガジンを検索",
		'not_found' => "見つかりません",
		'not_found_in_trash' =>"ゴミ箱にはありません",
		'parent_item_colon' => '親',
		'menu_name' => 'メールマガジン'
	);
	$mailmagazine_args = array(
		'labels' => $mailmagazine_labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
		'show_in_menu' => true,
		'query_var' => true,
		'rewrite' => true,
		'has_archive' => true,
		'hierarchical' => false,
		'menu_position' => 5,
		'map_meta_cap' => true,
		'supports' => array( 'title', 'editor')
	);
	register_post_type( 'mailmagazine', $mailmagazine_args );
}
add_action( 'init', 'mailmagazine_custom_init', 0);

// メールマガジンのカスタム投稿タイプにパーマネントリンクを設置-----------------------------------
function myposttype_rewrite() {
    global $wp_rewrite;
  
    $queryarg = 'post_type=mailmagazine&p=';
    $wp_rewrite->add_rewrite_tag('%mailmagazine_id%', '([^/]+)',$queryarg);
    $wp_rewrite->add_permastruct('mailmagazine', '/mailmagazine/%mailmagazine_id%', false);
  
}
add_filter('post_type_link', 'myposttype_permalink', 1, 3);
function myposttype_permalink($post_link, $id = 0, $leavename) {
    global $wp_rewrite;
    $post = &get_post($id);
    if ( is_wp_error( $post ) )
        return $post;
    $newlink = $wp_rewrite->get_extra_permastruct($post->post_type);
    $newlink = str_replace('%'.$post->post_type.'_id%', $post->ID, $newlink);
    $newlink = home_url(user_trailingslashit($newlink));
    return $newlink;
}
add_action('init', 'myposttype_rewrite');

// メールマガジンのカスタム投稿タイプに月別のarchiveを設置-----------------------------------

global $my_archives_post_type;
//
// post_type = post => post_type=disco に変更
//
function my_getarchives_where( $where, $r ) {
global $my_archives_post_type;

$my_archives_post_type = '';
if ( isset($r['post_type']) ) {
$my_archives_post_type = $r['post_type'];
$where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
}
return $where;
}

//
// ?post_type=disco を追加
//
function my_get_archives_link( $link_html ) {
global $my_archives_post_type;

if ($my_archives_post_type != '') {
$add_link .= '?post_type=' . $my_archives_post_type;
$link_html = preg_replace("/href=\'(.+)\'\s/","href='$1".$add_link."'",$link_html);
}
return $link_html;
}
?>

サイドバーへの記載
<?php
add_filter( 'getarchives_where', 'my_getarchives_where', 10, 2 );
add_filter( 'get_archives_link', 'my_get_archives_link', 10, 2 );
wp_get_archives('type=monthly&post_type=mailmagazine');
remove_filter( 'getarchives_where', 'my_getarchives_where');
remove_filter( 'get_archives_link', 'my_get_archives_link' );
?>