LoginSignup
8
7

More than 5 years have passed since last update.

ひたすらワードプレスのカスタム投稿使った場合のコードメモっていく

Last updated at Posted at 2017-06-06

もう自作はだいっきらい。特にカスタム投稿。

結構古いコードもあると思います。間違いも多いと思います。寄せ集めです。
実装はご自分の判断責任で。ミスの指摘は本当に感謝します。。。

style.css

style.css
/*
Theme Name: テーマの名前
Author: Naoki Shirahige
Author URI: http://naonosuke.com
Description: 詳細。
*/

index.php

・ヘッダーを表示する

index.php
<?php get_header(); ?>

・フッターを表示する

index.php
<?php get_footer(); ?>

・サイドバーを表示する

index.php
<?php get_sidebar(); ?>

・ループを開始させる。

index.php
<?php
  $wp_query = new WP_Query();
  $param = array(
      'posts_per_page' => '5', //表示件数を指定
      'post_type' => 'notice', //カスタム投稿タイプを指定
      'post_status' => 'publish', //公開のものだけ
      'orderby' => 'ID', //並び替え
      'order' => 'DESC'
  );
  $wp_query->query($param);
  if($wp_query->have_posts()): while($wp_query->have_posts()) : $wp_query->the_post();
?>

・記事の日付を表示したい

index.php
<?php echo get_the_date(); ?>

・NEWを表示したい

index.php
<?php
  $days = 7; //表示させたい期間の日数、この場合公開から7日以内
  $today = date_i18n('U');
  $entry = get_the_time('U');
  $kiji = date('U',($today - $entry)) / 86400 ;
  if( $days > $kiji ){
  echo '<span class="news_icon">New!</span>';
  } 
?>

・カテゴリを表示したい

index.php
<?php
    if ($terms = get_the_terms($post->ID, 'notice-cat')) {
    foreach ( $terms as $term ) {
      echo esc_html($term->name);
    }
    }
?>

・記事タイトルを表示したい

index.php
<?php the_title(); ?>

・忘れずループ終了

index.php
<?php endwhile; endif; ?>

header.php

・ホームリンクを表示したい

header.php
<?php echo esc_url( home_url( '/' ) ); ?>
header.php
<?php echo esc_url( get_home_url() ); ?>

・hrefでひっぱってきたい

header.php
<?php echo get_template_directory_uri(); ?>/〜.css

single.php

・パンくずリストを表示したい

single.php
<?php echo esc_html(get_post_type_object(get_post_type())->label ); ?>
> //これがパンくずの「>」
<?php
if ($terms = get_the_terms($post->ID, 'cat')) {
  foreach ( $terms as $term ) {
    echo esc_html($term->name);
  }
}
?>

・ループ開始をする

single.php
<?php if(have_posts()): while(have_posts()): the_post(); ?>

・記事タイトルを表示したい

single.php
<?php the_title(); ?>

・ポストタイプ(カスタム投稿タイプがある場合)を表示したい

single.php
<?php echo esc_html(get_post_type_object(get_post_type())->label ); ?> >

・記事の日付を表示したい

single.php
<?php echo get_the_date(); ?>

・??

single.php
<?php
  if ($terms = get_the_terms($post->ID, 'notice-cat')) {
    foreach ( $terms as $term ) {
      echo esc_html($term->name);
    }
  }
  ?>

・サムネイルを表示したい

single.php
<?php the_post_thumbnail(); ?>

・記事の内容を表示したい

single.php
<?php the_content(); ?>

・ループ終了

single.php
<?php endwhile; endif; ?>

functions.php

functions.php
add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'notice', //noticeがカスタムの名前
        array(
            'labels' => array(
            'name' => __( 'お知らせ' ), //表示する名前
            'singular_name' => __( 'お知らせ' ) 
            ),
            'public' => true, //公開するか
            'menu_position' =>5, //場所の順位
            'supports' => array('title','editor','thumbnail') //何を有効にするか
        )
    );
    register_taxonomy( //カスタムタクソノミー、タグタイプ
        'notice-cat', //(名前)-cat 
        'notice', //カテゴリの名前
        array(
          'hierarchical' => true, 
          'update_count_callback' => '_update_post_term_count',
          'label' => 'お知らせのカテゴリー', //表示する名前
          'singular_label' => 'お知らせのカテゴリー', 
          'public' => true,
          'show_ui' => true
        )
    );
    register_taxonomy( //タグ
        'notice-tag', //(名前)-tag
        'notice', //カテゴリの名前
        array(
            'hierarchical' => false, 
            'update_count_callback' => '_update_post_term_count',
            'label' => 'お知らせのタグ', //表示する名前
            'singular_label' => 'お知らせのタグ',
            'public' => true,
            'show_ui' => true
        )
    );
  //続ける場合はそのまま下に追加
);
}

・サムネイルを表示できるようにする

functions.php
add_theme_support('post-thumbnails');
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7