如何更改自定义帖子类型永久链接层次结构

时间:2015-03-12 11:02:54

标签: php wordpress wordpress-plugin permalinks

当我们将永久链接设置为Post name并转到wordpress任何默认帖子时,如“测试123”单页,其链接看起来像这样

localhost/foo_articles/testing-123

现在,当我们将永久链接更改为Custom Structure并将值设置为%category%/%postname%时,链接看起来像这样

http://localhost/foo_articles/testing/testing-123/

测试是我的类别slug

现在我的问题的主要部分是

我创建了一个插件,我在其中创建了帖子类型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_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();
}

注意:我通过插件设置更改我的帖子类型slug,其option_namefoo_plugin_slug(客户的想法)

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

1 个答案:

答案 0 :(得分:1)

您可以使用WP Walker概念。请检查

WP Walker

例如:

使用 ACF插件获取自定义字段。

列出页面代码: -

       $args = array(
          'child_of'     => $post->ID,
          'date_format'  => get_option('date_format'),
          'post_type'    => 'page',
          'title_li'     => __(''),
          'walker'       => new my_page_walker 
        ); 

        wp_list_pages( $args );

在function.php中扩展Walker_page

在function.php中

class my_page_walker extends Walker_Page {
    public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
        if ( $depth ) {
            $indent = str_repeat( "\t", $depth );
        } else {
            $indent = '';
        }

        $css_class = array( 'page_item', 'page-item-' . $page->ID );

        if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
            $css_class[] = 'page_item_has_children';
        }

        if ( ! empty( $current_page ) ) {
            $_current_page = get_post( $current_page );
            if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
                $css_class[] = 'current_page_ancestor';
            }
            if ( $page->ID == $current_page ) {
                $css_class[] = 'current_page_item';
            } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
                $css_class[] = 'current_page_parent';
            }
        } elseif ( $page->ID == get_option('page_for_posts') ) {
            $css_class[] = 'current_page_parent';
        }
        $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );

        if ( '' === $page->post_title ) {
            $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
        }

        $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
        $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];

        $page_permalink   = get_permalink( $page->ID );

        $user_defined_link =  get_field('my-custom-field',$page->ID)['url'];
        if (!is_null($user_defined_link)) {
            $page_permalink = $user_defined_link; 
        }

        $output .= $indent . sprintf(
            '<li class="%s"><a href="%s">%s%s%s</a>',
            $css_classes,
            $page_permalink,
            $args['link_before'],
            apply_filters( 'the_title', $page->post_title, $page->ID ),
            $args['link_after']
        );

        if ( ! empty( $args['show_date'] ) ) {
            if ( 'modified' == $args['show_date'] ) {
                $time = $page->post_modified;
            } else {
                $time = $page->post_date;
            }

            $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
            $output .= " " . mysql2date( $date_format, $time );
        }
    }

}

最后一个链接将是自定义字段中的值。