テーマファイルのベーステンプレート

ただ投稿本文を出力するだけの基本系

<?php get_header(); ?>
<h2><?php the_title(); ?></h2>
<article>
<?php
if (have_posts()) :
    while (have_posts()) : the_post();
        the_content();
    endwhile;
endif;
?>
</article>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

パーツ分割呼び出し系

パーツに分けたファイルの呼び出し

<?php get_template_part('inc/content-flow'); ?>

一覧ページ

サムネイル表示

//画像を縦横150pxにリサイズする場合
<?php the_post_thumbnail(array(150,150)); ?>

//管理画面で設定したサイズを使う場合
//サムネイル
<?php the_post_thumbnail('thumbnail'); ?>

//中サイズ
<?php the_post_thumbnail('medium'); ?>

//大サイズ
<?php the_post_thumbnail('large'); ?>

//リサイズなしサイズ
<?php the_post_thumbnail('full'); ?>

カテゴリ名をラベルとして表示させる

<div class="rep_item_img">
<span class="rep_item_label <?php echo $cat[0]->slug; ?>"><?php echo $cat[0]->name; ?></span>
<?php the_post_thumbnail('medium', array('class'=>"rep_pic")); ?>
</div>

ページャー

<!-- ページャー -->
<div id="next">
<span class="left"><?php previous_posts_link(__('< 前のページ')); ?></span>
<span class="right"><?php next_posts_link(__('次のページ >')); ?></span>
<div class="clear"></div>
</div>
<!-- / ページャー -->

ページネーション

<div class="pagination">
    <?php global $wp_rewrite;
    $paginate_base = get_pagenum_link(1);
    if(strpos($paginate_base, '?') || ! $wp_rewrite->using_permalinks()){
        $paginate_format = '';
        $paginate_base = add_query_arg('paged','%#%');
    }
    else{
        $paginate_format = (substr($paginate_base,-1,1) == '/' ? '' : '/') .
        user_trailingslashit('page/%#%/','paged');;
        $paginate_base .= '%_%';
    }
    echo paginate_links(array(
        'base' => $paginate_base,
        'format' => $paginate_format,
        'total' => $wp_query->max_num_pages,
        'mid_size' => 4,
        'current' => ($paged ? $paged : 1),
        'prev_text' => '« 前へ',
        'next_text' => '次へ »',
    )); ?>
</div>

ページネーションその2

“`php:functions.php
function pagination($pages = '', $range = 2)
{
$showitems = ($range * 2)+1;//表示するページ数(5ページを表示)

<pre><code>global $paged;//現在のページ値
if(empty($paged)) $paged = 1;//デフォルトのページ

if($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;//全ページ数を取得
if(!$pages) {//全ページ数が空の場合は、1とする
$pages = 1;
}
}

if(1 != $pages) {//全ページが1でない場合はページネーションを表示する
echo '<ul class="pagination">'.PHP_EOL;
//Prev:現在のページ値が1より大きい場合は表示
if($paged > 1) echo "<li class=\"prev\"><a href='".get_pagenum_link($paged – 1)."'>Prev</a></li>\n";

for ($i=1; $i <= $pages; $i++) {
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
echo ($paged == $i)? '<li class="active"><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>'.PHP_EOL : "<li><a href='".get_pagenum_link($i)."'>".$i."</a></li>\n";
}
}
//Next:総ページ数より現在のページ値が小さい場合は表示
if ($paged < $pages) echo "<li class=\"next\"><a href=\"".get_pagenum_link($paged + 1)."\">Next</a></li>\n";
echo '</ul>'.PHP_EOL;
}
</code></pre>

}

<

pre><code><br />
<span style="font-size: inherit; letter-spacing: 0.05em;">“`

php:呼び出し.php
<nav class="text-center"><?php if (function_exists("pagination")) pagination($additional_loop->max_num_pages); ?></div>

詳細ページ

よくある表示

$title = esc_html(get_the_title()); //投稿タイトル
$url = esc_url(get_permalink()); //投稿url
$date = get_the_time(‘Y/m/d’); //投稿日
$modify = get_the_modified_date(‘Y/m/d’); //更新日
$category = get_the_category(); //カテゴリ
$tags = get_the_tags(); //タグ
$content = mb_substr(esc_html(get_the_content()),0,60).”…”; //本文、一部
$excerpt = get_the_excerpt(); // 抜粋

//アイキャッチ画像のurlを取得しておく
if(has_post_thumbnail()):
$src = get_thumb_src(‘サイズ’);
else:
$src = “デフォルト画像へのパス”;
endif;

カテゴリ名表示

<?php $cat = get_the_category();
$cat = $cat[0];
echo get_cat_name($cat->term_id); ?>

カスタムフィールドの項目出力

<?php echo post_custom('place'); ?>

<!-- 画像 -->
<img src="<?php echo wp_get_attachment_url(get_post_meta($post->ID,"img1",true));?>"  width="145" alt="写真1" />

サイドバー

新着記事一覧

<ul>
<?php query_posts("showposts=5&post_type=blog"); ?>
<?php if(have_posts()): while(have_posts()): the_post(); ?>
<li class="clearfix">
    <div class="sidebox-thumb">
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
    </div>
    <div class="sidebox-contents">
        <div class="sidebox-day"><?php the_time('n月j日'); ?></div>
        <div class="sidebox-title">
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <?php the_title(); ?></a>
        </div>
    </div>
</li>
<?php endwhile; else: ?>
<li>記事はありません</li>
<?php endif; ?>
</ul>

※一覧系のquery_postsは良くないらしいので、get_postsを使う(通常のPHPと同じ感じでいけるので良い)

<?php
    $post_args = array(
        'category'  => $category->cat_ID
       ,'orderby'   => 'title'
       ,'order'     => 'ASC'
       ,'post_type' => 'post'
    );
    $shohin_posts = get_posts( $post_args );
    foreach($shohin_posts as $post) :
        setup_postdata( $post );
    ?>
        <h4><?php the_title(); ?>"></h4>
        <?php the_contents(); ?>
<?php
    endforeach; 
    wp_reset_postdata();
?>

参考サイト:http://qiita.com/TongChang/items/46903365eb5903f7211f
パラメタについて:http://elearn.jp/wpman/function/get_posts.html

アーカイブリンク、カテゴリー表示

<div id="sidebox">
    <div class="widget widget_archive">
    <div class="sidebar-title">アーカイブ</div>
        <ul>
        <?php wp_get_archives('type=monthly&post_type=blog'); ?>
        </ul>
    </div>
    <div class="widget widget_archive">
    <div class="sidebar-title">カテゴリー</div>  
        <ul>
        <?php wp_list_categories('title_li=&taxonomy=blog-cat'); ?>
        </ul>
    </div>
</div>

※アーカイブを年別に出力場合の注意点&カレントの年のaタグにclassを付与したい場合

wp_get_archives('type=yearly&after=年');
// 上記だけだと「年」がaタグの後ろに出力されてしまう
// また、カレントの年のaタグにclassを付与したい場合
// functions.phpに下記を追加
function my_archives_link($link_html){
 if(preg_match_all('@<a .*>([0-9]+)</a>@i', $link_html, $matches))
    if(get_query_var('year') == $matches[1][0])
      $link_html = preg_replace('@<a(.*)>@i', '<a class="active" $1>', $link_html);

  return preg_replace('@</a>(.+?)</li>@', '\1</a></li>', $link_html);
}
add_filter('get_archives_link', 'my_archives_link');

“`php

<

pre><code><br />“`php

その他よく使う関数

出力系

<?php the_permalink(); ?>
<?php the_time(__('Y-m-d')) ?>
<?php the_excerpt(); ?>
<?php the_content(); ?>
<?php echo mb_substr($post->post_title, 0, 40); ?>
<?php bloginfo('description'); ?>
<?php wp_title('|', true, 'right'); ?>

パス取得系

<?php echo get_template_directory_uri(); ?>
<?php echo home_url(); ?>
a要素のhref属性の書き方
<a href="<?php echo home_url(); ?>">
また、固定ページへのリンクは以下のように書きます。
<a href="<?php echo home_url(); ?>/スラッグ名">
画像のパスの書き方
<img src="<?php echo get_template_directory_uri(); ?>/images/head

条件判定系

        if ( is_category() ) { // If this is a category archive
            printf("<h4>".__("  投稿がありません").'</h4>', single_cat_title('',false));
        } else if ( is_date() ) { // If this is a date archive
            echo('<h4>'.__("Sorry, but there aren't any posts with this date.").'</h4>');
        } else if ( is_author() ) { // If this is a category archive
            $userdata = get_userdatabylogin(get_query_var('author_name'));
            printf("<h4>".__("Sorry, but there aren't any posts by %s yet.")."</h4>", $userdata->display_name);
        } else {
            echo("<h4>".__('No posts found.').'</h4>');
        }
タイトルとURLをコピーしました