WordPress hash posts

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:

Read More

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%

Related posts

Leave a Reply

1 comment

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