Wordpress 重定向不适用于自定义帖子类型

问题描述 投票:0回答:1

我正在构建一个 Wordpress 网站,具有两个嵌套的自定义帖子类型,但遇到错误。我希望能够实现以下基本结构:

  1. https://website.com/news/ --> 显示我的常规 WordPress 帖子
  2. https://website.com/events/ --> 显示自定义事件列表,我的自定义类型“事件”
  3. https://website.com/events/an-event/ --> 示例事件
  4. https://website.com/events/an-event/ride-01/ --> 显示自定义“乘车”帖子类型实例

我在“设置”>“固定链接”>“自定义结构”上实现了(1.):/news/%postname%/ - 它工作正常。

我使用自定义插件实现了其余部分,代码如下。

(2.) 工作正常。 (3.) 工作正常。

我的问题是(4.) - 我能够创建游乐设施,并且在管理面板中一切看起来都很好,甚至是查看它们的链接。但是,当我查看该帖子时,例如在“https://website.com/events/an-event/ride-01/”处,我收到 404 错误。在我看来这应该由这条线处理,但不是:

add_rewrite_rule( '^/events/([^/]+)/([^/]+)/?$','/index.php?rides=$matches[2]','top' );

当我查看 https://website.com/index.php?rides=ride-01 我可以看到我的“骑行”。

如何按照 (4.) 的要求查看具有所需 URL 的游乐设施?我已经尝试了“重写”和“层次结构”选项的多种配置,但似乎无法使其工作。

值得注意的是,每次进行更改时,我都会重新启动网络服务器并保存永久链接页面以清除所有缓存。

谢谢!

<?php

/*
Plugin Name: Etc etc.
*/

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

function fun_be_post_type()
{
    $labels = array(
        'name'               => _x('Events', 'post type general name'),
        'singular_name'      => _x('Event', 'post type singular name'),
        'add_new'            => _x('Add New', 'track'),
        'add_new_item'       => __('Add New Event'),
        'edit_item'          => __('Edit Event'),
        'new_item'           => __('New Event'),
        'all_items'          => __('All Events'),
        'view_item'          => __('View Event'),
        'search_items'       => __('Search Events'),
        'not_found'          => __('No events found'),
        'not_found_in_trash' => __('No events found in the Trash'),
        'menu_name'          => 'Events'
    );
    $args = array(
        'labels'        => $labels,
        'rewrite' => array(
            'slug' => 'events', // This controls the base slug that will display before each term
            'with_front' => false // Don't display the category base before "/locations/"
        ),
        'hierarchical' => true,
        'description'   => 'Holds our bike events and event-specific data',
        'public'        => true,
        'publicly_queryable' => true,
        'show_in_rest'  => true,
        'query_var'     => true,
        'capability_type'    => 'post',
        'map_meta_cap' => true,

        'rest_controller_class' => 'WP_REST_Posts_Controller',
        'menu_position' => 5,
        'menu_icon'     => 'data:image/svg+xml;base64,PHN2ZyB3etc...+', // base64 encoded svg menu icon
        'supports'      => array('title', 'editor', 'thumbnail', 'excerpt', 'comments', 'revisions', 'custom-fields'),
        'has_archive'   => true,
        'autop'         => false
    );
    register_post_type('events', $args);

    $labels = array(
        "name" => "Rides",
        "singular_name" => "Ride",
    );

    $args = array(
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "show_ui" => true,
        "has_archive" => true,
        "show_in_menu" => true,
        'menu_position' => 6,
        //'with_front' => false,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        'hierarchical' => false,
        "rewrite" => array("slug" => "events/%event%", "with_front" => false),
        "query_var" => true,
        "supports" => array("title", "revisions", "thumbnail")
    );

    register_post_type("rides", $args);
}
add_action('init', 'fun_be_post_type');


add_action('add_meta_boxes', function () {
    add_meta_box('rides-parent', 'Events', 'rides_attributes_meta_box', 'rides', 'side', 'default');
});

function rides_attributes_meta_box($post)
{
    $pages = wp_dropdown_pages(array('post_type' => 'events', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column' => 'menu_order, post_title', 'echo' => 0));
    if (! empty($pages)) {
        echo $pages;
    } // end empty pages check
}

add_action('init', function () {

    add_rewrite_rule('^/events/([^/]+)/([^/]+)/?$', '/index.php?rides=$matches[2]', 'top');
});

add_filter('post_type_link', function ($link, $post) {
    if ('rides' == get_post_type($post)) {
        //Lets go to get the parent cartoon-series name
        if ($post->post_parent) {
            $parent = get_post($post->post_parent);
            if (!empty($parent->post_name)) {
                return str_replace('%event%', $parent->post_name, $link);
            }
        } else {
            //to add later
        }
    }
    return $link;
}, 10, 2);

add_filter('manage_rides_posts_columns', 'set_custom_edit_br_columns');

function set_custom_edit_br_columns($columns)
{
    $columns['br_highlight'] = __('Strava ID', 'bike-ride');

    $columns = array(
        'cb' => $columns['cb'],
        'title' => __('Title'),
        'parent_id' => __('Event'),
        'date' => __('Date'),
    );

    return $columns;
}

add_action('manage_rides_posts_custom_column', 'custom_br_column', 10, 2);
function custom_br_column($column, $post_id)
{
    switch ($column) {

        case 'parent_id':
            $post_parent = get_post_parent($post_id);
            $parent_title = get_the_title($post_parent);
            $parent_view_link = get_permalink($post_parent);
            $parent_edit_link = get_edit_post_link($post_parent);
            echo ("
      <a class='row' href='" . $parent_edit_link . "' aria-label='" . $parent_title . " (Edit)'>" . $parent_title . "</a>
      
      <div class='row-actions'><span class='edit'><a href='" . $parent_edit_link . "' aria-label='Edit “" . $parent_title . "”'>Edit</a> | </span><span class='view'><a href='" . $parent_view_link . "' rel='bookmark' aria-label='View “" . $parent_title . "”'>View</a></span></div>
    ");
            break;
    }
}
php wordpress
1个回答
0
投票

根据mmm的评论,解决方案是将重写规则修改为:

add_rewrite_rule('events/([^/]+)/([^/]+)/?$', 'index.php?rides=$matches[2]', 'top');
© www.soinside.com 2019 - 2024. All rights reserved.