'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>multisite - Redirect Single Post CPT (Custom Post Type) to a specific URL
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

multisite - Redirect Single Post CPT (Custom Post Type) to a specific URL

programmeradmin1浏览0评论

I have a domain domain and subdomain materials.domain

At the domain scope, I created a custom post type called resources and a portfolio grid (using Elementor) to display it's featured image with the title.

When clicking on the grid item, the single post is shown for example: domain/resources/resource-title

I'd like to remove the single post from the domain and redirect on click to materials.domain/resource-title. I already have this page on the materialssubdomain).

How can I achieve that?

I have a domain domain and subdomain materials.domain

At the domain scope, I created a custom post type called resources and a portfolio grid (using Elementor) to display it's featured image with the title.

When clicking on the grid item, the single post is shown for example: domain/resources/resource-title

I'd like to remove the single post from the domain and redirect on click to materials.domain/resource-title. I already have this page on the materialssubdomain).

How can I achieve that?

Share Improve this question edited Jul 16, 2020 at 14:15 Fahad 235 bronze badges asked Jul 6, 2020 at 14:34 user1301037user1301037 836 bronze badges 7
  • How are you generating the grid item? Please add that code to your question. – jdm2112 Commented Jul 6, 2020 at 15:08
  • I'm using elementor portfolio widget to display resource archive CPT as grid – user1301037 Commented Jul 6, 2020 at 15:26
  • So you need a redirect so that browsing to domain/resources/resource-title redirects you immediately to materials.domain/resource-title ? – mozboz Commented Jul 6, 2020 at 15:44
  • Yes. If possible I want to disable the single from the CPT at domain and when click on the grid Item, access materials.domain/resource-title (in practice if I can be able to setu up a custom destiny url at domain works too). – user1301037 Commented Jul 6, 2020 at 15:59
  • I read at this thread how redirect the single. Any idea how can I pass a custom url to the function? (maybe using custom fields?) – user1301037 Commented Jul 6, 2020 at 16:10
 |  Show 2 more comments

2 Answers 2

Reset to default 5 +50

Sometimes this would be achievable with add_rewrite_url but it looks like in this case it's not as you need to redirect to a different domain.

In this case you need .htaccess or nginx rules to do this, so you need to add something like this to your nginx config for domain only, in order to rewrite those URLs:

rewrite ^/resources/(.+) https://materials.domain/$1 permanent;

You'll likely need to restart nginx after you add this to the config file.

Happy to help if this doesn't do exactly what you want. There's more examples of nginx rewrite rules here: https://www.thegeekstuff/2017/08/nginx-rewrite-examples/

If you want to do it from WordPress environment itself..

function wpse370481_redirect_url() {
    if ( is_singular( 'resources' ) ) {
        global $post;
    
        $path = $post->post_name;
        
        $redirect_url = 'https://materials.domain/' . $path;
        
        wp_redirect( $redirect_url );
        exit;
            
    }
}
add_action( 'template_redirect', 'wpse370481_redirect_url' );   

Havn't tested this code as I currently don't have a setup to test this, but in theory, the code should work.

发布评论

评论列表(0)

  1. 暂无评论