サポート » 使い方全般 » 月別アーカイブで特定のカテゴリーを非表示

  • 教えて下さい。

    月別アーカイブで特定のカテゴリーのポストを非表示にしたいのですが、上手くいきません。

    知識がないので、<?php query_posts(‘cat= を使ってみたりネットで探していろいろ試したのですが・・・上手くできませんでした。宜しくお願い致します。

    <?php if (have_posts()) : ?>
    <header class=”page-header”>
    <?php
    the_archive_title( ‘<h1 class=”page-title”>’, ‘</h1>’ );
    the_archive_description( ‘<div class=”taxonomy-description”>’, ‘</div>’ );
    ?>
    </header><!– .page-header –>
    <?php while ( have_posts() ) : the_post(); ?>
    <article id=”post-<?php the_ID(); ?>” <?php post_class(); ?>>
    <header class=”entry-header”>
    <?php if ( has_post_thumbnail() ) : ?>
    <div class=”post-thumb”>
    <?php the_post_thumbnail(); ?>
    </div><!– .post-thumb –>
    <?php endif; ?>
    <div class=”entry-meta clearfix”>
    <div class=”pull-left”>
    <span class=”entry-cats”><?php the_category(‘<span class=”comma”> / </span>’); ?></span><!– .entry-cats –>
    </div><!– .pull-left –>
    <div class=”pull-right”>
    <span class=”entry-comments”>
    <?php comments_popup_link( esc_html__( ‘0’, ‘tdmacro’ ), esc_html__( ‘1’, ‘tdmacro’ ), esc_html__( ‘%’, ‘tdmacro’ ) ); ?>
    </span><!– .entry-comments –>
    </div><!– .pull-right –>
    </div><!– .entry-meta –>
    <?php the_title( ‘<h1 class=”entry-title”>’, ‘</h1>’ ); ?>
    </header><!– .entry-header –>
    <div class=”entry-content”>
    <?php
    the_content();
    wp_link_pages( array(
    ‘before’ => ‘<div class=”page-links”><span class=”page-links-title”>’ . esc_html__( ‘Pages:’, ‘tdmacro’ ) . ‘</span>’,
    ‘after’ => ‘</div>’,
    ‘link_before’ => ‘<span>’,
    ‘link_after’ => ‘</span>’
    ) );
    ?>
    </div><!– .entry-content –>
    <?php the_tags( ‘<div class=”entry-tags”>’, ‘ ‘, ‘</div><!– .entry-tags –>’ ); ?>
    <?php get_template_part( ‘template-parts/author’, ‘section’ ); ?>
    </article><!– #post-## –>
    <?php endwhile; ?>
    <?php endif; ?>

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • query_posts のパラメータに category__not_in というのがあります、これで指定のカテゴリーに属さない投稿のみとなります。
    なお、query_posts は使うべきではないとされています。代わりに get_posts 等の使用を検討してください。

    テンプレートタグ/query posts – WordPress Codex 日本語版

    別のアプローチとして、pre_get_posts アクション フックを使用する方法はどうでしょうか?

    function my_pre_get_posts( $query ) {
        if ( !is_admin() && $query->is_main_query() ) {
            if ( $query->is_month() ) { // 月別アーカイブか?
                $query->set( 'category__not_in', array( 123 ) ); // 除外するカテゴリー ID の配列
            }
        }
    }
    add_action( 'pre_get_posts', 'my_pre_get_posts' );
1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「月別アーカイブで特定のカテゴリーを非表示」には新たに返信することはできません。