WordPress 永久链接:删除标签库并添加 .html 扩展名

发布于 2024-10-19 19:02:16 字数 2403 浏览 1 评论 0原文

我的问题:

我的永久链接设置:

常规设置>定制> /%postname%.html

可选设置>标签库>标签


我想要这样的:mysite.com/%tag-slug%.html

谢谢


为@Dimitry编辑的

正是我想要的

<?php
/*
Plugin Name: Custom tag URLs
Description: Appends .html to tag links
*/

// applied when calling get_tag_link()
add_filter('tag_link', 'my_tag_link', 10, 2);

/**
 * Returns a link to a tag. Instead of /tag/tag-name/ returns /tag-name.html
 */
function my_tag_link($tag_link, $tag_id) {
    $tag_base = get_option('tag_base');
    if ($tag_base) {
        // problem. returning: http://www.domain.com/post-tag/tag-name
        //$tag_link = preg_replace('@^' . preg_quote($tag_base, '@') . '@', '', $tag_link);

        // I added it. Result: http://www.domain.com/tag-name
        $tag_link = str_replace("$tag_base/", "", preg_replace('@^' . preg_quote($tag_base, '@') . '@', '', $tag_link)); 
        //echo "$tag_link<br>";
    }
    // problem. returning: http://www.domain.com/http://www.domain.com/tag-name.html
    //return '/' . trim($tag_link, '/') . '.html';

    // I added it. Result: http://www.domain.com/tag-name.html , 
    return trim($tag_link, '/') . '.html';

}
?>

我做了永久链接设置

常规设置>定制> /post-%postname%.html 可选设置>标签库> post-tag

.htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# Rewrites /tag-name.html as /post-tag/tag-name
# Assuming that your tag_base option is blank or set to 'post-tag'
RewriteCond %{REQUEST_URI} !^/post-.*
RewriteRule ^/?([^/]*)\.html$ /post-tag/$1

单页:http://www .domain.com/post-hello-world.html >>>正在工作 标签页:http://www.domain.com/tag-name.html > ;>未找到 404 http://www.domain.com/post-tag/tag-name >>>工作

问题:

找不到标签页

抱歉。我是初学者。谢谢迪米特里

My question:

My permalink settings:

General settings> Custom > /%postname%.html

Optional settings> Tag base > tag


I want to like this: mysite.com/%tag-slug%.html

Thanks


Edited for @Dimitry

was exactly the way I want

<?php
/*
Plugin Name: Custom tag URLs
Description: Appends .html to tag links
*/

// applied when calling get_tag_link()
add_filter('tag_link', 'my_tag_link', 10, 2);

/**
 * Returns a link to a tag. Instead of /tag/tag-name/ returns /tag-name.html
 */
function my_tag_link($tag_link, $tag_id) {
    $tag_base = get_option('tag_base');
    if ($tag_base) {
        // problem. returning: http://www.domain.com/post-tag/tag-name
        //$tag_link = preg_replace('@^' . preg_quote($tag_base, '@') . '@', '', $tag_link);

        // I added it. Result: http://www.domain.com/tag-name
        $tag_link = str_replace("$tag_base/", "", preg_replace('@^' . preg_quote($tag_base, '@') . '@', '', $tag_link)); 
        //echo "$tag_link<br>";
    }
    // problem. returning: http://www.domain.com/http://www.domain.com/tag-name.html
    //return '/' . trim($tag_link, '/') . '.html';

    // I added it. Result: http://www.domain.com/tag-name.html , 
    return trim($tag_link, '/') . '.html';

}
?>

I did the permalink settings

General settings> Custom > /post-%postname%.html
Optional settings> Tag base > post-tag

.htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# Rewrites /tag-name.html as /post-tag/tag-name
# Assuming that your tag_base option is blank or set to 'post-tag'
RewriteCond %{REQUEST_URI} !^/post-.*
RewriteRule ^/?([^/]*)\.html$ /post-tag/$1

Single pages: http://www.domain.com/post-hello-world.html >> is working
Tag Pages : http://www.domain.com/tag-name.html >> Not found 404
http://www.domain.com/post-tag/tag-name >> working

Problem:

Tag pages can not be found

I'm sorry. I am beginner. Thank you Dimitry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的呼吸 2024-10-26 19:02:16

我不确定您可以通过管理界面执行任何操作,但您可以添加一些自己的代码作为插件(在 wp-content/plugins/tag_links.php 内):

<?php
/*
Plugin Name: Custom tag URLs
Description: Appends .html to tag links
*/

// applied when calling get_tag_link()
add_filter('tag_link', 'my_tag_link', 10, 2);

/**
 * Returns a link to a tag. Instead of /tag/tag-name/ returns /tag-name.html
 */
function my_tag_link($tag_link, $tag_id) {
    $tag_base = get_option('tag_base');
    if ($tag_base) {
        $tag_link = preg_replace('@^' . preg_quote($tag_base, '@') . '@', '', $tag_link); 
    }
    return '/' . trim($tag_link, '/') . '.html';
}

并在上面的 .htaccess 文件中其他重写规则,请执行以下操作:

# Rewrites /tag-name.html as /post-tag/tag-name
# Assuming that your tag_base option is blank or set to 'post-tag'
RewriteCond %{REQUEST_URI} !^/post-.*
RewriteRule ^/?([^/]*)\.html$ /post-tag/$1

您的帖子 URL 结构将是 post-%postname%.html。

所有这些都表明您的标签和帖子在 URL 结构中必须至少具有一项区分功能。在本例中,它是帖子 URL 的“post-”前缀。

您正在构建 Wiki 吗?

I'm not sure there is anything you can do through the admin interface, but you can add a little of your own code as a plugin (inside wp-content/plugins/tag_links.php):

<?php
/*
Plugin Name: Custom tag URLs
Description: Appends .html to tag links
*/

// applied when calling get_tag_link()
add_filter('tag_link', 'my_tag_link', 10, 2);

/**
 * Returns a link to a tag. Instead of /tag/tag-name/ returns /tag-name.html
 */
function my_tag_link($tag_link, $tag_id) {
    $tag_base = get_option('tag_base');
    if ($tag_base) {
        $tag_link = preg_replace('@^' . preg_quote($tag_base, '@') . '@', '', $tag_link); 
    }
    return '/' . trim($tag_link, '/') . '.html';
}

And in your .htaccess file, above other rewrite rules, do something like:

# Rewrites /tag-name.html as /post-tag/tag-name
# Assuming that your tag_base option is blank or set to 'post-tag'
RewriteCond %{REQUEST_URI} !^/post-.*
RewriteRule ^/?([^/]*)\.html$ /post-tag/$1

And your post URL structure would be post-%postname%.html.

All this to say that your tags and posts must have at least one differentiating feature in the URL structure. In this case, it's the 'post-' prefix for post URLs.

Are you building a Wiki?

缘字诀 2024-10-26 19:02:16

如果我错了,请纠正我,但我认为由于内容重复而无法做到这一点。

Correct me if im wrong, but I think that can NOT be done because of duplicate content.

孤芳又自赏 2024-10-26 19:02:16

您好,试试这个http://wordpress.org/plugins/import-html-pages/

将格式正确的静态 HTML 文件导入 WordPress。需要 PHP 5。

该插件将导入文件目录作为页面或帖子。您可以指定包含要导入的内容的 HTML 标记(例如 、 或 )或 Dreamweaver 模板区域(例如“主要内容”)。

如果导入页面,目录层次结构将被保留。包含指定文件类型的目录将作为空父页面导入(或者,如果存在索引文件,则其内容将用于父页面)。不包含指定文件类型的目录将被忽略。

导入文件时,将显示生成的 ID、永久链接和标题。完成后,导入器将提供可在 .htaccess 文件中使用的 Apache 重定向列表,以将访问者从旧文件位置无缝转移到新的 WordPress 永久链接。从 2.0 开始,如果您在导入文件后更改永久链接结构,则可以重新生成重定向 - 文件的旧 URL 将作为自定义字段存储在导入的帖子中。

Hi Try this http://wordpress.org/plugins/import-html-pages/

mports well-formed static HTML files into WordPress. Requires PHP 5.

This plugin will import a directory of files as either pages or posts. You may specify the HTML tag (e.g. , , or ) or Dreamweaver template region (e.g. 'Main Content') containing the content you want to import.

If importing pages, the directory hierarchy will be preserved. Directories containing the specified file types will be imported as empty parent pages (or, if an index file is present, its contents will be used for the parent page). Directories that do not contain the specified file types will be ignored.

As files are imported, the resulting IDs, permalinks, and titles will be displayed. On completion, the importer will provide a list of Apache redirects that can be used in your .htaccess file to seamlessly transfer visitors from the old file locations to the new WordPress permalinks. As of 2.0, if you change your permalink structure after you've imported your files, you can regenerate the redirects—the file's old URL is stored as a custom field in the imported post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文