Can't remove slug in url

时间:2015-06-18 作者:JaneFarrow

我需要移除子弹movie 从现场。com/movie/mymoviename

我已尝试从代码中删除“slug”=>“movie”,“with\\u front”=>“FALSE”),但这对我没有帮助。这里有一个代码

    <?php 

/* Movies post type*/

function post_type_movies() {
register_post_type(
                    \'movies\', 
                    array( \'public\' => true,
                            \'publicly_queryable\' => true,
                            \'has_archive\' => true, 
                            \'hierarchical\' => false,
                            \'menu_icon\' => get_stylesheet_directory_uri() . \'/images/movie.png\',
                            \'labels\'=>array(
                                        \'name\' => _x(\'Movies\', \'post type general name\'),
                                        \'singular_name\' => _x(\'Movie\', \'post type singular name\'),
                                        \'add_new\' => _x(\'Add New\', \'Movies\'),
                                        \'add_new_item\' => __(\'Add New Movie\'),
                                        \'edit_item\' => __(\'Edit Movie\'),
                                        \'new_item\' => __(\'New Movie\'),
                                        \'view_item\' => __(\'View Movie\'),
                                        \'search_items\' => __(\'Search Movies\'),
                                        \'not_found\' =>  __(\'No Movies found\'),
                                        \'not_found_in_trash\' => __(\'No Movie found in Trash\'), 
                                        \'parent_item_colon\' => \'\'
                                        ),                           
                            \'show_ui\' => true,
                            \'menu_position\'=>5,
                            \'query_var\' => true,
                            \'rewrite\' => TRUE,
                            \'rewrite\' => array( \'slug\' => \'movie\', \'with_front\' => FALSE,),
                            \'register_meta_box_cb\' => \'mytheme_add_box\',
                            \'supports\' => array(
                                        \'title\',
                                        \'thumbnail\',
                                        \'comments\',
                                        \'editor\'
                                        )
                            ) 
                    );
                } 
add_action(\'init\', \'post_type_movies\');

/* Movie genre taxonomy */

function create_movie_genre_taxonomy() 
{
$labels = array(
                              \'name\' => _x( \'Movie Genre\', \'taxonomy general name\' ),
                              \'singular_name\' => _x( \'movie-genre\', \'taxonomy singular name\' ),
                              \'search_items\' =>  __( \'Search movie genres\' ),
                              \'all_items\' => __( \'All movie genres\' ),
                              \'parent_item\' => __( \'Parent movie genre\' ),
                              \'parent_item_colon\' => __( \'Parent movie genre:\' ),
                              \'edit_item\' => __( \'Edit movie genre\' ), 
                              \'update_item\' => __( \'Update movie genre\' ),
                              \'add_new_item\' => __( \'Add New Movie Genre\' ),
                              \'new_item_name\' => __( \'New movie genre Name\' ),
);  
register_taxonomy(\'movie-genre\',array(\'movies\'), array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'movie-genre\' ),
  ));
}

add_action( \'init\', \'create_movie_genre_taxonomy\', 0 );

?> 
我读了很多关于这方面的帖子,尝试了很多带有“slug remover”的插件,但都没有任何帮助。也许你可以看看我的代码,告诉我出了什么问题。

1 个回复
SO网友:Charles

在做了一些研究之后,我发现了以下代码,并将其制作成一个插件(您必须创建一个php文件并为其命名,将下面的代码复制到该文件中,然后将其放在您的插件文件夹中)
这不是我的代码,因此我引用了作者和原始源代码,并链接到github

激活此插件后,您必须(重新)设置永久链接,方法是转到“管理”面板中的“设置”永久链接,然后单击“保存更改”

注意:创建cpt的函数不是百分之百正确的(即使用两次\'rewrite\'=>, 并且使用文本域(或者不使用,但更确切地说,不要像您那样部分使用它),也许重新创建它是一个更好的选择?Online engine to create a CPT
只是一个提示,当使用自定义帖子类型时,将其制作为插件并将其放入插件文件夹中。(因此,在切换主题(即测试)后,您手头仍然有您的自定义帖子

我不是在本地测试的,但他们说它可以工作,我不能保证在不久的将来不会出现问题,因为(至少在我看来)很少会以你想要的方式留下鼻涕虫。

 <?php 
  /*
   Plugin Name:  Rewrite CPT Movie slug
   Description:  Removes slug from published post type permalinks.(Only affect our CPT though) / Have WordPress match postname to any of our public post types (movie, page, post)
   Author:       Charles
   License: GNU General Public License v2 or later
   License URI: http://www.gnu.org/licenses/gpl-2.0.html
 */
 /**
  * Remove the slug from published post permalinks. Only affect our CPT though.
  *
  * @author Kellen Mace <http://kellenmace.com>
  * @link   http://kellenmace.com/remove-custom-post-type-slug-from-permalinks/
  * @see    https://gist.github.com/kellenmace/65d100fa6c76d249c53f#file-remove-custom-post-type-slug-from-permalinks-2-php    
  */
 function remove_cpt_slug_191875( $post_link, $post, $leavename ) {

      if ( \'movie\' != $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\', \'remove_cpt_slug_191875\', 10, 3 );

/**
 * Have WordPress match postname to any of our public post types (movie, page, post)
 * All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
 * By default, core only accounts for posts and pages where the slug is /post-name/
 *
 * @author Kellen Mace <http://kellenmace.com>
 * @link   http://kellenmace.com/remove-custom-post-type-slug-from-permalinks/
 * @see    https://gist.github.com/kellenmace/b39553b3c7243ff62040#file-remove-slug-from-custom-post-type-php
 */
function parse_request_trick_191875( $query ) {
     // Only noop the main query
     if ( ! $query->is_main_query() )
     return;

    // Only noop our very specific rewrite rule match
    if ( 2 != count( $query->query ) || ! isset( $query->query[\'page\'] ) ) {
    return;
    }

   // \'name\' will be set if post permalinks are just post_name, otherwise the page rule will match
   if ( ! empty( $query->query[\'name\'] ) ) {
         $query->set( \'post_type\', array(  \'movie\', \'post\', \'page\' ) );
   }
}
add_action( \'pre_get_posts\', \'parse_request_trick_191875\' );

结束

相关推荐

访问‘POST’POST类型的Slug显示404未存档?

我正在尝试让WP默认的帖子类型“post”显示在/news/. 我已将permalink结构更改为/news/%postname%/ 这在访问单个页面时很好,但是当我只是访问/新闻/没有附加帖子名称时,会发出404,以反对显示archive-post.php 或事件archive.php 样板我已经检查了permalink的更改是否通过访问一篇文章刷新了重写规则,所以我认为这不可能是问题所在。如果您有任何解决问题的想法,我们将不胜感激,如果我方提供的更多信息能有所帮助,请随时询问。