使用自定义帖子名称自动创建一个链接,该链接返回到自定义帖子存档页面(包括主题 slug)

Posted

技术标签:

【中文标题】使用自定义帖子名称自动创建一个链接,该链接返回到自定义帖子存档页面(包括主题 slug)【英文标题】:Automatically create a link that goes back to the custom post archive page with the custom post name (and including the theme slug) 【发布时间】:2014-10-14 03:11:40 【问题描述】:

如何使用自定义帖子名称(包括主题 slug)自动创建返回到当前自定义帖子存档页面的链接

【问题讨论】:

【参考方案1】:

这里有一个快速的方法。您可以将其发布在您的单个帖子模板文件中。它将检查帖子类型,确定主博客是否在网站主页上,或确定主博客是否在辅助页面上,并显示适当的链接。

<?php
    // Get the current post type
    $postType = get_post_type();

    // Check if post type is "post" and if main blog is the home page
    if ($postType == post && get_option('show_on_front') == 'page') 
        echo '<a href="' . get_permalink(get_option('page_for_posts')) . '">Main Blog Archive Link</a>';
    // Check is post type is "post" and not set to have main blog on home page
     elseif ($postType == post && !get_option('show_on_front') == 'page') 
        echo '<a href="' . site_url() . '">Front Page Index Link</a>';
    // If custom post type do this
     else 
        echo '<a href="' . get_post_type_archive_link($postType) . '">Post Type Archive Link</a>';
    
?>

【讨论】:

...虽然生成的链接似乎转到当前帖子链接而不是 /theme/custom-post-archive/ 是的,这将获取当前帖子的帖子类型并生成指向该帖子存档页面的链接。它适用于常规“帖子”以及任何自定义帖子类型。你在找什么? 很好,但我只想要一个指向当前父自定义帖子的链接。例如,如果 slug 是 'site/theme/my-custom-post/the-custom-post' 那么我只想显示一个链接返回到父 'site/theme/my-custom-post/' 很抱歉,我不太明白您的要求。我认为您想返回父帖子类型,上面的代码将执行此操作。我在这里错过了什么吗? 我会再试一次 - 之前的链接刚刚转到自定义帖子类型的当前帖子

以上是关于使用自定义帖子名称自动创建一个链接,该链接返回到自定义帖子存档页面(包括主题 slug)的主要内容,如果未能解决你的问题,请参考以下文章

在标题后立即添加按钮/链接到自定义帖子类型编辑屏幕

PHP Wordpress插入自定义大小的帖子附件链接到自定义大小

WP 冲突自定义帖子类型链接到相同的页面名称

如何将电子邮件移动到自定义帖子类型?

Autoconf:链接器错误链接到自定义库

问:使用 DOM 将 Javascript(通过链接到 HTML 文件)添加到自定义元素