在本系列中,我介紹:
- 為附件分配類別和分類法,
- 按分類查詢媒體文件,以便您可以在自定義循環中輸出它們,
- 使用圖像分類查詢將其顯示在存檔頁面上,
- 將圖像添加到類別或分類術語作為類別或術語的“特色圖像”
在第 1 部分中,我演示了如何為附件創建新的分類法。在第 2 部分中,我向您展示了如何為文檔創建自定義模板文件并添加一個循環來顯示每個文檔的媒體文件的鏈接,在第 3 部分中,我為 gallery-category 創建了一個自定義模板文件分類法,將具有給定術語的所有圖像顯示為畫廊樣式的存檔頁面。
在最后一部分中,我將演示一些略有不同的內容:如何為圖像分配類別,然后編輯類別的存檔模板以將該圖像顯示為該類別的“特色圖像”。您也可以使用非常類似的技術對標簽或分類術語執行此操作。
在本教程中,我將創建一個主題,該主題將是二十四歲的子主題。該主題將包括一個函數文件和一個用于類別存檔的自定義模板文件。您可以在代碼包中下載該主題。
您需要什么
要學習本教程,您需要具備以下條件:
- WordPress 的開發安裝
- FTP 訪問(或者 MAMP 或類似的,如果您在本地工作)
- 代碼編輯器
1. 將類別應用于附件
默認情況下,WordPress 不允許您為附件分配類別和標簽,而在本教程中您需要能夠執行此操作。幸運的是,這個問題很容易修復,正如我在之前關于為附件分配類別和標簽的教程中所演示的那樣。
注意:此技術適用于標簽和類別,如果您使用自己的分類法執行此操作,則需要在注冊時將附件指定為分類法適用的帖子類型。您可以按照本系列的第一部分來完成此操作。
在您的主題中,創建一個名為 functions.php 的文件,并向其中添加以下代碼:
<?php // add categories to attachments function wptp_add_categories_to_attachments() { register_taxonomy_for_object_type( 'category', 'attachment' ); } add_action( 'init' , 'wptp_add_categories_to_attachments' ); ?>
這使用 register_taxonomy_for_object_type() 函數向附件添加類別。現在,當您查看媒體庫屏幕時,您將看到類別已啟用。
下一步是添加一些圖像 – 每個類別只需添加一個圖像。您還需要添加另一個名為“精選”的類別,并確保您想要以這種方式使用的每個圖像也屬于該類別。
下面您可以看到一個示例媒體編輯屏幕,其中顯示了類別:



您還可以查看我分配了正確類別的所有圖像:



最后,我將向我的網站添加一些虛擬帖子,并將它們放入相關類別中,以便在我的存檔頁面中顯示一些內容:



2.創建類別模板
下一步是創建自定義類別模板。由于我的主題是二十四的子主題,我將復制該主題的 category.php 文件并將其復制到我的子主題,并對開頭注釋進行一些更改:
<?php /** * The template for displaying Category pages * Custom template which displays a featured image first. * Supports Part 4 of tutorial series on Advanced Use of Images in WordPress for WPTutsplus */ get_header(); ?><section class="content-area" id="primary"><div class="site-content" id="content" role="main"> <?php if ( have_posts() ) : ?><header class="archive-header"><h1 class="archive-title"></h1> <?php // Show an optional term description. $term_description = term_description(); if ( ! empty( $term_description ) ) : printf( '<div class="taxonomy-description">%s</header> </div>', $term_description ); endif; ?> <!-- .archive-header --> <?php // Start the Loop. while ( have_posts() ) : the_post(); /* * Include the post format-specific template for the content. If you want to * use this in a child theme, then include a file called called content-___.php * (where ___ is the post format) and that will be used instead. */ get_template_part( 'content', get_post_format() ); endwhile; // Previous/next page navigation. twentyfourteen_paging_nav(); else : // If no content, include the "No posts found" template. get_template_part( 'content', 'none' ); endif; ?><!-- #content --></section><!-- #primary --><?php get_sidebar( 'content' ); get_sidebar(); get_footer(); ?>
3.向類別模板添加自定義查詢
在主循環上方,使用 WP_Query 添加自定義循環。在結束 標記后插入以下內容:
<?php // display a featured image for the category // identify the current category $currentcat = get_queried_object(); $currentcatname = $currentcat->slug; ?>
使用 get_queried_object() 標識當前顯示的類別。
下面,使用 WP_Query 定義自定義查詢的參數:
<?php // define query arguments for the featured image $args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'category_name' => $currentcatname, ); $query = new WP_Query( $args ); ?>
這標識當前類別以及“特色”類別中的所有附件。請注意,由于 WordPress 設置附件帖子狀態的方式,您需要包含 ‘post_status’ => ‘inherit’ 作為參數。
現在在此下方添加循環:
<?php // The Loop while ( $query->have_posts() ) : $query->the_post(); // define attributes for image display $imgattr = array( 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), ); // output the image ?> <div class="category-image"></div> <?php endwhile; // reset the query so the default query can be run wp_reset_postdata(); ?>
確保您不要錯過最后的 wp_reset_postdata() ,否則類別存檔的主查詢將無法工作。
添加完所有這些后,保存您的類別模板并查看您的類別存檔頁面之一。它應該類似于本教程開頭的屏幕截圖。
摘要
在這個由四個教程組成的系列中,我演示了一些在 WordPress 中處理圖像的高級技術。其中包括:
- 注冊專門用于附件的分類
- 使用自定義模板創建 dosucmtn 列表頁面
- 再次使用自定義模板創建圖庫頁面以顯示給定類別中的圖像
- 為每個類別創建“特色圖片”并將其顯示在類別存檔頁面上。
正如您所見,您可以在 WordPress 中對圖像和媒體執行更多操作,而不僅僅是將它們附加到帖子或將它們用作特色圖像。只要發揮一點想象力,您就可以像任何其他帖子類型一樣查詢它們,并輸出文檔或顯示圖像的鏈接。