WordPress网站如何为附件添加分类目录,并调用目录名字加入链接中

  [复制链接]  27查看
易西 发表于 2025-9-3 00:31:36 | 显示全部楼层 |阅读模式
WordPress网站如何为附件添加分类目录,并调用目录名字加入链接中
 楼主| 易西 发表于 2025-9-3 00:32:34 | 显示全部楼层
WordPress附件分类目录与链接集成方案
要实现为WordPress附件添加分类目录并将目录名加入链接中,可以通过以下完整方案实现:

一、为附件添加分类目录
‌注册自定义分类法‌:
需要为attachment post type注册自定义分类法,可以使用register_taxonomy_for_object_type函数实现
4
6。

‌推荐插件方案‌:

Media Categories插件:为媒体库项目添加分类功能,提供简洁的分类元框
4
WP Media Categories插件:支持为媒体文件添加分类管理
5
二、完整代码实现方案
以下是完整的实现代码,包含分类注册和链接修改:

三、功能说明
‌分类注册‌:

创建了名为"媒体分类"的自定义分类法
该分类法专门用于attachment文章类型
支持层级分类结构
4
6
‌链接修改‌:

使用attachment_link过滤器修改默认附件链接
新链接格式为:/media/{分类别名}/{附件名称}/
自动获取附件所属的第一个分类
16
22
‌重写规则‌:

添加自定义重写规则匹配新链接结构
确保新链接能够正确解析到附件页面
23
四、使用说明
将代码添加到主题的functions.php文件中
访问WordPress后台 -> 设置 -> 固定链接,点击保存刷新重写规则
在媒体库中为附件分配分类
使用get_attachment_link()获取包含分类的链接
五、获取分类名称的方法
在模板中获取附件分类名称可以使用以下代码:

php
Copy Code
$terms = wp_get_object_terms($attachment_id, 'media_category');


:::ml-data{name=citationList}
```json
[{"abstract":" 1添加文章分类 有分类才可以发文章,首先建分类目录 : 后台“ 文章-分类目录“ 新装Wordpress默认就已有一个分类,名为“未分类”,你也可以直接编辑改名字。 2发布文章内容 后台“文章-写文章”,把文章的标题,内容还有附加属性(可不填),再点击发布按钮,即可。","url":"https://blog.csdn.net/weixin_42634049/article/details/130432873","text":"wordpress建站基本教程(六)wordpress添加分类,发布文章","source":{"logo":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","name":"CSDN博客","tag":""},"icon":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","poster":"http://t7.baidu.com/it/u=1363938048,3235901893&fm=217&app=137&f=JPEG?w=800&h=533&s=0C287433131B646D4C5580DE0000C0B2","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"wordpress建站基本教程(六)wordpress添加分类,发布文章","thumbnail":"http://t7.baidu.com/it/u=1363938048,3235901893&fm=217&app=137&f=JPEG?w=800&h=533&s=0C287433131B646D4C5580DE0000C0B2","linkInfo":{"href":"https://blog.csdn.net/weixin_42634049/article/details/130432873"}},{"abstract":" 1、登陆后台,在文章-分类目录-添加分类目录:在名称下面输入“网站优化”,别名可以是英文也可以是该词的拼音。然后点击下面的“添加分类目录”按钮。 这样一个新的分类目录页就生成了,更新文章时选择你添加的那个如“网站优化”勾选上就可以。这样你的文章就生成到了这个分类页面上了。 2、分类目录可以显示到网站的导航栏上去,方法: 这时我们需要新建一个菜单,比如“网","url":"https://blog.csdn.net/ssnz88net/article/details/34847943","text":"旗袍女装网站分类优化:频道页优化","source":{"logo":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","name":"CSDN博客","tag":""},"icon":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"旗袍女装网站分类优化:频道页优化","linkInfo":{"href":"https://blog.csdn.net/ssnz88net/article/details/34847943"}},{"abstract":" 第一章、简介 上一篇介绍了wordpress后台管理的“媒体”管理,这篇文章紧接着,将介绍wordpress后台管理的“链接”管理 wordpress后台管理的“链接”管理,包括“全部链接”、“添加”和“链接分类目录”这三个子节点的内容,下面的章节将围绕这三个节点的内容展开进行描述,如下图所示。 与“文章”管理类似,本文将先后介绍“链接分类目录”、“添加”、“全部链接”。 第二章、链接分类目录 链接分类目录的作用可以分类我们常用的链接,比如“博客链接”分类目录存放一些非常好的博客参考链接。我们可以使用“链接分类目录”功能来创建一组链接,链接分类的名字必须是唯一的,如下图所示。 您可以通过“批量操作”来一次删除多个链接分类目录,但是删除操作并不影响分类中的链接。其下链接将被自动移至默认链接分类目录。 第三章、添加 我们可以在“添加”页面中,添加一个参考博客的链接及其他属性,如下图所示,将按几个步骤描述如何添加一个链接。其中,第六步骤,记得点击添加。 第四章、全部链接 顾名思义,“全部链接”页面,当然是显示并管理我们所有的链接啦,我们可以看到刚才添加的链接“沧海箫剑CSDN博客”,如下图所示。","url":"https://blog.csdn.net/xpj8888/article/details/109638072","text":"wordpress后台管理(四)链接管理:全部链接、添加、链接分类目录","source":{"logo":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","name":"CSDN博客","tag":""},"icon":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","poster":"http://t9.baidu.com/it/u=3088415583,3897989237&fm=217&app=126&f=JPEG?w=799&h=242&s=1CC2DC120F6265200E7834DA03009035","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"wordpress后台管理(四)链接管理:全部链接、添加、链接分类目录","thumbnail":"http://t9.baidu.com/it/u=3088415583,3897989237&fm=217&app=126&f=JPEG?w=799&h=242&s=1CC2DC120F6265200E7834DA03009035","linkInfo":{"href":"https://blog.csdn.net/xpj8888/article/details/109638072"}},{"abstract":" Allows users to assign categories (or other taxonomy terms) to items in their Media Library with a clean and simplified, searchable version of the standard category meta box. The “Search Categories” field allows you to narrow your search for a category as you type – this functionality is not native to WordPress but is instead borrowed from Jason Corradino’s Searchable Categoriesplugin. If you would like to enable this feature for your posts","url":"https://wordpress.org/plugins/media-categories-2/","text":"Media Categories","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"wordpress.org","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"Media Categories","linkInfo":{"href":"https://wordpress.org/plugins/media-categories-2/"}},{"abstract":"China 简体中文 获取WordPress Plugin Directory WP Media Categories  提交插件 我的收藏 登录  提交插件 我的收藏 登录 此插件尚未翻译成 简体中文。帮助翻译!搜索插件 该插件尚未通过WordPress的最新3个主要版本进行测试。 当与较新版本的WordPress一起使用时,可能不再受到维护或支持,并且可能会存在兼容性问题。 关闭  Chinese (Taiwan)、 Dutch、 English (US)、 German、 Norwegian (Bokmål)、 Spanish (Chile)、 Spanish (Spain) 和Swedish. 翻译成简体中文 ","url":"https://cn.wordpress.org/plugins/wp-media-categories/advanced/","text":"WP Media Categories","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"cn.wordpress.org","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"WP Media Categories","linkInfo":{"href":"https://cn.wordpress.org/plugins/wp-media-categories/advanced/"}},{"abstract":"WordPress Media Category 作者:wbcomdesigns  (3总评级)This plugin helps administrators of the site categorize their wordpress media.与6.3.0版本兼容 ","url":"https://cn.wordpress.org/plugins/media-category/embed/","text":"WordPress Media Category ","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"cn.wordpress.org","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"WordPress Media Category ","linkInfo":{"href":"https://cn.wordpress.org/plugins/media-category/embed/"}},{"abstract":" Attachment Pages Redirect (3总评级) Generate PDF from Your Form Submissions and Download/Email Them Send PDF for Contact Form 7 (80总评级) Create, customize and send PDF attachments with Contact Form 7 form Disable Media Pages (17总评级) Completely remove \"attachment\" pages for WordPress media. Improve SEO and prevent conflicts between page and image permalinks. Download Attachments (46总评级) Download Attachments is a new approach to managing downloads in WordPress. It allows you to easily add and display download links in any post or page. DCO Comment Attachment (10总评级) Allows your visitors to attach files with their comments Product Attachment for WooCommerce (22总评级) Plugin lets you easily upload any file type (images, documents, videos, etc.) to customer orders. DX Delete Attached Media (28总评级)","url":"https://cn.wordpress.org/plugins/tags/attachment/","text":"attachment","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"cn.wordpress.org","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"attachment","linkInfo":{"href":"https://cn.wordpress.org/plugins/tags/attachment/"}},{"abstract":" Hide Title  (87总评级) Allows authors to hide the title on single pages and posts via the edit post screen. Dojo Digital 40,000+ 个活跃安装 与5.2.21版本兼容  Post Tags and Categories for Pages  (23总评级) Adds the built in WordPress categories and tags to your pages. curtismchale 20,000+ 个活跃安装 与6.1.0版本兼容  Click to Call Button  (1总评级) Small floating click to call phone button for your website  Digital2 2,000+ 个活跃安装 与6.4.5版本兼容  WP SendFox  (8总评级) Capture emails and add them to your SendFox list via comments, registration, WooCommerce checkout, Gutenberg page or Divi Builder page. BogdanFix 1,000+ 个活跃安装 与6.4.5版本兼容  WP Hydra  (10总评级) Allows one WordPress installation to be resolved and browsed at multiple domains. Marin Atanasov 1,000+ 个活跃安装 与5.1.19版本兼容  WP-Farsi  (7总评级)","url":"https://cn.wordpress.org/plugins/tags/wp","text":"分类为 wp 的插件 | WordPress.org China 简体中文","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"cn.wordpress.org","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"分类为 wp 的插件 | WordPress.org China 简体中文","linkInfo":{"href":"https://cn.wordpress.org/plugins/tags/wp"}},{"abstract":" 社交媒体插件:这些插件允许用户在网站上添加社交媒体分享按钮、社交媒体登录功能等。例如,\"Social Warfare\"插件可以添加漂亮的社交分享按钮,提高内容的可分享性。 SEO插件:这些插件帮助优化网站以提高搜索引擎排名。例如,\"Yoast SEO\"插件可以分析网站内容并提供优化建议,以改善网站的可见性。 安全插件:这些插件帮助保护网站免受恶意攻击和黑客入侵。例如,\"Wordfence Security\"插件可以监控网站的安全性,并提供防火墙和恶意软件扫描功能。 缓存插件:这些插件可以提高网站的加载速度,提供更好的用户体验。例如,\"W3 Total Cache\"插件可以缓存网站的静态文件,减少服务器负载并加快页面加载速度。 表单插件:这些插件用于创建各种类型的表单,如联系表单、订阅表单等。例如,\"Contact Form 7\"插件是一个简单而灵活的表单插件,可以轻松创建各种表单。 腾讯云提供了适用于WordPress的云托管服务,称为\"云服务器CVM\"。它提供了高性能的虚拟机实例,可用于部署和运行WordPress网站。您可以在腾讯云官方网站上找到有关云服务器CVM的更多信息和产品介绍。","url":"https://cloud.tencent.com/developer/information/%E6%AD%A3%E7%A1%AE%E6%9F%A5%E8%AF%A2wordpress%E7%B1%BB%E5%88%AB%E7%9A%84%E9%99%84%E5%8A%A0%E5%B8%96%E5%AD%90","text":"正确查询wordpress类别的附加帖子","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"腾讯云","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"正确查询wordpress类别的附加帖子","linkInfo":{"href":"https://cloud.tencent.com/developer/information/%E6%AD%A3%E7%A1%AE%E6%9F%A5%E8%AF%A2wordpress%E7%B1%BB%E5%88%AB%E7%9A%84%E9%99%84%E5%8A%A0%E5%B8%96%E5%AD%90"}},{"abstract":" 方法一: $category = get_the_category();//默认获取当前所属分类 echo $category[0]->cat_name;//使用$categories->cat_name不能获得正确值,应该使用 $categories[0]->cat_name 输出第一项 //函数返回值:cat_ID - 分类ID ,cat_name - 分类名 ,category_nicename - 别名 ,category_description - 分类描述 ,category_parent - 父分类ID , category_count - 包涵文章数量 方法二: global $wp_query; $cat_ID = get_query_var('cat'); 只能在分类页里使用。","url":"https://blog.csdn.net/yanli_xu/article/details/7643737","text":"WordPress分类页面获取当前分类的ID","source":{"logo":"https://pic.rmb.bdstatic.com/bjh/user/cfb31af7056caab2fae2c60fc5e3a603.jpeg","name":"CSDN","tag":""},"icon":"https://pic.rmb.bdstatic.com/bjh/user/cfb31af7056caab2fae2c60fc5e3a603.jpeg","author_url":"https://author.baidu.com/home/1563894916825412?from=dusite_sresults","author_unify_sign":0,"author_name":"CSDN","author_img":"https://pic.rmb.bdstatic.com/bjh/user/cfb31af7056caab2fae2c60fc5e3a603.jpeg","type":"","vid":"","level":0,"suffix":"","title":"WordPress分类页面获取当前分类的ID","linkInfo":{"href":"https://blog.csdn.net/yanli_xu/article/details/7643737"}},{"abstract":"在wordpress中,可以使用以下代码从帖子中获取类别名称: 代码语言: php 复制 <?php $category = get_the_category ( ) ; //获取当前帖子的类别信息 if ( ! empty ( $category ) ) { $category_name = $category [ 0 ] -> name ; //获取第一个类别的名称 echo $category_name ; } ?> 上述代码中, get_the_category() 函数用于获取当前帖子的类别信息,并将结果存储在 $category 变量中.然后,通过判断 $category 是否为空,可以确定当前帖子是否有类别.如果有类别,可以通过 $category[0]->name 获取第一个类别的名称,并将结果存储在 $category_name 变量中.最后,使用 echo 语句输出类别名称. ","url":"https://cloud.tencent.com/developer/information/%E5%A6%82%E4%BD%95%E5%9C%A8Wordpress%E4%B8%AD%E4%BB%8E%E5%B8%96%E5%AD%90%E4%B8%AD%E8%8E%B7%E5%8F%96%E7%B1%BB%E5%88%AB%E5%90%8D%E7%A7%B0%EF%BC%9F","text":"如何在Wordpress中从帖子中获取类别名称?","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"腾讯云","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"如何在Wordpress中从帖子中获取类别名称?","linkInfo":{"href":"https://cloud.tencent.com/developer/information/%E5%A6%82%E4%BD%95%E5%9C%A8Wordpress%E4%B8%AD%E4%BB%8E%E5%B8%96%E5%AD%90%E4%B8%AD%E8%8E%B7%E5%8F%96%E7%B1%BB%E5%88%AB%E5%90%8D%E7%A7%B0%EF%BC%9F"}},{"abstract":"php $category_id = get_cat_id('Category Name'); query_posts('cat='.$category_id); if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile; endif; ?> 2.如何根据分类ID获取分类的链接URL? 要想实现这个功能,必须要用到这个函数:get_category_link(),这个函数的用法如下: <?php get_category_link( $category_id ) ?> 1. 下面是有关这个函数的一些介绍和说明:参数categroy_id 分类ID号 默认值:None,类型为整数,必需,返回的值,分类URL 字符串型 示例代码如下: <?php $category_id = get_cat_ID( 'Category Name' ); $category_link = get_category_link( $category_id ); ?> <a href=\"<?php echo $category_link; ?>\" title=\"Category Name\">你的分类目录名称</a> 3.显示文章所属分类目录的链接 此功能的调用函数为:the_category(),需要强调的是:必须在主循环LOOP中使用它!切记! 其用法如下:<?php the_ca","url":"https://blog.51cto.com/u_16213656/11435919","text":"wordpress 多个分类目录模板 wordpress调用分类目录","source":{"logo":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3385518650,4195902768&fm=195&app=88&f=JPEG?w=200&h=200","name":"51CTO博客","tag":""},"icon":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3385518650,4195902768&fm=195&app=88&f=JPEG?w=200&h=200","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"wordpress 多个分类目录模板 wordpress调用分类目录","linkInfo":{"href":"https://blog.51cto.com/u_16213656/11435919"}},{"abstract":" 以下代码: $args= array( 'type' => 'attachment', 'category_name' => 'portfolio' ); $attachments = get_posts($args); print_r($attachments);","url":"https://blog.csdn.net/weixin_35012431/article/details/115776849","text":"wordpress attachment.php,php –在wordpress中使用wp_get_attachment_image()的正确方法-CSDN博客","source":{"logo":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","name":"CSDN博客","tag":""},"icon":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"wordpress attachment.php,php –在wordpress中使用wp_get_attachment_image()的正确方法-CSDN博客","linkInfo":{"href":"https://blog.csdn.net/weixin_35012431/article/details/115776849"}},{"abstract":" accachment_icon 在get_attachment_icon函数中,该过滤器函数应用于附件图标。过滤器函数可接收的参数:图标文件(作为HTMLIMG标签),附件ID。 attachment_innerHTML 在get_attachment_innerHTML函数中,若附件没有图标,该过滤器函数应用于附件标题。","url":"https://blog.csdn.net/jazywoo123/article/details/9817315","text":"WordPress插件API/常用过滤器(filter)","source":{"logo":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","name":"CSDN博客","tag":""},"icon":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"WordPress插件API/常用过滤器(filter)","linkInfo":{"href":"https://blog.csdn.net/jazywoo123/article/details/9817315"}},{"abstract":" 1、Functions by category 分类函数,Post, Page, Attachment and Bookmarks Functions 文章,页面,附件和链接的函数 get_adjacent_post 返回邻近的文章信息数组,详情见:http://codex.wordpress.org/Function_Reference/get_adjacent_post. get_children 检索附件、版本、子页面等信息,一般情况下由父文章执行,详情见 :http://codex.wordpress.org/Function_Reference/get_children. get_extended 获取文章 more 标签分割的数组.$post 是文章的内容 get_next_post 获取下一篇邻近的文章信息. get_post 返回单篇文章的信息数组或对象,$id 是文章 id,注意这里只能用变量,详情见: http://codex.wordpress.org/Function_Reference/get_post. get_post_ancestors 返回基于文章 ID 的父文章的信息数组. get_post_mime_type 返回基于附件 ID 的方式类型信息. get_post_status 返回基于文章 ID 的文章状态. get_post_type 返回基于文章 ID 的文章类型. get_previous_post 返回上一篇邻近的文章信息. get_posts 获取一系列文章的集合,详情见:http://codex.wordpress.org/Template_Tags/get_posts. is_post 已弃用. is_single 判断是否规定的单篇日志. is_sticky 判断文章是否置顶. register_post_type 注册发布类型,详情见:http://codex.w","url":"https://blog.csdn.net/weixin_39605894/article/details/111859250","text":"wordpress函数手册_WordPress函数说明","source":{"logo":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","name":"CSDN博客","tag":""},"icon":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"wordpress函数手册_WordPress函数说明","linkInfo":{"href":"https://blog.csdn.net/weixin_39605894/article/details/111859250"}},{"abstract":" 1. 默认结构 默认结构的形式为:/?p=123。这是最基本的链接形式,使用文章ID作为标识。虽然简单,但对SEO和可读性都不友好,不建议使用。 2. 日期和名称结构 形如:/2023/05/15/sample-post/。这种结构包含发布日期和文章标题,对于新闻类网站比较适用。但URL较长,且日期可能影响文章的时效性认知。 3. 月份和名称结构 类似于:/2023/05/sample-post/。比完整日期结构简洁,仍保留时间信息,适合定期更新的博客。 4. 文章名结构 直接使用文章标题作为URL:/sample-post/。这是最简洁、对用户最友好的结构。有利于SEO,便于分享和记忆,是很多网站的首选。 5. 分类和名称结构 形如:/category/sample-post/。将文章分类纳入URL,有助于展示网站结构,对于内容层次分明的网站很有帮助。 6. 自定义结构 WordPress允许使用标签自定义URL结构,如/%category%/%postname%/。这提供了最大的灵活性,可以根据网站需求定制最佳结构。","url":"https://www.henghost.com/news/article/186914/","text":"在WordPress中,常见的永久链接结构有哪些?","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"www.henghost.com","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"在WordPress中,常见的永久链接结构有哪些?","linkInfo":{"href":"https://www.henghost.com/news/article/186914/"}},{"abstract":" 1、不要让日期出现在固定链接里面 这基于两个方面的考虑。一是如果日期出现在固定链接里面,等于提醒搜索引擎,这是很旧的内容了,没必要再爬一遍了。另外一个原因是,假如你要修改文章的日期重新发布的话,链接地址就变了,也就是意味着你的反向链接、PR 等等都没有了。 2、不要让一篇文章选择多个分类 这一点是很多人都会忽略的地方。一篇文章如果选择了多个分类的话,则会出现多个链接地址,这很容易造成因为重复内容而被搜索引擎惩罚。 3、链接层级不要过深 这一点比较常见,原因在于很多wordpress 用户在链接这一块选择了默认的固定链接是年/月/日/分类名/文章名。笔者当初也是选择日期和名称型的固定链接方式,但是还是注意到这一点,将“/”修改成了“-”,毕竟这种过于深的固定链接对搜索引擎是非常不利于抓取的。 4、中文字符 对于URL中使用中文字符的利弊有很多说法,仁者见仁智者见智。 优点: 中文关键词在中文URL中对搜索引擎可能有一定的权重; SERP上如果中文URL和搜索词有相同时,URL上会加粗展示,有可能提高点击率; URL结构比较清晰,可以直接从URL上判断,某种程度上用户体验会比较舒服; 增加网页和关键词的匹配; 缺点:URL中使用中文字符会涉及到“字符集编码方式”,被 Encode 转码,变的很长。 过长的中文URL可能会影响收录,百度对过长的URL收录不算太友好;","url":"https://blog.51cto.com/u_16213718/13973254","text":"wordpress 图文分离 wordpress分类页面布局","source":{"logo":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3385518650,4195902768&fm=195&app=88&f=JPEG?w=200&h=200","name":"51CTO博客","tag":""},"icon":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3385518650,4195902768&fm=195&app=88&f=JPEG?w=200&h=200","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"wordpress 图文分离 wordpress分类页面布局","linkInfo":{"href":"https://blog.51cto.com/u_16213718/13973254"}},{"abstract":" WordPress仅仅用了10 个表:wp_comments, wp_links, wp_options, wp_postmeta, wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms, wp_usermeta, wp_users 按照功能大致分为五类 用户信息: wp_users和wp_usermeta 链接信息: wp_links 文章及评论信息: wp_posts、wp_postmeta、wp_comments 对分类,链接分类,标签管理: wp_term,wp_term_relationships,wp_term_taxonomy 全局设置信息: wp_options wp_posts 博客发表”文章”存放的地方就是这个wp_posts表了。这个表里存放的除了普通的文章之外,还有附件和页面(page)的一些信息。post_type字段是用来区分文章类型的。如果post_type是’post’,那么就是文章,如果是’page’,那么就是页面,如果是’attachment’,那么就是附件了.","url":"https://www.cnblogs.com/xiaochaohuashengmi/archive/2011/08/08/2130679.html","text":"WordPress数据结构分析","source":{"logo":"https://search-operate.cdn.bcebos.com/175c73f4973c6168ed6acae5b638fa15.png","name":"博客园","tag":""},"icon":"https://search-operate.cdn.bcebos.com/175c73f4973c6168ed6acae5b638fa15.png","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"WordPress数据结构分析","linkInfo":{"href":"https://www.cnblogs.com/xiaochaohuashengmi/archive/2011/08/08/2130679.html"}},{"abstract":" 1️⃣ 什么是固定链接? 固定链接,也称为永久链接,是指文章或页面的URL地址,是您网站上每个页面的唯一网址。它通常包括您的网站域名和页面的自定义名称。 2️⃣ 为什么需要设置固定链接? 固定链接不仅影响搜索引擎优化,还直接影响用户体验。清晰、易懂的固定链接结构可以提高用户对网站内容的理解,有助于提高页面点击率。通过使用固定链接,能够清晰地显示文章的类别和标题,从而提高搜索引擎优化(SEO)的效果,搜索引擎对于包含关键词的固定链接更为青睐,能让您的网站更容易被搜索引擎找到,有助于提升网站在搜索结果中的排名, 3️⃣ 最佳固定链接结构 日期和名称型结构 将页面的发布日期和名称作为URL的一部分 例如:https://www.example.com/2023/12/15/sample-post/ 数字型结构 将页面的文章ID作为URL的一部分,适合社区或论坛使用。 例如:https://www.example.com/archives/123 文章名称结构 将页面的文章标题作为URL的一部分,后续可以自定修改链接名称,有利于谷歌SEO。 例如:https://www.example.com/sample-post/ 类别和标签结构 将页面所属的类别或标签作为URL的一部分 例如:https://www.example.com/category/sample-category/ 自定义结构 根据您的需求,可以创建自定义的固定链接结构,可通过伪静态设置或把前几种类型组合到一起的固定链接。 例如:https://www.example.com/sample-post-12345/","url":"https://blog.csdn.net/qq_52475653/article/details/135027603","text":"优化WordPress固定链接:最佳结构与修改指南","source":{"logo":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","name":"CSDN博客","tag":""},"icon":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","poster":"http://t9.baidu.com/it/u=2734891450,714956244&fm=217&app=126&f=JPEG?w=800&h=519&s=59A03C72198B404D0C7DC5DE0300C0B3","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"优化WordPress固定链接:最佳结构与修改指南","thumbnail":"http://t9.baidu.com/it/u=2734891450,714956244&fm=217&app=126&f=JPEG?w=800&h=519&s=59A03C72198B404D0C7DC5DE0300C0B3","linkInfo":{"href":"https://blog.csdn.net/qq_52475653/article/details/135027603"}},{"abstract":"默认情况下,WordPress会将你的固定链接结构为http://domain.com/?p=123,这个结构是比较“粗糙”的,无法受到搜索引擎的欢迎,很多博主为了给自己的博","url":"https://blog.51cto.com/topic/487d9308c35413b.html","text":"wordpress设置里修改了固定链接_51CTO博客","source":{"logo":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3385518650,4195902768&fm=195&app=88&f=JPEG?w=200&h=200","name":"51CTO博客","tag":""},"icon":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3385518650,4195902768&fm=195&app=88&f=JPEG?w=200&h=200","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"wordpress设置里修改了固定链接_51CTO博客","linkInfo":{"href":"https://blog.51cto.com/topic/487d9308c35413b.html"}},{"abstract":" 在WordPress 中,永久链接(Permalinks)是指每个页面、帖子或分类的永久 URL 地址。它们不仅为内容提供了固定的访问路径,还形成了网站的 URL 结构。良好的永久链接结构可以帮助搜索引擎更好地理解页面内容,并为用户提供更好的浏览体验。 默认情况下,WordPress 会根据设置中定义的结构自动生成永久链接。这使得发布内容变得简单,但在某些情况下,可能希望自定义这些链接以更好地满足 SEO 需求或用户体验。 何时更改 WordPress 永久链接 更改WordPress 永久链接结构通常不是日常操作,而是一项重大的调整。以下是可能需要更改永久链接的几个场景: 网站重塑 SEO 改进 改善用户体验 更改永久链接前应考虑和做什么 在进行永久链接更改之前,需要进行详细的规划。以下是几个关键的考虑因素: SEO 和用户体验:新的永久链接结构应有利于 SEO 并提升用户体验。简单直观的 URL 对于用户和搜索引擎来说都更友好。 未来增长需求:确保新的 URL 结构能够适应网站的长期发展,避免频繁修改链接结构。 备份网站:在更改之前,务必备份整个网站,以防出现意外问题。 更新内部链接和设置重定向:列出所有需要更新的内部链接,并为更改后的 URL设置301重定向,以确保外部链接不会导致404错误。 在自定义和预设结构之间进行选择 WordPress 提供了多种永久链接的预设结构,涵盖了从简单到复杂的各种需求。如果这些预设结构不能满足你的要求,还可以选择自定义结构。 常见的预设结构包括: 纯文本(默认结构,包含查询字符串) 日期和名称 月份和名称 数字(基于帖子ID) 帖子名称 自定义结构允许你根据需要组合不同的标签,如%year%、%postname%等,以创建特","url":"https://www.361sale.com/17050/","text":"如何更改和自定义 WordPress 中的永久链接","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"光子波动网","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"如何更改和自定义 WordPress 中的永久链接","linkInfo":{"href":"https://www.361sale.com/17050/"}},{"abstract":" 对于Wordpress比较熟悉的朋友应该知道,如果想修改文章(post)的固定链接结构(permalinks),可以到Wordpress后台管理界面进行修改,比如说,如果你想让页面地址都有一个“tag”的前缀,形如: http://www.nbzhuti.cn/tag/108 你可以在后台的“固定链接”设置界面将固定链接结构设置为“/news/%postname%”就行了。 但是,你会发现,只有文章页(post)的链接结构上这样改的,而一般页面(page)的链接结构没有变化,而且,标签(tag)的链接结构也变成了”/tag/%tag%”样子。","url":"https://zhuanlan.zhihu.com/p/422539780","text":"自定义WordPress标签(tag)和分类目录(category)的固定链接结构的方法","source":{"logo":"http://gips0.baidu.com/it/u=2373281029,3404312066&fm=3033&app=3033&f=JPEG?w=200&h=200","name":"nnewtzhihu","tag":""},"icon":"http://gips0.baidu.com/it/u=2373281029,3404312066&fm=3033&app=3033&f=JPEG?w=200&h=200","author_url":"","author_unify_sign":0,"author_name":"nnewtzhihu","author_img":"http://gips0.baidu.com/it/u=2373281029,3404312066&fm=3033&app=3033&f=JPEG?w=200&h=200","type":"","vid":"","level":0,"suffix":"","title":"自定义WordPress标签(tag)和分类目录(category)的固定链接结构的方法","linkInfo":{"href":"https://zhuanlan.zhihu.com/p/422539780"}},{"abstract":" 登录WordPress后台:打开WordPress网站的后台管理界面。 导航到设置选项:在左侧导航菜单中,找到并点击“设置”选项。 打开Permalink设置:在“设置”菜单下拉列表中,选择“永久链接”。 选择自定义结构:在Permalink设置页面中,选择“自定义结构”选项。 编辑自定义结构:在自定义结构文本框中,输入所需的链接结构。要创建3级自定义分类,可以使用以下标记: %category%:表示文章所属的主分类。 %category%/%subcategory%:表示文章所属的主分类和子分类。 %category%/%subcategory%/%postname%:表示文章所属的主分类、子分类和文章标题。 例如,如果你想要链接结构为:https://example.com/category/subcategory/postname,可以在自定义结构文本框中输入:/%category%/%subcategory%/%postname%/。 保存设置:点击“保存更改”按钮,将自定义结构保存为新的Permalink设置。","url":"https://cloud.tencent.com/developer/information/%E5%A6%82%E4%BD%95%E9%98%85%E8%AF%BBWordpress%20Permalink%E8%87%AA%E5%AE%9A%E4%B9%89%E5%88%86%E7%B1%BB%E4%B8%BA3%E7%BA%A7%EF%BC%9F-ask","text":"如何阅读Wordpress Permalink自定义分类为3级?","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"腾讯云","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"如何阅读Wordpress Permalink自定义分类为3级?","linkInfo":{"href":"https://cloud.tencent.com/developer/information/%E5%A6%82%E4%BD%95%E9%98%85%E8%AF%BBWordpress%20Permalink%E8%87%AA%E5%AE%9A%E4%B9%89%E5%88%86%E7%B1%BB%E4%B8%BA3%E7%BA%A7%EF%BC%9F-ask"}},{"abstract":" http://www.webhek.com/news/hollow-world/ AI写代码html 你可以在后台的“固定链接”设置界面将固定链接结构设置为“/news/%postname%”就行了。 但是,你会发现,只有文章页(post)的链接结构上这样改的,而一般页面(page)的链接结构没有变化,而且,标签(tag)的链接结构也变成了”/news/%tag%”样子。","url":"https://blog.csdn.net/sam_zhang1984/article/details/51159200","text":"自定义WordPress里标签(tag)和分类目录(category)的固定链接结构的方法_wordpress的product tag和post tag可以设置一样吗-CSDN博客","source":{"logo":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","name":"CSDN博客","tag":""},"icon":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2005731947,4139443793&fm=195&app=88&f=JPEG?w=200&h=200","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"自定义WordPress里标签(tag)和分类目录(category)的固定链接结构的方法_wordpress的product tag和post tag可以设置一样吗-CSDN博客","linkInfo":{"href":"https://blog.csdn.net/sam_zhang1984/article/details/51159200"}},{"abstract":"wp-include/post.php line 4785: function wp_get_attachment_url( $post_id = 0 ) { $post_id = (int) $post_id; if ( !$post = get_post( $post_id ) ) return false; if ( 'attachment' != $post->post_type ) return false; $url = \"; if ( $file = get_post_meta( $post->id, '_wp_attached_file', true) ) { //get attached file if ( ($uploads = wp_upload_dir()) &amp;&amp; false === $uploads['error'] ) { //get upload directory if ( 0 === strpos($file, $uploads['basedir']) ) //check that the upload base exists in the file location $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location elseif ( false !== strpos($file, 'wp-content/uploads') ) $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 ); else $url = $uploads['baseurl'] . \"/$file\"; ","url":"https://zh-cn.forums.wordpress.org/topic/wp-v392%E9%99%84%E4%BB%B6url%E9%97%AE%E9%A2%98","text":"wp v3.9.2附件url问题","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"WordPress中国简体中文支持论坛","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"wp v3.9.2附件url问题","linkInfo":{"href":"https://zh-cn.forums.wordpress.org/topic/wp-v392%E9%99%84%E4%BB%B6url%E9%97%AE%E9%A2%98"}},{"abstract":" 1、WordPress图片附件的默认链接方式 WordPress图片附件的默认链接方式是通过 image_default_link_type 来定义的,类型可以是’none’,’file’,’post’三个类型,分别是无链接,链接到媒体文件(原始地址),链接到附件页面。 要自定义默认的链接方式为”链接到媒体文件(原始地址)“,可以在主题的 functions.php 文件中添加以下代码即可: //图片默认连接到媒体文件(原始链接) update_option('image_default_link_type', 'file'); 2、使用WordPress自带高级设置 访问http://yoursite.com/wp-admin/options.php 这个地址, 找到image_default_link_type 填上file即可。","url":"https://cloud.tencent.com/developer/article/1482000","text":"WordPress图片附件默认链接图片URL","source":{"logo":"http://t9.baidu.com/it/u=2010487710,999831580&fm=217&app=126&f=JPEG?w=800&h=582&s=08A05D32856246AA2245A8CB010070B2","name":"腾讯云","tag":""},"icon":"http://t9.baidu.com/it/u=2010487710,999831580&fm=217&app=126&f=JPEG?w=800&h=582&s=08A05D32856246AA2245A8CB010070B2","poster":"http://t9.baidu.com/it/u=2010487710,999831580&fm=217&app=126&f=JPEG?w=800&h=582&s=08A05D32856246AA2245A8CB010070B2","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"WordPress图片附件默认链接图片URL","thumbnail":"http://t9.baidu.com/it/u=2010487710,999831580&fm=217&app=126&f=JPEG?w=800&h=582&s=08A05D32856246AA2245A8CB010070B2","linkInfo":{"href":"https://cloud.tencent.com/developer/article/1482000"}},{"abstract":"在wordpress中创建自定义url是通过使用插件或修改代码来实现的.以下是一种常见的方法: 使用插件:可以使用插件来轻松地创建自定义url.以下是一些常用的插件: \"custom permalinks\"插件:允许您自定义每个帖子和页面的url. \"yoast seo\"插件:除了提供seo功能外,还可以自定义url结构. 修改代码:如果您熟悉wordpress主题的开发,您可以通过修改代码来创建自定义url.以下是一些步骤: 打开wordpress主题文件夹中的functions.php文件. 在文件中找到register_post_type函数,该函数用于注册自定义帖子类型. 在register_post_type函数中添加'rewrite'参数,以定义自定义url结构.例如: 在register_post_type函数中添加'rewrite'参数,以定义自定义url结构.例如: 保存文件并刷新wordpress后台. 自定义url的优势是可以提高网站的可读性和搜索引擎优化(seo).通过使用有意义的关键字和结构化的url,可以增加网站在搜索引擎结果中的可见性. 自定义url的应用场景包括但不限于: 博客文章:可以根据文章的主题或关键字创建自定义url,以提高文章的可读性和搜索引擎排名. 产品页面:为每个产品创建自定义url,以便用户可以轻松地记住和分享产品链接. 事件页面:为特定事件或活动创建自定义url,以便用户可以方便地访问相关信息. 腾讯云提供的相关产品和服务包括: 云服务器(cvm):提供可扩展的虚拟服务器实例,用于托管wordpress网站. 云数据库mysql版(cdb):提供高性能,可扩展的mysql数据库服务,用于存储wordpress数据. 云存储(cos):提供安全,可靠的对象存储服务,用于存储wordpress中的媒体文件. 云安全中心(ssc):提供全面的安全监控和防护服务,保护wordpress网站免受网络攻击. 您可以访问腾讯云官方网站了解更多关于这些产品的详细信息和使用指南. ","url":"https://cloud.tencent.com/developer/information/%E6%88%91%E6%83%B3%E5%9C%A8wordpress%E4%B8%AD%E5%88%9B%E5%BB%BA%E8%87%AA%E5%AE%9A%E4%B9%89URL-salon","text":"我想在wordpress中创建自定义URL - 腾讯云开发者社区 - 腾讯云","source":{"logo":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","name":"腾讯云","tag":""},"icon":"https://gips3.baidu.com/it/u=3236075928,3550158464&fm=3028&app=3028&f=PNG&fmt=auto&q=75&size=f48_48","author_url":"","author_unify_sign":0,"author_name":"","author_img":"","type":"","vid":"","level":0,"suffix":"","title":"我想在wordpress中创建自定义URL - 腾讯云开发者社区 - 腾讯云","linkInfo":{"href":"https://cloud.tencent.com/developer/information/%E6%88%91%E6%83%B3%E5%9C%A8wordpress%E4%B8%AD%E5%88%9B%E5%BB%BA%E8%87%AA%E5%AE%9A%E4%B9%89URL-salon"}}]
:::

参考
 楼主| 易西 发表于 2025-9-3 00:37:27 | 显示全部楼层

  1. <?php
  2. // 1. 为附件注册分类目录
  3. function register_media_taxonomy() {
  4.     $labels = array(
  5.         'name' => '媒体分类',
  6.         'singular_name' => '媒体分类',
  7.         'search_items' => '搜索媒体分类',
  8.         'all_items' => '所有媒体分类',
  9.         'parent_item' => '父级分类',
  10.         'parent_item_colon' => '父级分类:',
  11.         'edit_item' => '编辑分类',
  12.         'update_item' => '更新分类',
  13.         'add_new_item' => '添加新分类',
  14.         'new_item_name' => '新分类名称',
  15.         'menu_name' => '媒体分类'
  16.     );

  17.     $args = array(
  18.         'hierarchical' => true,
  19.         'labels' => $labels,
  20.         'show_ui' => true,
  21.         'show_admin_column' => true,
  22.         'query_var' => true,
  23.         'rewrite' => array('slug' => 'media-category')
  24.     );

  25.     register_taxonomy('media_category', 'attachment', $args);
  26. }
  27. add_action('init', 'register_media_taxonomy');

  28. // 2. 修改附件链接结构包含分类信息
  29. function custom_attachment_link($link, $post_id) {
  30.     $post = get_post($post_id);
  31.     if ('attachment' != $post->post_type) {
  32.         return $link;
  33.     }
  34.    
  35.     $terms = wp_get_object_terms($post->ID, 'media_category');
  36.     if (!empty($terms)) {
  37.         $term = array_shift($terms);
  38.         $link = home_url("/media/{$term->slug}/{$post->post_name}/");
  39.     }
  40.    
  41.     return $link;
  42. }
  43. add_filter('attachment_link', 'custom_attachment_link', 10, 2);

  44. // 3. 添加重写规则
  45. function custom_attachment_rewrite_rules() {
  46.     add_rewrite_rule(
  47.         'media/([^/]+)/([^/]+)/?$',
  48.         'index.php?attachment=$matches[2]',
  49.         'top'
  50.     );
  51.     add_rewrite_tag('%media_category%', '([^/]+)');
  52. }
  53. add_action('init', 'custom_attachment_rewrite_rules');
复制代码



参考
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

邮箱|首页|小黑屋|吾侪 ( 蜀ICP备2020029307号-4 )

GMT+8, 2025-12-4 15:33 , Processed in 0.061811 second(s), 22 queries , APCu On.

Powered by Discuz! X3.5

Copyright © , 吾侪网

快速回复 返回顶部 返回列表