🚀 サイト構築が難しい?手取り足取りご案内します——「WordPressサイト構築伴走」サービスを詳しく見る →

WordPressカテゴリーページで記事を固定表示する3つの方法_プラグイン_コード

WordPress記事の固定表示は、記事を投稿する際に「ブログで固定表示()オプションをチェックするだけで実現できることはご存知でしょう。しかし、カテゴリーページでの固定記事を設定し、WPカテゴリーページでのみ固定記事を表示し、ホームページには表示しない方法は?本記事では、2つのWordPressカテゴリーページ記事固定プラグインとコード実装方法を紹介します。wordpress文章分类目录置顶

カテゴリー記事固定プラグイン

ここで紹介する、カテゴリーページで記事を固定表示できるプラグインは2つあります。Naiba サイト構築ノートWordPress 5.3バージョンでテスト済み、正常に動作します。

Category Sticky Post

Category Sticky Postの使用をお勧めしますWordPressプラグインこれにより、記事のカテゴリーページでの固定表示を簡単に実現できます。Category Sticky Postは、各カテゴリーページで指定した記事を固定表示することができます。
  • 固定表示する記事のカテゴリーを選択可能
  • 組み込みの固定機能と同様に、カテゴリーページで記事を固定表示
  • 特定のカテゴリーで異なる固定記事を表示可能
  • WordPress 5.3をサポート
Category Sticky Postプラグインをインストールした後、記事を投稿する際に右サイドバーに「Category Sticky」のオプションが表示されます。固定したいカテゴリーを選択してください。ダウンロードリンク

Sticky Posts – Switch

Sticky Posts – Switchプラグインは比較的新しく、カテゴリーページでスティッキー投稿を表示するだけでなく、タグページやカスタム投稿タイプでも表示をサポートしています。また、このプラグインで固定された記事は、管理画面の投稿一覧で星アイコンが表示され、スティッキー投稿の管理が容易になります。Sticky Posts – Switch同時に、設定でホームページにスティッキー投稿を表示するかどうかを選択できます。ダウンロードリンク

コードでカテゴリー別に固定記事を表示

コード版はNaibaがテストしていません。プラグインを使用したくない方は、コードの実装方法を自分で研究してください。テーマの関数ファイル functions.php に以下のコードを挿入します。挿入方法がわからない場合は、プラグインを使用して参考にすることができます。参照:functions.phpファイルにコードを安全に追加する方法:Code Snippets
add_filter('the_posts',  'putStickyOnTop' );
function putStickyOnTop( $posts ) {
  if(is_home() || !is_main_query() || !is_archive())
    return $posts;
    
  global $wp_query;

  // 获取所有置顶文章
  $sticky_posts = get_option('sticky_posts');
  
  if ( $wp_query->query_vars['paged'] <= 1 && !empty($sticky_posts) && is_array($sticky_posts) && !get_query_var('ignore_sticky_posts') ) {
    $stickies1 = get_posts( array( 'post__in' => $sticky_posts ) );
    foreach ( $stickies1 as $sticky_post1 ) {
      // 判断当前是否分类页 
      if($wp_query->is_category == 1 && !has_category($wp_query->query_vars['cat'], $sticky_post1->ID)) {
        // 去除不属于本分类的置顶文章
        $offset1 = array_search($sticky_post1->ID, $sticky_posts);
        unset( $sticky_posts[$offset1] );
      }
      if($wp_query->is_tag == 1 && !has_tag($wp_query->query_vars['tag'], $sticky_post1->ID)) {
        // 去除不属于本标签的文章
        $offset1 = array_search($sticky_post1->ID, $sticky_posts);
        unset( $sticky_posts[$offset1] );
      }
      if($wp_query->is_year == 1 && date_i18n('Y', strtotime($sticky_post1->post_date))!=$wp_query->query['m']) {
        // 去除不属于本年份的文章
        $offset1 = array_search($sticky_post1->ID, $sticky_posts);
        unset( $sticky_posts[$offset1] );
      }
      if($wp_query->is_month == 1 && date_i18n('Ym', strtotime($sticky_post1->post_date))!=$wp_query->query['m']) {
        // 去除不属于本月份的文章
        $offset1 = array_search($sticky_post1->ID, $sticky_posts);
        unset( $sticky_posts[$offset1] );
      }
      if($wp_query->is_day == 1 && date_i18n('Ymd', strtotime($sticky_post1->post_date))!=$wp_query->query['m']) {
        // 去除不属于本日期的文章
        $offset1 = array_search($sticky_post1->ID, $sticky_posts);
        unset( $sticky_posts[$offset1] );
      }
      if($wp_query->is_author == 1 && $sticky_post1->post_author != $wp_query->query_vars['author']) {
        // 去除不属于本作者的文章
        $offset1 = array_search($sticky_post1->ID, $sticky_posts);
        unset( $sticky_posts[$offset1] );
      }
    }
  
    $num_posts = count($posts);
    $sticky_offset = 0;
    // Loop over posts and relocate stickies to the front.
    for ( $i = 0; $i < $num_posts; $i++ ) {
      if ( in_array($posts[$i]->ID, $sticky_posts) ) {
        $sticky_post = $posts[$i];
        // Remove sticky from current position
        array_splice($posts, $i, 1);
        // Move to front, after other stickies
        array_splice($posts, $sticky_offset, 0, array($sticky_post));
        // Increment the sticky offset. The next sticky will be placed at this offset.
        $sticky_offset++;
        // Remove post from sticky posts array
        $offset = array_search($sticky_post->ID, $sticky_posts);
        unset( $sticky_posts[$offset] );
      }
    }

    // If any posts have been excluded specifically, Ignore those that are sticky.
    if ( !empty($sticky_posts) && !empty($wp_query->query_vars['post__not_in'] ) )
      $sticky_posts = array_diff($sticky_posts, $wp_query->query_vars['post__not_in']);

    // Fetch sticky posts that weren't in the query results
    if ( !empty($sticky_posts) ) {
      $stickies = get_posts( array(
        'post__in' => $sticky_posts,
        'post_type' => $wp_query->query_vars['post_type'],
        'post_status' => 'publish',
        'nopaging' => true
      ) );

      foreach ( $stickies as $sticky_post ) {
        array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );
        $sticky_offset++;
      }
    }
  }
  
  return $posts;
}
コード説明①、アーカイブページでもすべてのスティッキー投稿を表示したい場合は、12-45行目のコードを削除してください。 ②、特定のカテゴリーページでスティッキー投稿を表示したくない場合は、3行目のif(
// abc是分类名称
if ( is_category( 'abc' ) ||
③、特定のタグページでスティッキー投稿を表示したくない場合は、3行目のif(以下のように変更します:
// abc是标签名称
if ( is_tag( 'abc' ) ||
④、特定の著者ページでスティッキー投稿を表示したくない場合は、3行目のif(以下のように変更します:
// abc是作者昵称
if ( is_author( 'abc' ) ||
⑤、上記のコードはメインループにのみ有効です。アーカイブページで WP_Query や query_posts を使用して投稿リストを取得し、そのリストの先頭にスティッキー投稿を表示したい場合は、3行目のコードから以下のコードを削除してください(注意:設定した投稿数と表示数が異なる場合があります):
|| !is_main_query()
コードは露兜博客

🚀 チュートリアルを見てもまだ迷っていますか?私が手取り足取りご案内しましょう

「WordPressサイト構築伴走」——ドメイン選び、ホスティング購入から、テーマのインストール、公開、投稿まで、すべてのステップで私が伴走します。遠回りせず、目標に直行できます。

👉 サイト構築伴走サービスを詳しく見る
🔒

コメントは終了しました

この記事のコメント機能は終了しています。ご質問がある場合は、他の方法でお問い合わせください。

×
二维码

QRコードをスキャンしてフォロー

AIサイト構築アシスタント

🤖
こんにちは!私はNaibaサイト構築ノートのAIアシスタントです。何かお手伝いできることはありますか?
クイックコンサルティング: