WordPress カテゴリーリストにスラグclass追加等

この記事は年前に書かれました。不適当な記述を含む場合がありますので、参考程度に留めてください。

サムネイル利用

function.phpに記述


function mysetup() {
add_theme_support( ‘post-thumbnails’ );
}
add_action( ‘after_setup_theme’, ‘mysetup’ );

カテゴリーリストにスラグclass追加

function.phpに記述


function add_cat_slug_class( $output, $args ) {
$regex = ‘/<li class="cat-item cat-item-([d]+)[^"]*">/’;
$taxonomy = isset( $args[‘taxonomy’] ) && taxonomy_exists( $args[‘taxonomy’] ) ? $args[‘taxonomy’] : ‘category’;

preg_match_all( $regex, $output, $m );

if ( ! empty( $m[1] ) ) {
$replace = array();
foreach ( $m[1] as $term_id ) {
$term = get_term( $term_id, $taxonomy );
if ( $term && ! is_wp_error( $term ) ) {
$replace[‘/<li class="cat-item cat-item-‘ . $term_id . ‘("| )/’] = ‘<li class="cat-item cat-item-‘ . $term_id . ‘ ‘ . esc_attr( $term->slug ) . ‘$1’;
}
}
$output = preg_replace( array_keys( $replace ), $replace, $output );
}
return $output;
}
add_filter( ‘wp_list_categories’, ‘add_cat_slug_class’, 10, 2 );

カスタム投稿タイプの追加

「イペント(“event”)」というカスタム投稿タイプと「イベントタグ(“event_tag”)」というタクソノミーを追加
 
function.phpに記述


function add_event_type() { //カスタム投稿タイプ設定
$cp = array(
‘label’ => ‘イベント’,
‘labels’ => array(
‘singular_name’ => ‘イベント’,
‘add_new_item’ => ‘イベントを追加’,
‘add_new’ => ‘新規追加’,
‘new_item’ => ‘新規投稿’,
‘view_item’ => ‘イベントを表示’,
‘not_found’ => ‘イベントは見つかりませんでした’,
‘not_found_in_trash’ => ‘ゴミ箱にイベントはありません。’,
‘search_items’ => ‘イベントを検索’,
),
‘public’ => true,
‘hierarchical’ => false,
‘menu_position’ => 5,
‘supports’ => array(‘title’,’editor’,’author’,’thumbnail’,’excerpt’,’comments’,’custom-fields’),
‘has_archive’ => true
);
register_post_type(‘event’, $cp);

$args = array( //タクソノミー設定
‘label’ => ‘イベントタグ’,
‘labels’ => array(
‘name’ => ‘タグ’,
‘singular_name’ => ‘タグ’,
‘search_items’ => ‘タグを検索’,
‘popular_items’ => ‘よく使われているタグ’,
‘all_items’ => ‘すべてのタグ’,
‘parent_item’ => ‘タグ’,
‘edit_item’ => ‘タグの編集’,
‘update_item’ => ‘更新’,
‘add_new_item’ => ‘タグを追加’,
‘new_item_name’ => ‘タグ’,
),
‘public’ => true,
‘show_ui’ => true,
‘hierarchical’ => false,
‘show_tagcloud’ => true
);
register_taxonomy(‘event_tag’, ‘event’, $args); //"event"に"event_tag"というタクソノミー作成

flush_rewrite_rules();
}
add_action(‘init’, ‘add_event_type’); //カスタム投稿タイプ"event"作成

カスタム投稿タイプ”event”を5件表示


<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query(‘post_type=event’ . ‘&paged=’ . $paged . ‘&posts_per_page=5’);
?>
<?php if(have_posts()): while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

複数インデックスページ

index-AAA.php => http://example.com/archives/1?type=AAA
 
function.phpに記述


function index_template_switch($template) {
$new_template = $template;
if (isset($_GET[‘type’])) {
$new_template = ‘index-‘ . esc_html($_GET[‘type’]) . ‘.php’;
if (is_array($template)) {
$new_template = array(
$new_template,
isset($template[1]) ? $template[1] : ‘index.php’
);
} else {
$new_template = preg_replace(‘/[^/]+.php$/i’, $new_template, $template);
if (!file_exists($new_template)) {
$new_template = $template;
}
}
}
return $new_template;
}
add_filter(‘index_template’, ‘index_template_switch’);

文字数制限


<?php echo mb_substr(strip_tags($post->post_content), 0, 200); ?>