WordPressテーマ制作【応用】

アイキャッチ画像

functions.php
add_theme_support('post-thumbnails');//投稿画面にサムネイル表示
set_post_thumbnail_size(640, 360, true);

アップロードされたファイルのサイズを指定

add_image_size('item', 320, 180, true);
add_image_size('item_s', 640, 360, true);
add_image_size('large_item_s', 1280, 720, true);

カスタム投稿タイプ

functions.php
//カスタム投稿タイプ「作品」を追加
add_action( 'init', 'add_post_type_work', 0 );

function add_post_type_work(){
$args = array(
	'labels' => array('name' => '作品'),
	'public' => true,
	'menu_position' =>5,
	'has_archive' => true,
	'taxonomies' => array( 'category', 'post_tag' ),//WPカテゴリー・WPタグ
	'supports' => array('title','editor','excerpt','thumbnail','author'),
);
register_post_type('work', $args);
}
//カスタム分類「作品」を追加
add_action( 'init', 'add_custom_taxonomy_work', 0 );

function add_custom_taxonomy_work(){
$args = array(
	'hierarchical' => true,//trueでカテゴリー
	'update_count_callback' => '_update_post_term_count',
	'label' => '作品カテゴリー',
	'public' => true,
	'show_ui' => true,
	'query_var' => true, //URLの最適化。trueでOK!
	'rewrite' => array(
		'slug' => 'work/category/', //★URLでnews_catと表記されるのをnewsに変更
		'hierarchical' => true, //★true にすると階層化したURLを使用可能にする
		 'with_front' => false,
	)
);
register_taxonomy('work-cat','work',$args);

$args = array(
	'hierarchical' => false,//falseでタグ
	'update_count_callback' => '_update_post_term_count',
	'label' => '作品タグ',
	'public' => true,
	'show_ui' => true,
	'query_var' => true, //URLの最適化。trueでOK!
	'rewrite' => array(
		'slug' => 'work/tag/', //★URLでnews_catと表記されるのをnewsに変更
		'hierarchical' => true, //★true にすると階層化したURLを使用可能にする
		 'with_front' => false,
	)
);
register_taxonomy('work-tag','work',$args);
}
// カスタム投稿とカスタムタクソノミーを同一スラッグ(work)にした際に、ページネーションででる404エラーの回避
add_rewrite_rule('work/category/([^/]+)/?$', 'index.php?work-cat=$matches[1]', 'top');
add_rewrite_rule('work/category/([^/]+)/page/([0-9]+)/?$', 'index.php?work-cat=$matches[1]&paged=$matches[2]', 'top');
add_rewrite_rule('work/tag/([^/]+)/?$', 'index.php?work-tag=$matches[1]', 'top');
add_rewrite_rule('work/tag/([^/]+)/page/([0-9]+)/?$', 'index.php?work-tag=$matches[1]&paged=$matches[2]', 'top');

カスタムフィールド

「Advanced Custom Fields」

パンくずリスト

「Breadcrumb NavXT」

ページネーション

「WP-PageNavi」

ウィジェット

//ウィジェット有効化
function my_theme_widgets_init() {
	register_sidebar(array(
		'name' => 'サイドバー',
		//'name' => 'Main Sidebar',
		'id' => 'main-sidebar',
		'before_widget'=> '<li id="%1$s" class="%2$s sidebar-wrapper">',
		'after_widget'=> '</li>',
		'before_title' => '<h2>',
		'after_title' => '</h2>',
	));
  
	register_sidebar(array(
		'name' => 'フッター',
		'id' => 'footer-widgets',
		'before_widget'=> '<div class="sidebar-wrapper">',
		'after_widget'=> '</div>',
		'before_title' => '<h4 class="sidebar-title">',
		'after_title' => '</h4>',
	));
}
add_action( 'widgets_init', 'my_theme_widgets_init' );	

ブログカード

ショートコード

//おすすめ商品テーブル
function shortcode_sample($atts, $content = null) {
  return 'ここに内容を入れる';
}
add_shortcode( 'sample', 'shortcode_sample' );

コピーできました!