-2
我想將WordPress組合項目的URL更改爲普通URL。像http://example.com/portfolio-item/example
那樣爲http://example.com/example
。如何在wordpress網頁中顯示另一個網址?
我想將WordPress組合項目的URL更改爲普通URL。像http://example.com/portfolio-item/example
那樣爲http://example.com/example
。如何在wordpress網頁中顯示另一個網址?
首先,我們將從永久
function portfolio_remove_slug($post_link, $post, $leavename) {
if ('portfolio' != $post->post_type || 'publish' != $post->post_status) {
return $post_link;
}
$post_link = str_replace('/' . $post->post_type . '/', '/', $post_link);
return $post_link;
}
add_filter('post_type_link', 'portfolio_remove_slug', 10, 3);
取出塞只刪除蛞蝓是不夠的。現在,你會得到一個404頁面,因爲WordPress只希望帖子和頁面以這種方式行事。您還需要添加以下內容:
function portfolio_parse_request($query) {
if (! $query->is_main_query() || 2 != count($query->query) || ! isset($query->query['page'])) {
return;
}
if (! empty($query->query['name'])) {
$query->set('post_type', array('post', 'portfolio', 'page'));
}
}
add_action('pre_get_posts', 'portfolio_parse_request');
只要改變「組合」到您的自定義後的類型。您可能需要刷新您的固定鏈接。