【发布时间】:2017-04-07 19:21:52
【问题描述】:
在 WordPress 中,我使用 Jetpack 的投资组合自定义内容类型,但想将 slug 从“投资组合”更改为“示例”。我找到了这个关于如何做的例子(http://www.markwarddesign.com/2014/02/remove-custom-post-type-slug-permalink/)和这个插件(https://github.com/devinsays/no-slug-portfolio-post-types)。两者都基于 Jetpack 团队链接到的 Wordpress VIP 上的帖子,现在有一个死链接。
这是我的代码,由于某种原因它不起作用。我已通过转到“设置”>“永久链接”并点击“保存更改”来刷新我的永久链接。
/**
* Remove the slug from custom post type permalinks.
*/
function vipx_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( ! in_array( $post->post_type, array( 'portfolio' ) ) || 'publish' != $post->post_status )
return $post_link;
$post_link = str_replace( '/' . $post->post_type . '/', '/examples', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'vipx_remove_cpt_slug', 10, 3 );
/**
* Some hackery to have WordPress match postname to any of our public post types
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* Typically core only accounts for posts and pages where the slug is /post-name/
*/
function vipx_parse_request_tricksy( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) )
$query->set( 'post_type', array( 'post', 'portfolio', 'page' ) );
}
add_action( 'pre_get_posts', 'vipx_parse_request_tricksy' );
任何想法如何让这段代码再次工作?
【问题讨论】:
-
已继续并正在使用此解决方案:stackoverflow.com/questions/24383154/…
标签: php wordpress custom-post-type slug