WP 備忘録 01

タイトル
<?php the_title(); ?>

コンテンツ
<?php the_content(); ?>

マーマリンク
<?php the_permalink();?>

ホームリンク
<?php echo home_url(); ?>

テーマディレクトリリンク
<?php bloginfo('template_url') ?>

テンプレートリンク
<?php get_template_part('module/header'); ?>

ループ例1
<?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>
<?php get_template_part('template/ZZZZZZ'); ?>
<?php endwhile; endif; ?>

ループ例2
<?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>
<?php if(get_field('XXX')): ?><?php the_field('XXX'); ?><?php endif; ?>
<time class="date"><?php the_time('Y.m.d') ?></time>
<?php echo get_the_term_list($post->ID,'information_cat'); ?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>

記事に付与されたカテゴリを表示
<?php $categories = get_the_category(); if ( $categories ) { foreach ( $categories as $category ) {echo '<span>'.$category->name.'</span>';}} ?>

記事に付与されたタグを表示
<?php the_tags('<ul class="tag"><li>','</li><li>','</li></ul>'); ?>

ACF基本
<?php if(get_field('XXX')): ?><?php the_field('XXX'); ?><?php endif; ?>

カスタム投稿タイプループ
<?php $YYYY_posts = get_posts('post_type=xxxx&posts_per_page=xx'); if ( !empty($YYYY_posts) ): ?>
<?php foreach ( $YYYY_posts as $post ): setup_postdata($post); ?>
<?php get_template_part('template/ZZZZZZ'); ?>
<?php endforeach; wp_reset_postdata(); ?>
<?php endif; ?>

投稿に付与されたタクソノミー(ターム)を出力
<?php
$terms = get_the_terms($post->ID, 'blog_cat');
if ( $terms ) {
foreach ( $terms as $term ) {
echo '<span>['.$term->name.']</span>';
}
}
?>

タクソノミーループ
<?php $terms = get_terms('information_cat');
foreach ($terms as $term ) {
$des_list .= '<a class="cat '. $term-> slug .'" href="' . get_term_link( $term ) . '">';
$des_list .= $term->name . '</a>';
}
echo $des_list; ?>

タクソノミーページ(taxonomy.php)でタームスラッグをclassに入れる
<bodu class="<?php echo $term ?>">

分岐例
<?php if(is_post_type_archive('XXX') || is_single('XXX') || is_tax('XXX_cat')) : ?>
<?php elseif(): ?>
<?php else: ?>
<?php endif; ?>

投稿者別ループ
<?php
$post_author_id = get_post_field( 'post_author', $post_id );
$postID = get_the_ID();
$post_time = get_the_time('U'); $days = 7; $last = time() - ($days * 24 * 60 * 60);
$args = array( 'exclude' => $postID, 'author' => $post_author_id, 'posts_per_page' => -1, 'post_type' => 'information', 'showposts' => 3);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>