简体   繁体   English

WordPress - 您可以为页面添加斜杠但没有对帖子使用斜杠吗?

[英]WordPress - Can you have Trailing Slashes for Pages but No Slashes for Posts?

A friend wants his WordPress site to have:一位朋友希望他的 WordPress 网站具有:

By default, WordPress will add the trailing slash for pages and posts.默认情况下,WordPress 将为页面和帖子添加尾部斜杠。 Setting a custom permalink structure to /%postname%.html will add the extension to blog posts , but removes the trailing slash from pages, of course.将自定义永久链接结构设置为/%postname%.html会将扩展名添加到blog posts ,但当然会从页面中删除尾部斜杠。

I'm able to get the product post-type to rewrite with add_permastruct我可以使用 add_permastruct 重写产品后类型

function rr_permastruct_html( $post_type, $args ) {
    // Works fine!
    if ( $post_type === 'product' )
        add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%.html", $args->rewrite );
    
    // Worth a shot, but no...   
    //if ( $post_type === 'page' )
    //    add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%/", $args->rewrite );
}

But when it comes to appending posts with the .html extension and still have pages keep the trailing slash, I keep running into walls.但是当涉及到使用 .html 扩展名附加帖子并且仍然有页面保留斜杠时,我一直在撞墙。

I can't get it to work using .htaccess because it affects both.我无法使用 .htaccess 让它工作,因为它会影响两者。 And my attempts with $wp_rewrite, after a while, have gone nowhere:一段时间后,我对 $wp_rewrite 的尝试无处可去:

function rr_rewrite_page_permalink() {
    global $wp_rewrite;
    if (!strpos($wp_rewrite->get_page_permastruct(), '.html')) {
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
        $wp_rewrite->set_permalink_structure( $wp_rewrite->page_structure );
    }
    $wp_rewrite->flush_rules();
}
add_action('init', 'rr_rewrite_page_permalink', -1);

Permalink plugins produce the same behaviors with no solution.永久链接插件产生相同的行为而没有解决方案。 So, is it even possible to have posts with .html extensions and have pages keep the trailing slash?那么,是否有可能发布带有 .html 扩展名的帖子并让页面保留尾部斜杠?

Thank you谢谢

File names should not end in a trailing slash.文件名不应以斜杠结尾。

If you force a trailing slash on a file name, then that will cause the browser to think it is a folder and will result in a 404 error message.如果您在文件名上强制使用尾部斜杠,则会导致浏览器认为它是一个文件夹并导致 404 错误消息。 Should You Have a Trailing Slash at the End of URLs?你应该在 URL 末尾加一个斜杠吗?

And don't forget并且不要忘记

It can cause problems with duplicate content and crawl efficiency if your pages are accessible with and without a trailing slash.如果您的页面可以使用或不使用尾部斜杠访问,则可能会导致重复内容和抓取效率问题。

Learn more about Permalink for specific page name - WordPress Development Stack Exchange了解有关特定页面名称的永久链接的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM