WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Page slug (5 posts)

  1. brockangelo
    Member
    Posted 6 years ago #

    I am building a CMS and am trying to build a "Department" template. I am designing WordPress so that my Department pages have the same slug as their respective category and link categories.

    In the template I would like to extract the permalink slug of the page so that it can be used with the category and link categories.

    For example, at this page:

    http://localhost/wordpress/dept/human-resources

    I want to be able to extract "human-resources", put it into a variable, and use it with:

    http://localhost/wordpress/category/human-resources
    http://localhost/wordpress/link-category/human-resources

    The permalink_anchor template tag did not turn up any results, and I poured over the template tags. I suppose if this isn't supported by the wp-core I'll just extract using php.

    Anyone know of some other way to do this? It seems like I am missing something obvious.

  2. brockangelo
    Member
    Posted 6 years ago #

    Well shoot, I marked this as resolved before I submitted this and I lost my last post :-(

    Here is what I found:

    PHP has a built in function called "substr". If you enter the following:

    echo substr(get_permalink(),35);

    it will extract data from the string starting at the 35th character. In my case, the blog url + category = 35th character. Voila! I have the slug of the page.

    This is not the cleanest, and I'll have to change it before I take the site live. Anyone have a better idea?

  3. brockangelo
    Member
    Posted 6 years ago #

    Okay - here is the clean solution if you need to get your page or post slug for use with category slugs:

    <?php echo $post->post_name; ?>

    TA-DAAAA! :-)
    I knew there had to be an easier way. This outputs the slug, so you can plug this into a variable called $slug for use all throughout your template code.

  4. thecoup
    Member
    Posted 5 years ago #

    thank you so much for this!

    I wish this was in the documentation somewhere. I searched for slug, posttitle, sanitize, and permalink, over and over again trying to find out how to access this. I guess I should have looked at the tables. But I figured it was a variable I could access.

    thanks again. $post->post_name; is a life saver...

  5. inkywillow
    Member
    Posted 5 years ago #

    Nice one!

    That's saved me some searching, thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic