WordPress自定義分類PHP開發

類別提供了一種將相關博客文章組合在一起的有用方法。您可能想為博客中涵蓋的不同主題創建類別。您可以使用類別將您的博客文章分類和分組到不同的部分。例如,如果您有涵蓋特定音樂類型的文章,您可能希望將這些文章歸為一個類別。

類別不僅有助於使帖子井井有條,而且您還可以使用類別在您網站的多個位置顯示帖子,幫助訪問者快速了解您網站的主題,並允許他們更快地瀏覽您的網站。類似於 WordPress 中標籤的使用,人們通過類別搜索來查找相似的主題和主題。

您可以有父類別和子類別,使帖子的層次結構成為可能。此外,一篇文章可以放在多個類別中。這為您提供了很大的靈活性,可以準確顯示您想要的帖子,完全按照您希望的方式在小部件、菜單中或直接在您的 WordPress 主題中顯示。

在帖子中顯示類別

可以通過某些方式顯示類別。其中之一在帖子中。當您要閱讀 WordPress 帖子時,您會看到該帖子屬於某些類別(一個或多個)。要在 WordPress 的帖子循環中顯示這些類別,您可以使用以下代碼。

while ( have_posts() ) : the_post(); . . $categories = get_the_category(); foreach($categories as $category){ echo $category->name . wp_get_list_item_separator(); } . .endwhile;

輸出

Category 1,Category 2,Category 3,

get_the_category  () 內置的 WordPress 函數返回 WP_Term,它是類別的 term_id、名稱、slug、描述等詳細信息列表。

  • term_id
  • 名稱
  • 鼻涕蟲
  • 術語組
  • term_taxonomy_id
  • 分類
  • 類別
  • 描述
  • 父母
  • 數數
  • 篩選
  • 貓ID
  • 類別計數
  • 類別_描述
  • 貓名
  • category_nicename
  • 類別_父母

如果你想在循環外使用這個函數,你必須將帖子 ID 作為參數傳遞。

get_the_category($post_id );

在此示例中,您必須自己控制分隔符。另一件事是類別名稱沒有鏈接且不可單擊,最好改用 get_the_category_list() 函數。

在 WordPress 的循環中還有另一種簡潔方便的方式來顯示帖子類別。get_the_category_list() 函數顯示帶有額外選項的類別。

while ( have_posts() ) : the_post(); . . $categories_list = get_the_category_list( wp_get_list_item_separator() ); if ( $categories_list ) { printf( /* translators: %s: List of categories. */ '<span>' . esc_html__( 'Categorized as %s', 'textdomain' ) . ' </span>', $categories_list // phpcs:ignore WordPress.Security.EscapeOutput ); } . .endwhile;

輸出

Categorized as Category 1,Category 2,Category 3

在此示例中,您可以顯示 WordPress 類別列表名稱,並且名稱鏈接到類別列表顯示部分。此外,您可以使用此方法顯示 WordPress 自定義帖子類型類別。

如何在 WordPress 中創建類別模板存檔頁面

對於 WordPress 網站,通常為類別、標籤、自定義帖子類型和分類法使用不同的模板存檔頁面。通過為類別創建模板,您可以將特定功能添加到類別存檔頁面。

例如,您可以允許用戶訂閱類別、添加類別圖像、顯示類別描述以及為每個類別選擇不同的布局。

要創建和設計 WordPress 類別存檔模板,您需要為該特定類別創建一個頁面模板。轉到主題的文件夾。從主題文件列表中,打開 category.php 文件,如果那裡沒有 category.php 文件,則創建一個。

對於類別,層次結構相當簡單。例如,假設類別的 slug 是 新聞 ,類別 ID 是 6模板層次結構指定 WordPress 將使用它在當前主題目錄中從以下列表中找到的第一個模板文件:

  1. 類別 slug.php
  2. 類別ID.php
  3. 類別.php
  4. 歸檔.php
  5. 索引.php

也就是說,如果您沒有 category-slug.php(假設是 category-news.php),WordPress 將檢查 category-ID.php(如 category-6.php),依此類推。

現在打開 category-news.php 文件並向其中添加以下代碼。

<?phpget_header();$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );?><div> <div> <h1><?php echo apply_filters( 'the_title', $term->name ); ?> News</h1> <?php if ( !empty( $term->description ) ): ?> <div> <?php echo esc_html($term->description); ?> </div> <?php endif; ?> <?php if ( have_posts() ): while ( have_posts() ): the_post(); ?> <div <?php post_class('post'); ?>> <h2><a href="<?php%20the_permalink();%20?>"><?php the_title(); ?></a></h2> <div> <p><?php the_time(get_option('date_format')); ?> by <?php the_author_posts_link(); ?></p> </div> <div> <?php the_content( __('Full story…') ); ?> </div> </div> <?php endwhile; ?> <?php else: ?> <h2>No Post Type in <?php echo apply_filters( 'the_title', $term->name ); ?></h2> <div> <div> <p>It seems there isn't anything happening in <strong><?php echo apply_filters( 'the_title', $term->name ); ?></strong> right now. Check back later, something is bound to happen soon.</p> </div> </div> <?php endif; ?> </div><?php get_footer(); ?>

這是用於顯示類別帖子列表和存檔的 WordPress 類別模板示例。

如何以編程方式創建 WordPress 類別

如果你想通過代碼在 WordPress 中創建一個類別,你可以簡單地使用 wp_insert_term() 函數,如下所示。

wp_insert_term( // the name of the category 'Category A', // the taxonomy, which in this case if category (don't change) 'category', array( 'slug' => 'category-a', ));

如果您想在 WordPress 中為您的自定義帖子類型創建一個類別,只需將「類別」更改為您的帖子類型類別 slug。在這種情況下是「hscategory」。

wp_insert_term( // the name of the category 'Category A', // the taxonomy, which in this case if category (don't change) 'hscategory', array( 'slug' => 'category-a', ));

要為父類別創建子類別,您需要稍微更改代碼。

wp_insert_term( // the name of the sub-category 'Sub-category 1', // the taxonomy 'category' (don't change) 'category', array( 'slug' => 'sub-cat-1', // link with main category. In the case, become a child of the "Category A" parent 'parent'=> term_exists( 'Category A', 'category' )['term_id'] ));

對於自定義帖子類型類別

wp_insert_term( // the name of the sub-category 'Sub-category 1', // the taxonomy 'category' (don't change) 'hscategory', array( 'slug' => 'sub-cat-1', // link with main category. In the case, become a child of the "Category A" parent 'parent'=> term_exists( 'Category A', 'hscategory' )['term_id'] ));

如何在 WordPress 中以編程方式刪除類別

要按代碼刪除類別,請使用以下代碼。

$categ_ID = 204;if ( wp_delete_category( $categ_ID ) ) { echo "Category #$categ_ID was successfully deleted";} else { echo "Category #$categ_ID deletion failed";}

wp_delete_category() WordPress 內置函數用於刪除類別。

通過is_category()函數判斷頁面是否為分類頁面

is_category();// When any Category archive page is being displayed. is_category( '204' );// When the archive page for Category 204 is being displayed.is_category( 'Category One' );// When the archive page for the Category with Name "Category One" is being displayed.is_category( 'category-slug' );// When the archive page for the Category with Category Slug "category-slug" is being displayed.is_category( array( 204, 'category-slug', 'Category One' ) );// Returns true when the category of posts being displayed is either term_ID 204,// or slug "category-slug", or name "Category One".// Note: the array ability was added in version 2.5.

相關文章