<ul>
<?php
$terms = get_terms('cat_recruit');
foreach ($terms as $term) {
echo '<li><a href="#' . esc_html($term->slug) . '">' . esc_html($term->name) . '</a></li>';
}
?>
</ul>
<?php
// タクソノミ取得
$catargs = array(
'taxonomy' => 'cat_recruit',
'order' => 'DESC'
);
$catlists = get_categories($catargs);
foreach ($catlists as $cat) : // 取得したカテゴリの配列でループを回す
?>
<section>
<h2 class="ttl" id="<?php echo $cat->slug; ?>"><?php echo $cat->name; ?></h2>
<?php
$args = array(
'post_type' => 'recruit',
'cat_recruit' => $cat->slug,
'posts_per_page' => -1,
);
$my_posts = get_posts($args);
if ($my_posts) { // 該当する投稿があったら
foreach ($my_posts as $post) :
setup_postdata($post);
?>
<table>
<tr>
<th>職種</th>
<td><?php the_title(); ?></td>
</tr>
</table>
<?php
endforeach;
} else {
echo '投稿はありません';
}
wp_reset_postdata();
?>
</section>
<?php endforeach; ?>
目次
カスタムタクソノミーごとのまとまりへ、アンカーリンクするためのナビゲーション
<ul>
<?php
$terms = get_terms('cat_recruit');
foreach ($terms as $term) {
echo '<li><a href="#' . esc_html($term->slug) . '">' . esc_html($term->name) . '</a></li>';
}
?>
</ul>
cat_recruitタクソノミーについて、
<li><a href="#★タームスラッグ★'">★ターム名★</a></li>
というコードを出力させる書き方。
カスタムタクソノミーごとに一覧表示する
<?php
// タクソノミ取得
$catargs = array(
'taxonomy' => 'cat_recruit',
'order' => 'DESC'
);
$catlists = get_categories($catargs);
foreach ($catlists as $cat) : // 取得したカテゴリの配列でループを回す
?>
<section>
<h2 class="ttl" id="<?php echo $cat->slug; ?>"><?php echo $cat->name; ?></h2>
<?php
$args = array(
'post_type' => 'recruit',
'cat_recruit' => $cat->slug,
'posts_per_page' => -1,
);
$my_posts = get_posts($args);
if ($my_posts) { // 該当する投稿があったら
foreach ($my_posts as $post) :
setup_postdata($post);
?>
<table>
<tr>
<th>職種</th>
<td><?php the_title(); ?></td>
</tr>
</table>
<?php
endforeach;
} else {
echo '投稿はありません';
}
wp_reset_postdata();
?>
</section>
<?php endforeach; ?>
recruitという投稿タイプについて、
cat_recruitのタクソノミーごとに
<section> <h2 class="ttl" id="★カテゴリスラッグ★">★カテゴリ名★</h2> <table> <tr><th>職種</th><td>★記事タイトル★</td></tr> </table> <table> <tr><th>職種</th><td>★記事タイトル★</td></tr> </table> </section>
というコードを出力させる書き方。
コメント
コメント一覧 (1件)
[…] https://deep-space.blue/web/1227 […]