【问题标题】:Wordpress rewrite not working with different postWordpress 重写不适用于不同的帖子
【发布时间】:2016-02-12 09:12:03
【问题描述】:

我正在制作一个插件,我在其中检测选定的自定义字段。如果用户在自定义字段中选择了男孩,则 url 往往是 www.example.com/boys/shirts 如果用户选择女孩,则 url 往往是 www.example.com/girls/top。问题是 wordpress rewite 每次只适用于一种情况。更改选项并保存帖子后,我清除了永久链接的设置。带有

的网址
    if($model_type=="boys") {
            $labels = array(
                'name'                => _x( 'Boys', 'Post Type General Name', $txtdomain ),
                'singular_name'       => _x( 'Boy', 'Post Type Singular Name', $txtdomain ),
                'menu_name'           => __( 'Models', $txtdomain ),
                'parent_item_colon'   => __( 'Parent Model', $txtdomain ),
                'all_items'           => __( 'All Models', $txtdomain ),
                'view_item'           => __( 'View Model', $txtdomain ),
                'add_new_item'        => __( 'Add New Model', $txtdomain ),
                'add_new'             => __( 'New Model', $txtdomain ),
                'edit_item'           => __( 'Edit Model', $txtdomain ),
                'update_item'         => __( 'Update Model', $txtdomain ),
                'search_items'        => __( 'Search models', $txtdomain ),
                'not_found'           => __( 'No models found', $txtdomain ),
                'not_found_in_trash'  => __( 'No models found in Trash', $txtdomain ),
            );

            $this->args = array(
                'description'           => __( 'Models-boys', $txtdomain ),
                'labels'                => $labels,
                'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
                'hierarchical'          => false,
                'public'                => true,
                'show_ui'               => true,
                'show_in_menu'          => true,
                'show_in_nav_menus'     => true,
                'show_in_admin_bar'     => true,
                'menu_icon'             => trailingslashit( $uri ) . 'images/model-icon.png',
                'can_export'            => true,
                'has_archive'           => true,
                'exclude_from_search'   => false,
                'publicly_queryable'    => true,
                'rewrite'               => array( 'slug' => 'boys' )
            );
    } else {
            $labels = array(
                'name'                => _x( 'Girls', 'Post Type General Name', $txtdomain ),
                'singular_name'       => _x( 'Girl', 'Post Type Singular Name', $txtdomain ),
                'menu_name'           => __( 'Models', $txtdomain ),
                'parent_item_colon'   => __( 'Parent Model', $txtdomain ),
                'all_items'           => __( 'All Models', $txtdomain ),
                'view_item'           => __( 'View Model', $txtdomain ),
                'add_new_item'        => __( 'Add New Model', $txtdomain ),
                'add_new'             => __( 'New Model', $txtdomain ),
                'edit_item'           => __( 'Edit Model', $txtdomain ),
                'update_item'         => __( 'Update Model', $txtdomain ),
                'search_items'        => __( 'Search models', $txtdomain ),
                'not_found'           => __( 'No models found', $txtdomain ),
                'not_found_in_trash'  => __( 'No models found in Trash', $txtdomain ),
            );

            $this->args = array(
                'description'           => __( 'Models-mens', $txtdomain ),
                'labels'                => $labels,
                'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
                'hierarchical'          => false,
                'public'                => true,
                'show_ui'               => true,
                'show_in_menu'          => true,
                'show_in_nav_menus'     => true,
                'show_in_admin_bar'     => true,
                'menu_icon'             => trailingslashit( $uri ) . 'images/model-icon.png',
                'can_export'            => true,
                'has_archive'           => true,
                'exclude_from_search'   => false,
                'publicly_queryable'    => true,
                'rewrite'               => array( 'slug' => 'girls' )
            );
    }

女孩们正在工作并打开single.php,但男孩们打开了未找到的页面。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我不确定这样的双重结构是否可能,
    但您可以尝试在代码中添加它:

    flush_rewrite_rules( false );
    

    if 块和else 块的末尾。

    '因为你必须在发生时强制改变结构。

    来源:https://codex.wordpress.org/Function_Reference/flush_rewrite_rules

    【讨论】:

    • 嗯,这实际上似乎对我有用。看起来很棒。但它会一次又一次地改变 htaccess
    • 我在 htaccess 中有自定义代码。每当调用它时,它会被删除吗?
    • 我不知道,也不能告诉你对.htaccess 的影响。我建议您获取.htaccess 的副本并尝试。通常,其他参数应该保持不变,或者至少会以相同的方式自动重写。
    • 我在 codex.wordpress.org 看到了。参数:(布尔值)(可选)是更新 .htaccess(硬刷新)还是仅更新 rewrite_rules 瞬态(软刷新)。默认值:true(硬刷新)。试试flush_rewrite_rules( false );
    • 但我也读到不建议在每次重新加载页面时使用此功能。即使我在每个页面加载时都使用软,它也会刷新。那会好吗?您的解决方案有效,但我很担心
    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 2011-04-26
    • 2023-03-31
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    相关资源
    最近更新 更多