Wordpress哈希帖子

时间:2014-12-09 15:39:05

标签: jquery ajax wordpress

我正在使用Ajax Wordpress网站,我在主页内有一些帖子。 当我点击帖子上的显示时,会显示一个带有帖子的弹出窗口,链接会更改为example.com/#post-id,其中id是帖子ID。还行吧。

我想当有人在example.com/#post-3上加入id为3的帖子时,我也是这样做的。但是我看到了一个问题:

永久链接我不能把#。

我的问题是如何将我的固定链接转换为#的链接或如何更改其他方式?哪个是更好的选择?

由于

编辑:

我目前的永久链接是:

/后%POST_ID% 我想把它变成 /#post-%post_id%

1 个答案:

答案 0 :(得分:0)

如果您只想在the_permalink()调用的链接上更改此内容,则可以对其进行过滤。使用您的示例

add_filter('the_permalink', 'addHashToPermalink', 10);
function addHashToPermalink($permalink) {
    $base       = get_bloginfo('url'). "/post-";
    $replace    = get_bloginfo('url'). "/#post-";
    $permalink  = str_replace($base, $replace, $permalink);
    return $permalink;
}

这只能直接在the_permalink()上使用,并且不会反映在导航中(菜单,下一个/上一个等)。您可以使用post_link过滤器将更改应用于帖子的所有永久链接。但是,这不适用于页面,附件或自定义帖子类型。

add_filter('post_link', 'addHashToPermalink', 10);
function addHashToPermalink($permalink) {
    if(is_admin()) return $permalink; // only apply the changes on the front end
    // add more conditionals to fine tune where the changes should apply
    $base       = get_bloginfo('url') . "/post-";
    $replace    = get_bloginfo('url') . "/#post-";
    $permalink  = str_replace($base, $replace, $permalink);
    return $permalink;
}