LoginSignup
9
4

More than 5 years have passed since last update.

先頭に固定表示をトップページ以外で利用する

Last updated at Posted at 2018-02-25
<div class="subLoop">
    <ul class="list vertical">
        <?php
        $list_count = 5; // 取得したい件数を指定する
        // まずは先頭固定記事のみを取得する
        $q_parm = array(
            'category_name'  => 'news',
            'post_type'      => 'post',
            'posts_per_page' => $list_count,
            'post__in'       => get_option( 'sticky_posts' )
        );
        // global変数wp_queryを上書きする(必要がある場合はべつ変数に待避しておく)
        $the_query = new WP_Query( $q_parm );
        ?>
        <?php
        while ( $wp_query->have_posts() ):
            $wp_query->the_post();
        ?>
        <li class="pickUp">
            <div class="thumbnail">
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('200_thumbnail'); ?>
                </a>
            </div>
            <div class="textBody">
                <span class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
                <span class="upDate">
                    <?php the_time(Y年n月j日); ?>
                </span>
                <span class="description">
                    <?php the_excerpt(); ?>
                </span>
            </div>
        </li>
        <?php endwhile; ?>
        <?php
        // 続いて先頭固定記事以外を取得する
        $q_parm = array(
            'category_name'  => 'news',
            'post_type'      => 'post',
            'posts_per_page' => $list_count,
            'post__not_in'   => get_option( 'sticky_posts' )
        );
        // global変数wp_queryを上書きする(必要がある場合はべつ変数に待避しておく)
        $wp_query = new WP_Query( $q_parm );
        ?>
        <?php
        while ( $wp_query->have_posts() ):
            $wp_query->the_post();
        ?>
        <li>
            <div class="thumbnail">
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('200_thumbnail'); ?>
                </a>
            </div>
            <div class="textBody">
                <span class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
                <span class="upDate">
                    <?php the_time(Y年n月j日); ?>
                </span>
                <span class="description">
                    <?php the_excerpt(); ?>
                </span>
            </div>
        </li>
        <?php endwhile; wp_reset_postdata(); ?>
    </ul>
</div>

参考
サイドバーなどの記事一覧で先頭固定を有効にする

9
4
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
9
4