使用自定义帖子名称(包括主题slug)自动创建返回自定义帖子存档页面的链接 [英] Automatically create a link that goes back to the custom post archive page with the custom post name (and including the theme slug)

查看:148
本文介绍了使用自定义帖子名称(包括主题slug)自动创建返回自定义帖子存档页面的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I automatically create a link that goes back to the current custom post archive page with the custom post name (and including the theme slug)

推荐答案

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

Here's a quick way to do it. You can post this in your single post template files. It will check for the post type, determine if the main blog is on the homepage of the site, or determine if the main blog is on a secondary page, and display the appropriate links.

<?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>';
    }
?>

这篇关于使用自定义帖子名称(包括主题slug)自动创建返回自定义帖子存档页面的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆