この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:4
ページ更新者:guest
更新日時:2026-06-11 07:07:02

タイトル: 独自の関数一覧
SEOタイトル: WordPress 主要独自関数完全ガイド (Loop / Enqueue / Hook / Conditional Tag)

この記事の要点
  • ループ系: have_posts() / the_post() / get_the_title() / the_content()
  • クエリ: WP_Query / get_posts() / wp_query グローバル
  • アセット: wp_enqueue_script() / wp_enqueue_style()(直書きせず必ずこれを使う)
  • フック: add_action("init", fn) / add_filter("the_content", fn)
  • 条件分岐タグ: is_home() / is_single() / is_page() / is_user_logged_in()

1. ループ系関数

', '' );      // タイトル出力
        the_content();                      // 本文出力
        the_excerpt();                      // 抜粋
        the_permalink();                    // パーマリンク URL
        the_post_thumbnail( 'medium' );     // アイキャッチ
        the_author();                       // 著者
        the_date();                         // 投稿日
    endwhile;
endif;

// 値取得版 (get_ 接頭辞: echo せず return)
$title = get_the_title();
$content = get_the_content();
$url = get_permalink();
$thumb = get_the_post_thumbnail_url();

2. クエリ関数

 'post',
    'posts_per_page' => 10,
    'category_name'  => 'news',
    'meta_query'     => [
        ['key' => 'featured', 'value' => '1'],
    ],
    'tax_query'      => [
        [
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => 'shoes',
        ],
    ],
    'orderby'        => 'date',
    'order'          => 'DESC',
]);

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
        the_title();
    endwhile;
    wp_reset_postdata();   // ★ 必須: グローバル $post を元に戻す
endif;

// 軽量版: 単純な投稿配列を取得
$posts = get_posts([
    'numberposts' => 5,
    'category'    => 1,
]);
foreach ( $posts as $p ) {
    echo $p->post_title;
}

3. アセット読み込み (Enqueue)

functions.php や プラグインで CSS/JS を読み込む場合、必ず Enqueue 関数を使います。直接