LoginSignup
0
0

More than 5 years have passed since last update.

[WordPress] ターム(カテゴリー・タグ)アーカイブで記事が指定件数以下なら404扱いにしたい

Last updated at Posted at 2018-01-26

タイトルそのまま。
template_redirect アクションフック使うだけ。
タームにはカテゴリー、タグも含まれるので条件分岐はよしなに。

via. https://increment-log.com/access-redirect-404/

コード

tag_archive_template_redirect.php
<?php
add_action( 'template_redirect', 'tag_archive_template_redirect' );
function tag_archive_template_redirect() {

    if ( ! is_tag() ) { // タグアーカイブ以外は何もしない
        return;
    }

    $queried = get_queried_object();
    if ( $queried->count < 6 ) { // 総記事数が5件以下なら
        global $wp_query;
        $wp_query->set_404();
        status_header( 404 );
    }
}

現場からは以上です。

0
0
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
0
0