将自定义分类术语存档重定向到页面

发布于 2025-01-10 04:09:01 字数 545 浏览 0 评论 0原文

我希望将我的条款模板重定向到带有保存段的页面(以允许客户端自定义页面并使用此页面的模板模型),这是我的代码,

function redirect_archive_term() {
   
    $categories = get_terms( array('taxonomy' => 'chapters') );

    foreach($categories as $category) {
    
        if( is_post_type_archive( $category->slug ) ) { // like chapter-1, chapter-2 ...
            wp_redirect( home_url( '/'.$category->slug ), 301 );
            exit();
        }
    }

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

由于我认为的条件,这不起作用一种针对他们的方法?

感谢您的帮助:)

I wish redirect my terms template to a page with the save slug (to allow the client to custom the page and use a template model for this page), this is my code

function redirect_archive_term() {
   
    $categories = get_terms( array('taxonomy' => 'chapters') );

    foreach($categories as $category) {
    
        if( is_post_type_archive( $category->slug ) ) { // like chapter-1, chapter-2 ...
            wp_redirect( home_url( '/'.$category->slug ), 301 );
            exit();
        }
    }

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

this is doesn't work because of the condition I think, there a way to target them ?

Thanks for the help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2025-01-17 04:09:01

应该很简单。以下未经测试。

函数必须在部署标头之前触发,因此使用 wp钩子。

is_tax() 可以在搜索或存档页面上触发,我们需要将其过滤掉,因此 ! is_search() && ! is_archive()。

我们从 get_term() 获取当前术语,并且我们通过 wp_safe_redirect()

<?php

/**
 * Redirect term page to the corresponding custom slug page.
 * 
 * @see https://stackoverflow.com/questions/71266569/redirect-custom-taxonomy-terms-archive-to-page
 * 
 * @since   1.0.0
 * 
 * @param   void
 * @return  void
 */
add_action( 'wp', 'wpso_71266569' );  //hook into wp's init action hook

if ( ! function_exists( 'wpso_71266569' ) ) {
    
    function wpso_71266569() {
    
        //Let's make sure we're on a taxonomy "chapters" page:
        //Either taxonomy-{taxonomy}.php or taxonomy-{taxonomy}-{term}.php.
        if ( ! is_search() && ! is_archive() && is_tax( 'chapters' ) ) {
    
            $term = get_term( get_queried_object_id() ); //Retrieve the current term slug.
    
            wp_safe_redirect( home_url( trailingslashit( $term->slug ) ) ); //eg: Redirect to https://example.com/term_slug/.
    
        };
    
    };
    
};

Should be pretty straight forward. The following is untested.

Function has to fire before the header is deployed, thus the use of the wp hook.

is_tax() can fire on search or archive page, we need to filter that out, thus ! is_search() && ! is_archive().

We get the current term from get_term() and we redirect through wp_safe_redirect().

<?php

/**
 * Redirect term page to the corresponding custom slug page.
 * 
 * @see https://stackoverflow.com/questions/71266569/redirect-custom-taxonomy-terms-archive-to-page
 * 
 * @since   1.0.0
 * 
 * @param   void
 * @return  void
 */
add_action( 'wp', 'wpso_71266569' );  //hook into wp's init action hook

if ( ! function_exists( 'wpso_71266569' ) ) {
    
    function wpso_71266569() {
    
        //Let's make sure we're on a taxonomy "chapters" page:
        //Either taxonomy-{taxonomy}.php or taxonomy-{taxonomy}-{term}.php.
        if ( ! is_search() && ! is_archive() && is_tax( 'chapters' ) ) {
    
            $term = get_term( get_queried_object_id() ); //Retrieve the current term slug.
    
            wp_safe_redirect( home_url( trailingslashit( $term->slug ) ) ); //eg: Redirect to https://example.com/term_slug/.
    
        };
    
    };
    
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文