Forums

How to properly echo post-slug/post-name and/or Page Permalink Title? (11 posts)

  1. jonokane
    Member
    Posted 1 year ago #

    Hello all,
    I have wasted serious time on this issue and hoping a real expert can clear this up. I have a wordpress site, which has different pages. I want to be able to echo the 'post-name' (or slug?) in the page, so that I can use that as a an ID in my markup for CSS purposes. So, here's an example:

    I have a page called "mailing-list". Because I am using the "pretty" formatting for urls, the URL is mywebsite.com/mailing-list. In the markup, I want the final output to look like this: "<div id="mailing-list">. I have tried the following:
    - <?php wp_title('', 'true', ''); ?> gives me "Mailing List". Nope.
    - <?php the_title( '', '' ); ?> gives me "Mailing List". No.
    - <?php the_ID(); ?> gives me the post ID. I could use this, but would rather use real language here.

    I am wondering if I'm just experiencing a nomenclature issue here.

    Because I'm using it for an ID or class in my markup, I need it to not have spaces, just like the post-name in the permalink. I hope this makes sense. I am guessing there is an easy/obvious way of doing this? Please help!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try <?php echo $post->post_name;?> within the Loop.

  3. jonokane
    Member
    Posted 1 year ago #

    Aha! Thanks so much for your super-fast response, esmi. This solution almost works, however, on the page where I have the actual blog, it (understandably) echos the most recent post, instead of the actual title of the page. Is there something similar for the page slug itself?

    Thanks!

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    In that case, try <?php echo $wp_query->pagename;?> before the Loop.

  5. jonokane
    Member
    Posted 1 year ago #

    Hmm - no errors, but I'm not getting anything back with that. I wonder what I'm missing?

  6. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Is this your site's main posts page or another archive (post listing) page?

  7. jonokane
    Member
    Posted 1 year ago #

    It's the site's main posts page. When I do <?php echo $post->post_name;?> it works for all the other pages, other than the posts page.

    On my site, just to test this functionality, I'm echoing the results to the top of the page. You can see the two in action here:
    A page where this works great: http://truewinter.com/mailing-list
    The main posts page where it's displaying the post instead of the page: http://truewinter.com/news

  8. jonokane
    Member
    Posted 1 year ago #

    It is surprising to me how difficult/undocumented this is. Is this not a normal feature/pattern? I am wondering if there are other methods to give pages unique identifiers for id's or classes?

  9. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    get_option('page_for_posts ') should give you the id of the main post page. Once you have that, you can use get_page to grab all the details of the current posts page.

    http://codex.wordpress.org/Function_Reference/get_option
    http://codex.wordpress.org/Option_Reference

  10. jonokane
    Member
    Posted 1 year ago #

    Thanks, esmi - I really appreciate your help! I'll look in to the get_option. Cheers!

  11. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    No problem.

Topic Closed

This topic has been closed to new replies.

About this Topic