如何更改自定义POST类型Pemalink层次结构

时间:2015-03-12 作者:deemi-D-nadeem

当我们将permalink设置为Post name 然后转到wordpress的任何默认帖子,如“Testing 123”单页,其链接如下所示

localhost/foo_articles/testing-123
现在,当我们将永久链接更改为Custom Structure 并设置如下值%category%/%postname%, 链接如下所示

http://localhost/foo_articles/testing/testing-123/
测试是我的类别slug

Now the main part of my question is

我制作一个插件,在其中创建一个帖子类型foo_articles 和自定义分类法foo_categories

它工作得很好。当我单击一个类别时,其链接如下所示

http://localhost/foo_articles/foo_category/junk-food/
当我点击一篇文章的单个页面时,它的链接如下所示

http://localhost/foo_articles/foo_articles/how-to-reduce-the-intake-of-junk-food-in-children/
foo\\u articles是我的帖子类型,可以更改

现在我的问题是,当用户设置永久链接时,如何设置链接Custom Structure 并设置如下值%category%/%postname% 我的链接也会像上面的默认贴子单页一样更改。

http://localhost/foo_articles/article cat slug/how-to-reduce-the-intake-of-junk-food-in-children/
以下是自定义邮件类型代码:

add_action(\'init\', \'foo_articles\');
function foo_articles() {

    $foo_slug = \'foo_articles\';
    $foo_slug = get_option(\'foo_plugin_slug\');

    $labels = array(
        \'name\'                  =>  __(\'Foo\', \'fff\'),
        \'singular_name\'         =>  __(\'Foo\', \'fff\'),
        \'all_items\'             =>  __(\'Articles\', \'fff\'),
        \'add_new\'               =>  __(\'New Article\', \'fff\'),
        \'add_new_item\'          =>  __(\'Add New Article\', \'fff\'),
        \'edit_item\'             =>  __(\'Edit Article\', \'fff\'),
        \'new_item\'              =>  __(\'New Article\', \'fff\'),
        \'view_item\'             =>  __(\'View Articles\', \'fff\'),
        \'search_items\'          =>  __(\'Search Articles\', \'fff\'),
        \'not_found\'             =>  __(\'Nothing found\', \'fff\'),
        \'not_found_in_trash\'    =>  __(\'Nothing found in Trash\', \'fff\'),
        \'parent_item_colon\'     =>  \'\'
    );

    $foo_rewrite = array(
        \'slug\'          =>  FOO_PLUGIN_SLUG, // i define this in plugin index file
        \'with_front\'    =>  true,
        \'pages\'         =>  false,
        \'feeds\'         =>  true,
    );

    $args = array(
        \'labels\'                =>  $labels,
        \'public\'                =>  true,
        \'publicly_queryable\'    =>  true,
        \'show_ui\'               =>  true,
        \'query_var\'             =>  true,
        \'menu_icon\'             =>  plugin directory.\'images/icon-foo.png\',
        \'capability_type\'       =>  \'post\',
        \'hierarchical\'          =>  false,
        \'menu_position\'         =>  3,
        \'supports\'              =>  array(\'title\',\'editor\',\'thumbnail\',\'comments\',\'tags\'),
        \'rewrite\'               =>  $foo_rewrite,
        \'show_in_menu\'          =>  true,
        \'show_in_nav_menus\'     =>  true,
        \'show_in_admin_bar\'     =>  true,
        \'can_export\'            =>  true,
        \'has_archive\'           =>  true,
        \'exclude_from_search\'   =>  true
    );

    register_post_type( \'foo_articles\' , $args );
    flush_rewrite_rules();
}
add_action( \'init\', \'foo_taxonomies\', 0 );

// Article taxonamy
function foo_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        \'name\'              =>  __( \'Article Category\', \'fff\'),
        \'singular_name\'     =>  __( \'Article Category\', \'fff\' ),
        \'search_items\'      =>  __( \'Search Article Category\', \'fff\' ),
        \'all_items\'         =>  __( \'All Article Categories\', \'fff\' ),
        \'parent_item\'       =>  __( \'Parent Article Category\', \'fff\' ),
        \'parent_item_colon\' =>  __( \'Parent Article Category:\', \'fff\' ),
        \'edit_item\'         =>  __( \'Edit Article Category\', \'fff\' ),
        \'update_item\'       =>  __( \'Update Article Category\', \'fff\' ),
        \'add_new_item\'      =>  __( \'Add New Article Category\', \'fff\' ),
        \'new_item_name\'     =>  __( \'New Article Category Name\', \'fff\' ),
    \'menu_name\'         =>  __( \'Categories\', \'fff\' )
    );  

    register_taxonomy( \'foo_categories\', array( \'foo_articles\' ), array(
        \'hierarchical\'      =>  true,
        "labels"            =>  $labels,
        "singular_label"    =>  __( \'Foo Category\', \'foo\'),
        \'show_ui\'           =>  true,
        \'query_var\'         =>  true,
        \'rewrite\'           =>  array( \'slug\' => \'foo_category\', \'with_front\' => true )
    ));
    flush_rewrite_rules();
}
Note: 我通过插件设置及其option_namefoo_plugin_slug (这是客户的想法)

所以请告诉我怎么做。是否有任何挂钩、过滤器或htaccess代码

1 个回复
SO网友:Sabir Abdul Gafoor Shaikh

你试过这个插件吗?

WP Permastructure

添加了使用重写标记(如%post\\u id%和%author%)为自定义帖子类型配置永久链接的功能。

enter image description here

下载此插件https://wordpress.org/plugins/wp-permastructure/screenshots/

结束

相关推荐

在加载plugins_后,get_plugins()不工作

知道为什么下面的代码function my_plugin_load() { get_plugins(); } add_action( \'plugins_loaded\', \'my_plugin_load\' ); 抛出此错误?Fatal error: 不应调用未定义的函数get\\u plugins()get_plugins() 定义在plugins_loaded 胡克开火了?如果不是,那么什么才是合适的钩子呢?(这个钩子应该启动插件的引导/加载过程)