#在本系列中,我介紹:
- 為附件分配類別和分類法,
- 按分類查詢媒體文件,以便您可以在自訂循環中輸出它們,
- 使用圖像分類查詢將其顯示在存檔頁面上,
- 將圖像新增至類別或分類術語作為類別或術語的「特色圖像」
在第 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</div>', $term_description ); endif; ?> </header><!-- .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; ?></div><!-- #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 中对图像和媒体执行更多操作,而不仅仅是将它们附加到帖子或将它们用作特色图像。只要发挥一点想象力,您就可以像任何其他帖子类型一样查询它们,并输出文档或显示图像的链接。
以上是在 WordPress 中建立「特色圖片」:將附件提升到新的水平的詳細內容。更多資訊請關注PHP中文網其他相關文章!

WordPressisapowerfulCMSwithsignificantadvantagesandchallenges.1)It'suser-friendlyandcustomizable,idealforbeginners.2)Itsflexibilitycanleadtositebloatandsecurityissuesifnotmanagedproperly.3)Regularupdatesandperformanceoptimizationsarenecessarytomainta

WordPressExcccelineaseeandAdaptability,MakeitItiDealForBeginnersandsMallToMedium-SizedBusinesses.1)siseofuse:wordpressisuser-Frylyly.2)安全:drupalleadswithstrongsecurityfeatures.3)性能:performance:performance formation:phast offersefersefersefersefersefersefersefersefersexcellentperformanceedueTonode.sscore.jssor.jjsy.jjsy.4)4)

是的,您可以使用Plypluginslikememberpress,PayMembersubScriptions,OrwooCommerceForuserManagemention,ContentAccesControl,andPaymentMenthandling.2)

你不需要編程知識就能使用WordPress,但掌握編程可以提升體驗。 1)使用CSS和HTML可以調整主題樣式。 2)PHP知識能編輯主題文件,添加功能。 3)自定義插件和元標籤可優化SEO。 4)注意備份和使用子主題以防更新問題。

TosecureaWordPresssite,followthesesteps:1)RegularlyupdateWordPresscore,themes,andpluginstopatchvulnerabilities.2)Usestrong,uniquepasswordsandenabletwo-factorauthentication.3)OptformanagedWordPresshostingorsecuresharedhostingwithawebapplicationfirewal

WordPressExcelSoverotherWeberSiteBuilderSduetoItsflexible,可伸縮性,andopen-sourcenature.1)它'saversatilecmswithExtEnsextEnsexenSiveCustomizedOptionsVIATHEMESANDPLUGINS.2)它的alllearbutoffersbutoffersbutoffersbutoffersbutefersbuterbutfulcontrololoncemastered.3)

2025年網站開發的七個必備WordPress插件 在2025年建立頂級WordPress網站需要速度,響應能力和可擴展性。 實現這種有效的實現通常取決於戰略插件的選擇。 這篇文章Highlig

WordPresscanbeusedforvariouspurposesbeyondblogging.1)E-commerce:WithWooCommerce,itcanbecomeafullonlinestore.2)Membershipsites:PluginslikeMemberPressenableexclusivecontentareas.3)Portfoliosites:ThemeslikeAstraallowstunninglayouts.Ensuretomanageplugins


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境