yuheijotaki.com

WordPress スニペット その2

この前のよりは少し使用頻度は低いもの
 
◯PC / スマホ 振り分け
→ functions.php

<?php
function detect_sp() {
	$agent = @$_SERVER['HTTP_USER_AGENT'];
	if (strpos($agent, "iPhone")) {
		return true;
	} else if (strpos($agent, "iPod")) {
		return true;
	} else if (strpos($agent, "Android")) {
		if (strpos($agent, "Mobile")  !== false) {
			return true;
		}
	}
	return false;
}
?>

→ テンプレートファイル

<?php if ( detect_sp() ) : ?>
	<!-- スマホ用の記述 -->
<?php else : ?>
	<!-- PC用の記述 -->
<?php endif; ?>

 
◯カスタム投稿タイプ/タクソノミー作成
→ functions.php

<?php
	// カスタム投稿タイプ 作成
	register_post_type( 'POST_TYPE_NAME',
		array(
			'labels' => array(
				'name' => __( 'POST_TYPE_NAMEの名前' ),
				'singular_name' => __( 'POST_TYPE_NAMEの名前' )
			),
			'public' => true,
			'menu_position' => 5,
			'supports' => array('title','editor','thumbnail',
			'custom-fields','excerpt','author','trackbacks',
			'comments','revisions','page-attributes'),
			'has_archive' => true
		)
	);
	// タクソノミー 作成
	register_taxonomy(
		'TAXONOMY_NAME_category',
		'POST_TYPE_NAME',
		array(
			'hierarchical' => true,
			'update_count_callback' => '_update_post_term_count',
			'label' => 'TAXONOMY_NAMEのラベル',
			'singular_label' => 'TAXONOMY_NAMEのラベル',
			'rewrite' => array(
				'slug' => 'TAXONOMY_NAME'
			),
			'public' => true,
			'show_ui' => true
		)
	);
?>

 
◯ウィジェット作成/出力
→ functions.php

<?php
register_sidebar( array(
	'name' => 'ウィジェット名',
	'id' => 'my_widget',
	'description' => 'ウィジェットの説明文が入ります。',
	'before_widget' => '',
	'after_widget' => '',
	'before_title' => '',
	'after_title' => ''
) );
?>

 
→ テンプレートファイル

<?php if ( is_active_sidebar( 'my_widget' ) ) : ?>
	<!-- ウィジェットに入力項目がある場合 -->
	<?php dynamic_sidebar( 'my_widget'); ?>
<?php else : ?>
	<!-- ウィジェットに入力項目がない場合 -->
<?php endif; ?>

 
◯サイト情報の出力
→ テンプレートファイル

<!-- サイトURLを出力 -->
<?php echo site_url(); ?>

<!— サイト説明文を出力 —> <?php bloginfo(‘description’); ?>

<!— テンプレートURLを出力 —> <?php bloginfo(‘template_url’); ?>

 
◯カスタム投稿タイプ/タクソノミー/タームの出力
→ テンプレートファイル

<!-- カスタム投稿タイプ名を出力 -->
<?php
	$postType = get_query_var( 'post_type' );
	$postTypeName = esc_html( get_post_type_object( $postType )->label );
?>

<!— ターム名、タームスラッグを出力(index.phpなど) —> <?php $terms = get_the_terms($post->ID,array(‘POST_TYPE_NAME’)); if ( $terms && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { $termName = $term->name; $termSlug = $term->slug; } } else { // タームが見つからない場合 $termName = ’-’; $termSlug = ’-’; } ?>

<!— ターム名、タームスラッグを出力(taxonomy.php) —> <?php $taxonomy = get_query_var(‘taxonomy’); $term = get_term_by(‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) ); $termName = $term->name; $termSlug = $term->slug; ?>

 
◯ターム一覧の出力
→ テンプレートファイル

<!-- ターム一覧を出力 -->
<?php
	$taxonomy = 'TAXONOMY_NAME';
	$args = array(
		'hide_empty' => true
	);
	$terms = get_terms( $taxonomy , $args );
	if ( count( $terms ) != 0 ) {
		echo '<ul>';
		foreach ( $terms as $term ) {
			$term = sanitize_term( $term, $taxonomy );
			$term_link = get_term_link( $term, $taxonomy );
			if ( is_wp_error( $term_link ) ) {
				continue;
			}
			echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '(' . $term->count . ')</a></li>';
		}
		echo '</ul>';
	}
?>

 
◯is_main_query
→ functions.php

<?php
function mont_posts_per_page( $wp_query ) {
	if (!is_admin()) {
		if ( $wp_query->is_main_query() && $wp_query-> is_front_page() ) {
			$wp_query->set( 'post_type', array('POST_TYPE_NAME') );
			$wp_query->set( 'posts_per_page', 15 );
		}
	}
}
add_action( 'pre_get_posts', 'mont_posts_per_page' );
?>

→ テンプレートファイル

<?php if($wp_query -> have_posts()): ?>
	<?php while($wp_query -> have_posts()): $wp_query->the_post();?>
		<?php the_permalink(); ?>
		<?php the_title(); ?>
	<?php endwhile; ?>
<?php endif; ?>

 
◯ページ送り関連
→ テンプレートファイル

<!-- 最大ページ数を表示 -->
<?php echo $wp_query->max_num_pages; ?>

<!— 次のページがある場合の処理 —> <?php $next_link = get_next_posts_link(); ?> <?php if ( isset( $next_link ) ) : ?> <!— 次のページがある場合 —> <?php endif; ?>

 
◯検索関連タグ
→ テンプレートファイル

<!-- 検索フォーム -->
<form role="search" method="get" id="searchform" action="<?php echo home_url(); ?>">
	<?php if ( is_search() ) : ?>
		<input type="text" placeholder="検索" name="s" id="s" value="<?php the_search_query(); ?>">
	<?php else : ?>
		<input type="text" placeholder="検索" name="s" id="s">
	<?php endif; ?>
	<input type="submit">
</form>

<!— 検索キーワードを出力 —> 「<?php the_search_query(); ?>」の検索結果

<!— 検索件数を出力 —> <?php echo $wp_query->found_posts; ?>件

 
◯管理画面設定
→ functions.php

<?php
// 管理画面のメニューを非表示
function remove_admin_menus() {
	global $menu;
	unset($menu[2]);  // ダッシュボード
	unset($menu[5]);  // 投稿
	unset($menu[10]); // メディア
	unset($menu[20]); // 固定ページ
	unset($menu[25]); // コメント
	unset($menu[60]); // 外観
	unset($menu[65]); // プラグイン
	unset($menu[70]); // ユーザー
	unset($menu[75]); // ツール
	unset($menu[80]); // 設定
}
add_action('admin_menu', 'remove_admin_menus');
?>

 
→ functions.php

<?php
// 管理画面 ダッシュボードにカスタム投稿タイプの投稿数を表示
function dashboard_customposttype_items( $elements ) {
	foreach ( array( 'POST_TYPE_NAME','POST_TYPE_NAME' ) as $post_type ) {
		$num_posts = wp_count_posts( $post_type );
		if ( $num_posts && $num_posts->publish ) {
			$text = number_format_i18n( $num_posts->publish ).' 件';
			$postTypeLabel = get_post_type_object( $post_type )->label;
			$elements[] = sprintf( '<a href="edit.php?post_type=%1$s" class="%1$s-count"><b>%3$s</b>:%2$s</a>', $post_type, $text, $postTypeLabel );
		}
	}
	return $elements;
}
add_filter( 'dashboard_glance_items', 'dashboard_customposttype_items' );

// 管理画面 ダッシュボードにカスタム投稿タイプの投稿数を表示 アイコン function dashboard_style() { ?> <style> #dashboard_right_now .POST_TYPE_NAME-count:before, #dashboard_right_now .POST_TYPE_NAME-count:before { content: “\f109”; } </style> <?php } add_action( ‘admin_print_styles’, ‘dashboard_style’ );

?>