php 从发布的帖子永久链接 - Wordpress中删除slug

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 从发布的帖子永久链接 - Wordpress中删除slug相关的知识,希望对你有一定的参考价值。

<?php 
/**
 * Remove the slug from published post permalinks. Only affect our custom post type, though.
 */
function prefix_remove_cpt_slug( $post_link, $post, $leavename ) {

    $custom_post_types = array( 'custom_post_type', 'custom_post_type_2', 'custom_post_type_3' );

    if ( ! in_array( $post->post_type, $custom_post_types ) || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
}
add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );


/**
 * Have WordPress match postname to any of our public post types (post, page, race).
 * All of our public post types can have /post-name/ as the slug, so they need to be unique across all posts.
 * By default, WordPress only accounts for posts and pages where the slug is /post-name/.
 *
 * @param $query The current query.
 */
function prefix_add_cpt_post_names_to_main_query( $query ) {
    // Bail if this is not the main query.
    if ( ! $query->is_main_query() ) {
        return;
    }
    // Bail if this query doesn't match our very specific rewrite rule.
    if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
        return;
    }
    // Bail if we're not querying based on the post name.
    if ( empty( $query->query['name'] ) ) {
        return;
    }
    // Add CPT to the list of post types WP will include when it queries based on the post name.
    $query->set( 'post_type', array( 'post', 'page', 'custom_post_type', 'custom_post_type_2', 'custom_post_type_3' ) );
}
add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );

以上是关于php 从发布的帖子永久链接 - Wordpress中删除slug的主要内容,如果未能解决你的问题,请参考以下文章

错误 404 帖子名称永久链接 wordpress

将类别添加到永久链接时,WordPress CPT 会导致 404

通过 +1 php 更改永久链接的最终编号

Wordpress 单帖子类型模板不显示帖子

WordPress 永久链接仅适用于帖子

更新 httpd.conf 不适用于永久链接