Forums

[resolved] get_permalink to output just the path? (3 posts)

  1. cathlaura
    Member
    Posted 4 months ago #

    Hi, I'm editing my search.php file and I'd like to display as part of the search results the URL of each result since our site hierarchy is a little crazy but would be informative for our users. But I'd like it to just print
    "/resources/library/kids" instead of "http://www.mydomain.org/resources/library/kids".

    I saw someone else use str_replace on get_permalink for their issue, so I guess I should be trying to change the permalink string somehow - I don't know PHP but I sure can copy and paste. This will only be my own site search, so the part I want to remove will always be the same thing, same length...if that helps.

  2. Pratik Talati
    Member
    Posted 4 months ago #

    Try doing following

    // specify id if you are using outside post loop
    // otherwise keep it as get_permalink()
    $permalink = get_permalink( $id );
    
    // printing output
    echo str_replace( get_bloginfo( 'url' ) . '/' , '', $permalink );

    See if this helps!

  3. cathlaura
    Member
    Posted 3 months ago #

    Cool. A friend commended your solution as elegant. I played with what you gave me a bit a finally settled on this:

    Posted in <?php // printing output
    echo str_replace( get_bloginfo( 'url' ) , '...', get_permalink( ) );
    ?>

    Thanks a bunch.

Reply

You must log in to post.

About this Topic