Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 使用自定义分类名称、自定义分类术语和默认帖子名称(非CPT)的自定义Permalink结构_Wordpress - Fatal编程技术网

Wordpress 使用自定义分类名称、自定义分类术语和默认帖子名称(非CPT)的自定义Permalink结构

Wordpress 使用自定义分类名称、自定义分类术语和默认帖子名称(非CPT)的自定义Permalink结构,wordpress,Wordpress,我正在建立一个网站,将不使用网页,而是使用帖子(旧式可湿性粉剂使用这里)。不过,我不会使用内置的“类别”,而是使用四种自定义分类法中的一种。同样,这些自定义分类将附加到默认帖子,而不是自定义帖子类型。我遇到的问题是permalink结构。我希望我的post permalink包含分配给它的第一个分类术语,以及分类名称。例如: 我有一个自定义的分类法,叫做“学术”,有一个术语“教育”。如果一篇文章被归类为“教育”,那么该文章的URL应该是“/academics/education/postname

我正在建立一个网站,将不使用网页,而是使用帖子(旧式可湿性粉剂使用这里)。不过,我不会使用内置的“类别”,而是使用四种自定义分类法中的一种。同样,这些自定义分类将附加到默认帖子,而不是自定义帖子类型。我遇到的问题是permalink结构。我希望我的post permalink包含分配给它的第一个分类术语,以及分类名称。例如:

我有一个自定义的分类法,叫做“学术”,有一个术语“教育”。如果一篇文章被归类为“教育”,那么该文章的URL应该是“/academics/education/postname/”

即使这篇文章使用多个自定义分类术语进行分类,我也只希望找到的第一个术语显示在URL中

下面是我如何设置分类法的:

register_taxonomy( 'academics', 'post', array(
    'labels' => array(
        'name' => __( 'Academics' ),
        'singular_name' => __( 'Academics' ),
        'search_items' => __( 'Search Academics' ),
        'popular_items' => __( 'Popular Academics' ),
        'all_items' => __( 'All Academics' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Academics' ),
        'update_item' => __( 'Update Academics' ),
        'add_new_item' => __( 'Add New Academics' ),
        'new_item_name' => __( 'New Academics Name' ),
        'separate_items_with_commas' => __( 'Separate Academics With Commas' ),
        'add_or_remove_items' => __( 'Add or Remove Academics' ),
        'choose_from_most_used' => __( 'Choose From the Most Used Academics' )
    ),
    'public' => true,
    'hierarchical' => false,
    'show_ui' => true,
    'show_in_quick_edit' => false,
    'meta_box_cb' => false,
    'show_in_nav_menus' => true,
    'show_admin_column' => true,
    'args' => array( 'orderby' => 'term_order' ),
    'rewrite' => true,
    'query_var' => true
) );
然后,我的permalink结构(在管理区域)设置如下:

/%custom_category%/%postname%/
然后我有两个函数。。。第一个术语将
%custom\u category%
替换为与帖子关联的第一个自定义分类法中的第一个术语:

/**
 * replace the '%custom_category%' tag with the first
 * term slug found for the post
 *
 * @wp-hook post_link
 * @param string $permalink
 * @param WP_Post $post
 * @return string
 */
add_filter( 'post_link', 'custom_taxonomy_permalink', 10, 3 );
add_filter( 'post_type_link', 'custom_taxonomy_permalink', 10, 3 );
function custom_taxonomy_permalink( $permalink, $post_id, $leavename ) {
    if ( strpos( $permalink, '%custom_category%' ) === false ) {
        return $permalink;
    }
    // Get post
    $post = get_post( $post_id );
    if ( ! $post ) {
        return $permalink;
    }
    // Get taxonomy terms
    $terms = wp_get_object_terms( $post->ID, array(
        'academics',
        'art-performance',
        'on-campus-announcements',
        'student-life-events'
    ) );
    if ( ! is_wp_error( $terms ) && ! empty( $terms ) && is_object( $terms[0] ) ) {
        $taxonomy = $terms[0]->taxonomy . '/';
        $taxonomy_slug = $terms[0]->slug;
    } else {
        $taxonomy = '';
        $taxonomy_slug = '';
    }
    return str_replace('%custom_category%', $taxonomy . $taxonomy_slug, $permalink);
}
所有这些都非常有效,前端的permalink结构完全符合我的要求。。。然而,每一篇文章都是404。所以我认为我需要创建自定义重写。因此,我有另一个函数,它应该添加重写规则:

    // Function to create custom rewrite rules
    function custom_rewrite_rules( $rules ) {
        $newRules  = array();
        $newRules['academics/(.+)/?$'] = 'index.php?academics=$matches[1]';
        $newRules['academics/(.+)/(.+)/?$'] = 'index.php?pagename=$matches[2]';
        return array_merge( $newRules, $rules );
    }
    add_filter( 'rewrite_rules_array', 'custom_rewrite_rules' );
这就是我猜测出问题的地方,因为我对自定义重写不太熟悉。目前,该函数只处理“学术界”自定义分类法,因为我认为如果我让这个分类法起作用,我可以对其余分类法使用相同的逻辑

有人知道为什么我的永久链接不起作用吗?每次更改重写时,我都会访问permalinks设置页面刷新规则。我还使用旧插件来查看我的规则生效,所以我知道它们在那里。。。他们只是不工作