Php Wordpress-页面链接过滤器
我正在尝试更改默认页面的post类型permalink,我想在其末尾添加一个“.php”,例如:Php Wordpress-页面链接过滤器,php,wordpress,permalinks,Php,Wordpress,Permalinks,我正在尝试更改默认页面的post类型permalink,我想在其末尾添加一个“.php”,例如:home\u url()。邮政署名称。php' 我不能使用WordPress permalinks页面,因为我已经在这样使用它:/%category%/%postname%.php,这不会影响默认的页面发布类型 所以我就这样做了: function wp_pages_permalink( $permalink, $post_id ) { if ( empty( $post_id ) ) ret
home\u url()。邮政署名称。php'
我不能使用WordPress permalinks页面,因为我已经在这样使用它:/%category%/%postname%.php
,这不会影响默认的页面发布类型
所以我就这样做了:
function wp_pages_permalink( $permalink, $post_id ) {
if ( empty( $post_id ) ) return $permalink;
$post = get_post( $post_id );
return home_url( $post->post_name . '.php' );
}
add_filter( 'page_link', 'wp_pages_permalink', 10, 2 );
然而,当我尝试访问该页面时,我得到了404,并且我已经通过访问永久链接页面刷新了重写规则,所以您只是更改了链接的输出 带着你想要的地址 但实际页面不会被更改 我认为你应该瞄准$permalink_结构,而不仅仅是$permalink, 尝试使用类似以下内容:
function wp_pages_permalink( $permalink_structure, $post_id ) {
if ( empty( $post_id ) ) return $permalink_structure ;
$post = get_post( $post_id );
return ( $permalink_structure . '.php' );
}
add_filter( 'page_link', 'wp_pages_permalink', 10, 2 );
我根据您的代码进行了更改,因此您可能需要对其进行操作
无论如何,如果您想检查permalinks会发生什么,请查看文档()尝试执行以下步骤:
/%post\u id%/%postname%.php
$this->page_structure = $this->root . '%pagename%.php';
正如所怀疑的,这需要一个重写规则。。我不知道为什么post_链接筛选器不需要添加重写规则
function ba_rewrite() {
add_rewrite_rule('^([^/]*)?.php', 'index.php?pagename=$matches[1]', 'top');
}
add_action( 'init', 'ba_rewrite' );
不,当然我不会替换wp includes文件夹中的一行。我已经回答了我的问题,从未尝试编辑wp content文件夹之外的文件。那么,你对我的代码做了什么?您更改了变量名?现在应该可以用了吧?我不这么认为。不,我不知道你的项目中的代码是否有效。。但是在WP_rewrite类中,我看到$permalink_结构方法是permalink的制作方法,直接访问它可能不是一个好方法。。。