BLOG

ブログ

  • Web制作

WordPress のコーポレートサイト制作でよく使うコードまとめ

WordPress でコーポレートサイトを制作する際によく使うコードをまとめました。

必要なタグやトップページに新着記事を表示させたい場合、施工事例などカスタム投稿を表示させたい場合などの書き方です。

随時更新していきます。

<!--:::::::::::::::::::: テーマテンプレートファイル内の </head> タグ直前に挿入 ::::::::::::::::::::-->
<?php wp_head(); ?>


<!--:::::::::::::::::::: テーマテンプレートファイル内の </body> タグ直前に挿入 ::::::::::::::::::::-->
<?php wp_footer(); ?>


<!--:::::::::::::::::::: タイトルタグ ::::::::::::::::::::-->
<title><?php wp_title('|', true, 'right'); ?></title>


<!--:::::::::::::::::::: テーマのディレクトリのパス ::::::::::::::::::::-->
<?php echo get_template_directory_uri(); ?>/


<!--::::::::::::::::::::トップページへのリンク ::::::::::::::::::::-->
<a href="<?php echo home_url(); ?>/"></a>


<!--:::::::::::::::::::: ヘッダーを読み込む ::::::::::::::::::::-->
<?php get_header(); ?>


<!--:::::::::::::::::::: サイドバーを読み込む ::::::::::::::::::::-->
<?php get_sidebar(); ?>


<!--:::::::::::::::::::: フッターを読み込む ::::::::::::::::::::-->
<?php get_footer(); ?>


<!--:::::::::::::::::::: "inc"フォルダの"part.php"を読み込む ::::::::::::::::::::-->
<?php get_template_part('inc/part'); ?>




<!--:::::::::::::::::::: トップページで新着の投稿を表示 ::::::::::::::::::::-->
        <ul class="headlineList">
            <?php
            $args = array(
                'posts_per_page' => 3
            );
            $the_query = new WP_Query($args);
            if ($the_query->have_posts()) :
                while ($the_query->have_posts()) : $the_query->the_post();
            ?>
                    <li>
                        <a href="<?php the_permalink() ?>">
                            <time><?php the_time('Y.m.d') ?></time>
                            <?php $cat = get_the_category();
                            $cat = $cat[0]; {
                                echo '<div class="cat ' . $cat->category_nicename . '">';
                            } ?><?php $cat = get_the_category();
                                $cat = $cat[0]; {
                                    echo $cat->cat_name;
                                } ?>
    </div>
    <div class="headline"><?php the_title(); ?></div>
    <i class="fa fa-angle-right" aria-hidden="true"></i>
    </a>
    </li>
<?php endwhile;
            endif; ?>
<?php wp_reset_postdata(); ?>
</ul>



<!--:::::::::::::::::::: トップページでカスタム投稿「work」の新着記事を4件表示 ::::::::::::::::::::-->
<ul class="worksList">
    <?php
    $args = array(
        'post_type' => 'work',
        'posts_per_page' => 4,
        'tax_query' => array(
            array(
                'taxonomy' => 'work_cat',// タクソノミーの指定(必要なら)
                'field' => 'slug',
                'terms' => 'work_cat-buildings'// タームの指定(必要なら)
            )
        )
    );
    $the_query = new WP_Query($args);
    if ($the_query->have_posts()) :
        while ($the_query->have_posts()) : $the_query->the_post();
    ?>
            <?php
            $after_img = get_field('after_img');
            $text = get_field('text');
            // resize
            $after_imgRS = $after_img['sizes']['worksImageL'];
            ?>
            <li>
                <a href="<?php the_permalink() ?>">
                    <div class="photo">
                        <div class="inner">
                            <img src="<?php echo $after_imgRS; ?>" alt="<?php echo $after_img['alt'] ?>" />
                        </div>
                    </div>
                    <p class="text"><?php the_title(); ?></p>
                    <?php
                    if ($terms = get_the_terms($post->ID, 'work_cat')) {
                        foreach ($terms as $term) {
                            echo '<p class="cat">' . $term->name . '</p>';
                        }
                    }
                    ?>
                </a>
            </li>
    <?php endwhile;
    endif; ?>
    <?php wp_reset_postdata(); ?>
</ul>






<!--:::::::::::::::::::: ページャー ::::::::::::::::::::-->
<?php
if (function_exists('pagination')) {
    $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
    $max_num_pages = $the_query->max_num_pages;
    pagination($max_num_pages);
}
?>


<!--:::::::::::::::::::: アーカイブページのページャー ::::::::::::::::::::-->
<?php
if (function_exists('pagination')) {
    $GLOBALS[]->max_num_pages = $the_query->max_num_pages;
    $max_num_pages = $the_query->max_num_pages;
    pagination($max_num_pages);
}
?>



<!--:::::::::::::::::::: カスタム投稿「blog」のサイドバーメニュー(widgetを使わない) ::::::::::::::::::::-->
<aside class="side-column">
    <div class="widget">
        <h4 class="heading">最新記事</h4>
        <ul>
            <?php $posts = get_posts('post_type=blog&posts_per_page=5'); ?>
            <?php foreach ($posts as $post) : ?>
                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php endforeach; ?>
        </ul>
    </div>

    <div class="widget">
        <h4 class="heading">アーカイブ</h4>
        <ul>
            <?php wp_get_archives('type=monthly&post_type=blog&limit=12'); ?>
        </ul>
    </div>

    <div class="widget">
        <h4 class="heading">カテゴリ-</h4>
        <ul>
            <?php
            $catlist = wp_list_categories(array(
                'taxonomy' => 'blog_cat', // タクソノミーの指定
                'title_li' => '', // リストの外側に表示されるタイトルを非表示
            ));
            echo $catlist; // タームの一覧を表示
            ?>
        </ul>
    </div>
</aside>



<!--:::::::::::::::::::: 適用のテンプレートファイル確認 ::::::::::::::::::::-->
<?php
global $template;
$template_name = basename($template, '.php');
echo $template_name;
?>