php - 自定义帖子类型和页面层次结构 - 错误 404

标签 php wordpress http-status-code-404 custom-post-type advanced-custom-fields

我定义了两种自定义帖子类型:

  • 主题
  • 菲切

我还有 7 个页面使用定义的 rubrique 模板:(template-rubrique.php)

我的 ThemeFiche 都有一个 ACF post-object 字段。

  • 主题 ACF 后对象字段用于定位 Rubrique
  • Fiche ACF 后对象字段用于定位主题

我希望我的 CPTS URL 采用以下格式:example.com/myRubriqueName/myThemeName/myFicheName

myRubriqueName is a Page, while myThemeName and myFicheName are CPT.


到目前为止,我所有的 fichetheme 帖子 URL 都生成良好,但它们最终出现在 404 页面中。此外,我的帖子和页面被重定向到首页。

我正在使用 this post 中的代码,我试图根据自己的情况进行调整。


CPT 注册:

register_post_type('theme', array(
    'labels' => array(
        'name' => 'Thèmes',
        'singular_name' => 'Thème',
    ),
    'public' => true,
    'has_archive' => false,
    'hierarchical' => false,
    'menu_icon' => 'dashicons-art',
    'rewrite' => array(
        'slug' => '%rubrique%', // %rubrique% is used as placeholder
        'with_front' => false
    )
));


register_post_type('fiche', array(
    'labels' => array(
        'name' => 'Fiches',
        'singular_name' => 'Fiche',
    ),
    'public' => true,
    'has_archive' => false,
    'hierarchical' => false,
    'menu_icon' => 'dashicons-clipboard',
    'rewrite' => array(
        'slug' => '%rubrique%/%theme%', // %rubrique%/%theme% is used as placeholder
        'with_front' => false
    ),
));


重写规则

function fiche_rewrite() {

    add_rewrite_tag(
        '%theme%',
        '([^&]+)',
        'theme='
    );
}
add_action( 'init', 'fiche_rewrite' );


function theme_rewrite() {

    add_rewrite_tag(
        '%rubrique%',
        '([^&]+)',
        'rubrique='
    );
}
add_action( 'init', 'theme_rewrite' );


CPT 占位符重写

function gpc_custom_post_link_replacements( $post_link, $post ) {

    $cpts = array('theme', 'fiche');

    if ( empty( $post ) || !in_array($post->post_type, $cpts) ) {
        return $post_link;
    }

    switch ($post->post_type) {
        case 'fiche':
            $theme_id = get_field('fiche-attachment', $post->ID);
            $theme_slug = get_post_field( 'post_name', $theme_id );

            $rubrique_id = get_field('theme-attachment', $theme_id);
            $rubrique_slug = get_post_field('post_name', $rubrique_id);

            if ( !empty( $theme_slug ) && !empty( $rubrique_slug ) ) {
                $post_link = str_replace('%rubrique%', $rubrique_slug, $post_link );
                $post_link = str_replace('%theme%', $theme_slug, $post_link );
            }

            break;

        case 'theme':

            $rubrique_id = get_field('theme-attachment', $post->ID);
            $rubrique_slug = get_post_field('post_name', $rubrique_id);

            if ( !empty( $rubrique_slug ) ) {
                $post_link = str_replace('%rubrique%', $rubrique_slug, $post_link );
            }

            break;

    }

    return $post_link;
}
add_filter( 'post_type_link', 'wpc_custom_post_link_replacements', 9, 2 );


不匹配的相关帖子重定向

function custom_post_redirects() {

    global $post, $wp_query;
    $redirect_to = get_home_url();

    if( ! is_singular( 'fiche' ) && ! is_singular('theme') ) {
        return;
    }


    if( is_singular('fiche') ) {
        $given_slug = $wp_query->get( 'theme' );
        $expected_theme = get_field('field-attachment', $post->ID );

        if( empty( $given_slug ) || empty( $expected_theme ) ) {
            wp_redirect( $redirect_to );
            exit();
        }

        $expected_slug = get_post_field( 'post_name', $expected_theme );

        if( $given_slug !== $expected_slug ) {
            wp_redirect( $redirect_to );
            exit();
        }
    }

    else if( is_singular('theme' ) ) {
        $given_slug = $wp_query->get( 'rubrique' );
        $expected_rubrique = get_field('theme-attachment', $post->ID);

        if( empty( $given_slug ) || empty( $expected_theme ) ) {
            wp_redirect( $redirect_to );
            exit();
        }

        $expected_slug = get_post_field( 'post_name', $expected_rubrique );

        if( $given_slug !== $expected_slug ) {
            wp_redirect( $redirect_to );
            exit();
        }
    }

}
add_action( 'template_redirect', 'custom_post_redirects' );

最佳答案

“刷新”永久链接。

当添加了创建或影响自定义帖子类型的代码时,通常会通过简单地“刷新”永久链接来解决 404 问题。可以通过访问设置 -> 永久链接手动执行此操作。
https://typerocket.com/flushing-permalinks-in-wordpress/ .

这是一个“昂贵”的操作,因此建议不要包含执行此操作的代码https://codex.wordpress.org/Function_Reference/flush_rewrite_rules ,但只需手动执行一次。

关于php - 自定义帖子类型和页面层次结构 - 错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53573281/

相关文章:

php - Codeigniter 中的图像提交按钮 - 我缺少什么?

php - 存储库模式与 ORM

wordpress - Universal Analytics 中的增强型链接归因不起作用

java - Azure spring应用程序部署,找不到 Controller 404

php - php 中发布日期出现错误

php - 总是无效的用户名有/没有坏词

javascript - JS : Should I use a switch-case statement or an if/else for this kind of situation?

wordpress - 如何在 Wordpress 3 中将自定义字段添加到我的自定义帖子类型?

javascript - 404 和 500 上的 JQuery Mobile “Error Loading Page”

php - App::abort(404) 等同于 Laravel 5?