条件分岐で「モバイルでiOS以外」とし、グーグルフォントを読み込ませる
下の例は、明朝体フォントを読み込ませる記述
<?php if( wp_is_mobile() && !is_iOS()): ?> <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+JP&display=swap"> <?php endif; ?>
条件分岐で「モバイルでiOS以外」とし、グーグルフォントを読み込ませる
下の例は、明朝体フォントを読み込ませる記述
<?php if( wp_is_mobile() && !is_iOS()): ?> <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+JP&display=swap"> <?php endif; ?>
Advanced Custom Fieldsのカスタムフィールドを利用してカスタム投稿タイプのタームで記事を絞り込む例
tax_queryのrelationをANDにしてブランドとタイプで絞り込み、WP_Queryでループさせる
カスタム投稿タイプ:items
カスタム投稿タイプのタクソノミー:items_category
カスタムフィールド:00brand , item_type , 02item-image1(メイン画像)
<h3>関連商品</h3>
<div id="recommend">
<?php
$brand = get_field('00brand');
$type = get_field('item_type');
$args = array(
'post_type' => 'items', //カスタム投稿タイプ名
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'items_category',
'field' => 'slug',
'terms' => $brand,
),
array(
'taxonomy' => 'items_category',
'field' => 'slug',
'terms' => $type,
),
),
'numberposts' => 12, //12件表示(デフォルトは5件)
'orderby' => 'abc', //ランダム表示 rand
'post__not_in' => array($post->ID) //表示中の記事を除外
);
?>
<p><?php echo $type; ?></p>
<div class="item">
<div class="itemList inner clearfix">
<?php $recommend = new WP_Query( $args ); ?>
<?php if($recommend -> have_posts()): while($recommend -> have_posts()): $recommend -> the_post(); ?>
<?php
$attachment_id1 = get_field('02item-image1');
$size1 = "thumbnail"; // (thumbnail, medium, large, full or custom size)
$image1 = wp_get_attachment_image_src( $attachment_id1, $size1 );
$attachment_id1f = get_field('02item-image1');
$size1f = "large"; // (thumbnail, medium, large, full or custom size)
$image1f = wp_get_attachment_image_src( $attachment_id1f, $size1f );
$attachment = get_post( get_field('02item-image1') );
$alt1 = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title1 = $attachment->post_title;
?>
<div class="itemBox4"><a href="<?php the_permalink(); ?>"><img src="<?php echo $image1f[0]; ?>" alt="<?php echo $alt1; ?>" title="<?php echo $image_title1; ?>"><h4><?php echo nl2br(post_custom('01model')); ?></h4><p><?php echo nl2br(post_custom('03price')); ?></p></a></div>
<?php endwhile; ?>
</div><!--/itemList-->
</div><!--/item-->
<?php else : ?>
<p>関連アイテムはまだありません。</p>
</div><!--/itemList-->
</div><!--/item-->
<?php endif; wp_reset_postdata(); ?>
</div><!--/#recommend-->
WordPress 閲覧履歴をプラグイン無しで行う
※cookieを利用
header.phpのheadタグ内に以下を記述
解説ではheadタグ直下に記述しないとエラーになると書いてあったが、タグ内下部に記述しても動作した。
<?php
global $rireki;
//記事ページのみcookieに登録
if(is_single()){
//閲覧履歴用のcookieが存在する場合
if( isset($_COOKIE['rireki']) ){
//配列にする
$rireki = explode(",", $_COOKIE['rireki']);
//cookieに現在の記事IDがあるかどうか調べる
$aruno = in_array($post->ID, $rireki);
//ある場合の処理
if($aruno == true){
//cookieにある現在の記事IDを削除(順番整理&表示除外用)
$rireki = array_diff($rireki,array($post->ID));
$rireki = array_values($rireki);
}
//cookieが5個以上ある場合、4個に減らす
if(count($rireki) >= 5 ){
$set_rireki = array_slice($rireki , 0, 4);
}else{
$set_rireki = $rireki;
}
//cookieに登録
$touroku = $post->ID.','.implode(",", $set_rireki);
setcookie( 'rireki', $touroku, time() + 7776000,'/');
//cookieに現在の記事IDが無い場合の処理
}else{
$touroku = $post->ID;
setcookie( 'rireki', $touroku, time() + 7776000,'/');
}
//記事ページ以外ならcookieの読み込みのみ
}else{
if( isset($_COOKIE['rireki']) ){
$rireki = explode(",", $_COOKIE['rireki']);
}
}
?>
閲覧履歴を出力したい場所に以下記述
<?php
global $rireki;
//履歴が現在の記事を除いて、一つでもある場合
if (!empty($rireki)){
$args = array(
'posts_per_page' => -1,
'post__in' => $rireki,
'orderby' => 'post__in',
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<!--ここに記事を表示させる処理-->
<?php the_title(); ?>
<?php
endwhile;
endif;
wp_reset_postdata();
}else{ ?>
//cookieが無い場合の処理
<?php } ?>
カスタム投稿タイプのカスタムタクソノミー(カテゴリー・タグ)を作成時、
タグをチェックボックス化する場合、「’hierarchical’」をtrueにすればよいが、
デフォルトの投稿の場合は以下をfunctions.phpに記述する
2~7行の記述は必要ないかも・・・
function post_tag_checkbox() {
global $wp_rewrite;
$rewrite = array(
'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(),
'ep_mask' => EP_TAGS,
);
$labels = array(
'name' => _x( 'Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
'search_items' => __( 'Search Tags' ),
'popular_items' => __( 'Popular Tags' ),
'all_items' => __( 'All Tags' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Tag' ),
'view_item' => __( 'View Tag' ),
'update_item' => __( 'Update Tag' ),
'add_new_item' => __( 'Add New Tag' ),
'new_item_name' => __( 'New Tag Name' ),
'separate_items_with_commas' => __( 'Separate tags with commas' ),
'add_or_remove_items' => __( 'Add or remove tags' ),
'choose_from_most_used' => __( 'Choose from the most used tags' ),
'not_found' => __( 'No tags found.' )
);
register_taxonomy( 'post_tag', 'post', array(
'hierarchical' => true,
'query_var' => 'tag',
'rewrite' => $rewrite,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'_builtin' => true,
'labels' => $labels
));
}
add_action( 'init', 'post_tag_checkbox', 1 );
WordPress カスタム投稿タイプとカスタムタクソノミー(カテゴリー・タグ)の作成
functions.php
// カスタム投稿タイプを作成する
add_action('init', 'add_websites_post_type');
function add_websites_post_type() {
$params = array(
'labels' => array(
'name' => 'サイト',
'singular_name' => 'サイト',
'add_new' => '新規追加',
'add_new_item' => 'サイトを新規追加',
'edit_item' => 'サイトを編集する',
'new_item' => '新規サイト',
'all_items' => 'サイト一覧',
'view_item' => 'サイトの説明を見る',
'search_items' => '検索する',
'not_found' => 'サイトが見つかりませんでした。',
'not_found_in_trash' => 'ゴミ箱内にサイトが見つかりませんでした。'
),
'public' => true,
'has_archive' => true,
'supports' => array(
'title',
'editor',
'author',
'custom-fields',
),
'taxonomies' => array('websites_category','websites_tag')
);
register_post_type('websites', $params);
}
// カスタム投稿タイプ(websites)用のカテゴリ&タグを作成する
add_action('init', 'create_websites_taxonomies');
function create_websites_taxonomies() {
// カテゴリを作成
$labels = array(
'name' => 'Webカテゴリ', //複数系のときのカテゴリ名
'singular_name' => 'Webカテゴリ', //単数系のときのカテゴリ名
'search_items' => 'Webカテゴリを検索',
'all_items' => '全てのWebカテゴリ',
'parent_item' => '親カテゴリ',
'parent_item_colon' => '親カテゴリ:',
'edit_item' => 'Webカテゴリを編集',
'update_item' => 'Webカテゴリを更新',
'add_new_item' => '新規Webカテゴリを追加',
'new_item_name' => '新規Webカテゴリ',
'menu_name' => 'Webカテゴリ' //ダッシュボードのサイドバーメニュー名
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'rewrite' => array( 'slug' => 'websites_cat' )
);
register_taxonomy( 'websites_category', 'websites', $args );
// タグを作成
$labels = array(
'name' => 'Webタグ', //複数系のときのタグ名
'singular_name' => 'Webタグ', //単数系のときのタグ名
'search_items' => 'Webタグを検索',
'all_items' => '全てのWebタグ',
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Webタグを編集',
'update_item' => 'Webタグを更新',
'add_new_item' => '新規Webタグを追加',
'new_item_name' => '新規Webタグ',
'separate_items_with_commas' => 'Webタグをコンマで区切る',
'add_or_remove_items' => 'Webタグを追加or削除する',
'choose_from_most_used' => 'よく使われているWebタグから選択',
'not_found' => 'アイテムは見つかりませんでした',
'menu_name' => 'Webタグ' //ダッシュボードのサイドバーメニュー名
);
$args = array(
'hierarchical' => false, //trueでタグをチェックボックス化
'labels' => $labels,
'update_count_callback' => '_update_post_term_count', //タグの動作に必要なCallback設定
'rewrite' => array( 'slug' => 'websites_tag' )
);
register_taxonomy( 'websites_tag', 'websites', $args );
}