Automatically create a link that goes back to the custom post archive page with the custom post name (and including the theme slug)

*爱你&永不变心* 提交于 2019-12-12 02:43:05

问题


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)


回答1:


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


来源:https://stackoverflow.com/questions/25410175/automatically-create-a-link-that-goes-back-to-the-custom-post-archive-page-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!