スニペット/WordPress single posts
breadcrumb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php $cats = get_the_category(); $print = ''; foreach ($cats as $category) { if ($category -> parent == 1720) { $cat_url = get_category_link($category -> cat_ID); $print = $print . '<a href="' . $cat_url . '">' . $category -> cat_name . '</a>' . ', '; } else if ($category -> parent == 1163) { $cat_url = get_category_link($category -> cat_ID); $print = $print . '<a href="' . $cat_url . '">' . $category -> cat_name . '</a>' . ', '; } else if ($category -> parent == 1160) { $cat_url = get_category_link($category -> cat_ID); $print = $print . '<a href="' . $cat_url . '">' . $category -> cat_name . '</a>' . ', '; } } ?> <?php if (strlen($print)) _e("<div>" . rtrim($print, ', ') . "</div>"); ?> |
prev-next
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <nav id="nav-pn"> <ul> <?php $prevpost = get_previous_post(true); ?> <?php $nextpost = get_next_post(true); ?> <?php if ($prevpost): ?> <?php $prevThumb = get_the_post_thumbnail($prevpost->ID, 'full'); ?> <li <?php if (is_latest_post()): ?>class="item-prev"<?php endif; ?>> <a href="<?php echo get_permalink($prevpost->ID); ?>"><dl> <dt><?php echo $prevThumb; ?></dt> <dd>前へ<br><?php echo get_the_title($prevpost->ID); ?></dd> </dl></a> </li> <?php endif; ?> <?php if ($nextpost): ?> <?php $nextThumb = get_the_post_thumbnail($nextpost->ID, 'full'); ?> <li <?php if (is_oldest_post()): ?>class="item-next"<?php endif; ?>> <a href="<?php echo get_permalink($nextpost->ID); ?>"><dl> <dt><?php echo $nextThumb; ?></dt> <dd>次へ<br><?php echo get_the_title($nextpost->ID); ?></dd> </dl></a> </li> <?php endif; ?> </ul> <!-- /#nav-pn --></nav> |
posts_per_page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $args = array( 'post_type' => post, 'posts_per_page' => 1, // 'p' => 555 'orderby' => rand, 'category__and' => array(2083), 'cat' => '-466, -1785, -271' ); $postslist = get_posts($args); foreach ($postslist as $post) : setup_postdata($post); ?> <?php get_template_part('front/opus'); ?> <?php endforeach; wp_reset_postdata(); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php $args = array( 'posts_per_page' => 1, 'post_type' => 'hoge', 'tax_query' => array( array( 'field' => 'slug', // 必須 'taxonomy' => 'archive', 'terms' => 'week' ) ) ); $postslist = get_posts($args); foreach ($postslist as $post) : setup_postdata($post); ?> <?php get_template_part('article/blank'); ?> <?php endforeach; wp_reset_postdata(); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php $args = array( 'posts_per_page' => -1, 'category' => 2479 ); $postslist = get_posts($args); if (!empty($postslist)): ?> <section class="block-top-opus"> <?php foreach ($postslist as $post): setup_postdata($post); ?> <div class="inner-opus"> <a href="<?php the_permalink(); ?>"><dl> <dt><?php the_post_thumbnail(); ?> <div class="ic"> <?php $parent_cat_id = 9; $genre = custom_get_the_child_categories($parent_cat_id); if (!empty($genre)): $genre = $genre[0]; ?> <small class="ic-genre <?php echo $genre->slug; ?>"><?php echo $genre->cat_name; ?></small> <?php endif; ?> <?php $parent_cat_id = 226; $genre = custom_get_the_child_categories($parent_cat_id); if (!empty($genre)): $genre = $genre[0]; ?> <small class="ic-genre <?php echo $genre->slug; ?>"><?php echo $genre->cat_name; ?></small> <?php endif; ?> <?php $parent_cat_id = 10; $site = custom_get_the_child_categories($parent_cat_id); if (!empty($site)): $site = $site[0]; ?> <small class="ic-genre site"><?php echo $site->cat_name; ?></small> <?php endif; ?> </div> </dt> <dd><?php the_title(); ?> </dd> </dl></a> <!-- /.inner-opus --></div> <?php endforeach; wp_reset_postdata(); ?> <!-- /.block-top-opus --></section> <?php endif; ?> |
relation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | function custom_get_the_child_categories($parent_cat_id, $post_id = 0) { // カレント記事の属するカテゴリーを配列で取得 $post_cats = get_the_category($post_id); if (empty($post_cats)) return array(); // それらのIDをカンマ区切りで取得 $post_cat_ids = ''; foreach ($post_cats as $post_cat) { if (!empty($post_cat_ids)) $post_cat_ids .= ','; $post_cat_ids .= $post_cat->term_id; } // 投稿が属するカテゴリーのうち指定カテゴリーを親とするものを取得 $args = array( 'parent' => $parent_cat_id, // 親カテゴリーの直下の子供に限定 'order_by' => 'id', // ID順 'order' => 'asc', // 昇順 'hide_empty' => 1, // 記事のないカテゴリーは出力しない 'include' => $post_cat_ids, // 記事の属するカテゴリーに限定する ); $child_cats = get_categories($args); return $child_cats; } // 指定カテゴリーに属する記事を指定分取得する。ただし、カレントの記事は除く function custom_get_sibling_posts($cat_id, $post_id, $orderby = 'ID', $posts_per_page = 3) { $sibling_posts = array(); $total_num = 0; // 指定カテゴリーに属する記事を指定分取得。ただし、カレントの記事は除く $args = array( 'orderby' => $orderby, // ID順 'order' => 'DESC', // 降順 'posts_per_page' => $posts_per_page, // 取得する記事数 'cat' => $cat_id, // カテゴリーID 'suppress_filters' => true, // get_posts() と同じにする 'post__not_in' => array($post_id) // カレントの記事は除く ); $query = new WP_Query($args); return $query; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | <?php $parent_cat_id = 13; // 親カテゴリ $parent_cat = get_category($parent_cat_id); // 投稿が属するカテゴリーのうち指定カテゴリーを親とするものを取得 $child_cats = custom_get_the_child_categories($parent_cat_id, $post->ID); if (!empty($child_cats)): foreach ($child_cats as $child_cat): // A ?> <?php // 指定カテゴリーに属する記事を指定分取得。ただし、カレントの記事は除く $orderby = 'rand'; $posts_per_page = 3; $queried_datum = custom_get_sibling_posts($child_cat->term_id, $post->ID, $orderby, $posts_per_page); if (!empty($queried_datum)): // B $sibling_posts = $queried_datum->posts; $found_posts = $queried_datum->found_posts; if (!empty($sibling_posts)) : // C 該当する記事がある場合のみsection出力 ?> <h3 class="h-lv3"><?php echo esc_html($child_cat->name); ?>(<?php echo $child_cat->count; ?>)</h3> <section class="section-parts"> <?php foreach ($sibling_posts as $post) : setup_postdata($post); ?> <div class="inner-parts"> <a href="<?php the_permalink();?>"><dl> <dt><?php the_post_thumbnail(); ?> <div class="ic"> <?php $parent_cat_id = 9; $genre = custom_get_the_child_categories($parent_cat_id); if (!empty($genre)): $genre = $genre[0]; ?> <small class="ic-genre <?php echo $genre->slug; ?>"><?php echo $genre->cat_name; ?></small> <?php endif; ?> <?php $parent_cat_id = 10; $site = custom_get_the_child_categories($parent_cat_id); if (!empty($site)): $site = $site[0]; ?> <small class="ic-genre site"><?php echo $site->cat_name; ?></small> <?php endif; ?> </div> </dt> <dd><?php the_title(); ?></dd> </dl></a> <p><time>配信日:<?php if (get_field('release')): ?><?php the_field('release'); ?><?php else: ?>-<?php endif; ?></time> <?php if (in_category(array('heydouga', 'traveling-glossmen')) ): ?> 販売価格:$<?php the_field('price'); ?>~ <?php else: ?> 販売価格:<?php the_field('price'); ?>円~ <?php endif; ?> </p> <?php $days = 30; $today = date_i18n('U'); $entry = get_the_time('U'); $elapsed = date('U',($today - $entry)) / 86400; if ($days > $elapsed){ echo '<span class="ic-new">NEW</span>'; } ?> <!-- /.inner-parts --></div> <?php endforeach; wp_reset_postdata(); ?> <?php if ($found_posts > count($sibling_posts)) : // D ?> <span><a href="<?php echo esc_url(get_category_link($child_cat->term_id)); ?>"><?php echo esc_html($child_cat->name); ?>一覧</a></span> <?php endif; // D ?> <!-- /.section-parts --></section> <?php endif; // C ?> <?php endif; // B ?> <?php endforeach; endif; // A ?> |
投稿ページ振り分け
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function single_branch($single_template) { $new_template = $single_template; $post = get_queried_object(); if (has_category('dc', $post)) { $single_dg_template = locate_template('single_dg.php'); if (!empty ($single_dg_template)) $new_template = $single_dg_template; } elseif (has_category('dmm', $post) || has_category('duga', $post)) { $single_ckd_template = locate_template('single_ckd.php'); if (!empty ($single_ckd_template)) $new_template = $single_ckd_template; } return $new_template; } add_filter('single_template', 'single_branch'); |
コメントを投稿する