weixin_33711641 2014-12-09 15:39 采纳率: 0%
浏览 13

WordPress的哈希帖子

I'm working on a Ajax Wordpress website and I have some posts inside homepage. When I click Show on a post a popup with the post appears and the link changes to example.com/#post-id where id is post ID. This is ok.

I want when someone goes on example.com/#post-3 to accest the post with id 3. I did that too. But I saw a problem:

On permalinks I can't put #.

My question is how can I transform my permalink into a link with # or how can I change the other way? Which is the better option?

Thanks

EDIT:

My current permalink is this:

/post-%post_id% and I want to make it into /#post-%post_id%

  • 写回答

1条回答 默认 最新

  • hurriedly% 2014-12-09 18:04
    关注

    If you only want to change this on links called by the_permalink() you can filter it. Using your example

    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;
    }
    

    This will only work directly on the_permalink(), and won't reflect in navigation (menus, next/previous, etc.). You can apply the changes to all permalinks for posts by using the post_link filter. This will not work on pages, attachments or custom post types, however.

    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;
    }
    
    评论

报告相同问题?