问题
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