【问题标题】:How to remove slug from Custom Post Type?如何从自定义帖子类型中删除 slug?
【发布时间】:2020-02-07 08:43:58
【问题描述】:

我正在尝试从名为“文章”的自定义帖子类型中删除 slug。我正在探索 StackOverflow,我发现了很多讨论,但它们都不起作用。如果你用 slug="/" 重写它会弄乱其他帖子和页面。那么WordPress是否能够删除slug?谢谢。

我已经尝试过堆栈溢出的不同解决方案。

    'rewrite' => array( 
        'slug'          => '/',
        'with_front'    => false 
    ),

【问题讨论】:

  • 你在使用任何插件吗?

标签: wordpress custom-post-type


【解决方案1】:

你可以试试这个:

add_filter( 'post_type_link', function( $post_link, $post, $leavename ) {

    $post_types = array(
        'product_pages',
        'my_test_page'
    );

    if ( in_array( $post->post_type, $post_types ) && 'publish' === $post->post_status ) {
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    }

    return $post_link;

}, 10, 3 );

add_action( 'pre_get_posts', function( $query ) {

    
    if ( ! $query->is_main_query() )
        return;
    
    if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    
    if ( ! empty( $query->query['name'] ) ) {

            $post_types = array(
            'post', 
            'page', 
            'product_pages',
            'my_test_page'
        );

        $query->set( 'post_type', $post_types );

    }

});

【讨论】:

    【解决方案2】:
    try this
    chinesepost is slug name of custom post type
    <?php
    function na_remove_slug_chinesespost( $post_link, $post, $leavename ) {
    
        if ( 'chinesespost' != $post->post_type || 'publish' != $post->post_status ) {
            return $post_link;
        }
    
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    
        return $post_link;
    }
    add_filter( 'post_type_link', 'na_remove_slug_chinesespost', 10, 3 );
    
    function na_parse_request_chinesespost( $query ) {
    
        if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
            return;
        }
    
        if ( ! empty( $query->query['name'] ) ) {
            $query->set( 'post_type', array( 'post', 'chinesespost', 'page' ) );
        }
    }
    add_action( 'pre_get_posts', 'na_parse_request_chinesespost' );
    ?>
    

    【讨论】:

      【解决方案3】:

      你可以试试这个:

      fw-portfolio 是后期类型的 slug 名称,您应该包含您的名称

       function gp_remove_cpt_slug( $post_link, $post ) {
              if ( 'fw-portfolio' === $post->post_type && 'publish' === $post->post_status ) {
                  $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
              }
              return $post_link;
          }
          add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 2 );
      
      
          function gp_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', 'fw-portfolio' ) );
          }
          add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-27
        • 2019-02-23
        • 1970-01-01
        • 2015-10-31
        • 2020-02-23
        • 2015-02-06
        • 2021-08-21
        相关资源
        最近更新 更多