Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在wordpress中为特定类别的帖子维护单独的永久链接结构_Php_Wordpress - Fatal编程技术网

Php 如何在wordpress中为特定类别的帖子维护单独的永久链接结构

Php 如何在wordpress中为特定类别的帖子维护单独的永久链接结构,php,wordpress,Php,Wordpress,嗨,目前在我们的wordpress中,我们默认的永久链接结构是 abc.com/%postname%-%post\u id%.html 但对于特定类别,我需要不同的permalink结构 例如:对于照片类别,我希望url结构为 abc.com/%post\u id%.html 当前我正在使用以下代码,但它不工作 add_filter( 'post_link', 'custom_permalink', 10, 3 ); function custom_permalink( $permalink,

嗨,目前在我们的wordpress中,我们默认的永久链接结构是

abc.com/%postname%-%post\u id%.html

但对于特定类别,我需要不同的permalink结构 例如:对于照片类别,我希望url结构为

abc.com/%post\u id%.html

当前我正在使用以下代码,但它不工作

add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
    // Get the categories for the post
    $category = get_the_category($post->ID); 
    if (  !empty($category) && $category[0]->cat_name == "Photos" ) {
        $permalink = home_url('/'.$post->ID .'.html' );
    }
    return $permalink;
}

但我没有得到不同的permalink结构的具体类别的职位。请帮助我。

帖子必须有一个非数字的slug,因为slug是唯一的标识符

因此,请将%post_id%与其他文本结合使用。这肯定会奏效


特定类别文章的自定义永久链接url(如abc.com/%post\u id%.html)是不可能的,上面讨论了相同的主题。

每当您尝试更改永久链接设置时,您还需要
刷新重写规则以更新它们(您可以检查数据库以验证这一点)。如果您没有执行
flush\u rewite\u rules()
或转到后端的
permalink
页面并单击
Save
刷新规则,则您编写的代码从未生效。