Forums

How can I get the post slug from a post ID? (8 posts)

  1. GrayHunter
    Member
    Posted 9 months ago #

    How can I get the post slug from a post ID?

    So if I change the slug - WP still knew what slug has the post (with the ID).

  2. Andrew Nevins
    Volunteer Moderator
    Posted 9 months ago #

    Can you define the global $post array and use that?

  3. GrayHunter
    Member
    Posted 9 months ago #

    I have several posts which I show the title of. So I need to use ID of the post each time. Could you give me the code of the topic question pls?

  4. Andrew Nevins
    Volunteer Moderator
    Posted 9 months ago #

    What do you want to accomplish with the slug? Echo it for each post?

  5. GrayHunter
    Member
    Posted 9 months ago #

    Yes:
    I have the post with ID 111 and the post with ID 222. So I need the code which gives me the slug of the post with ID 111 (by ID) and the same with 222.

  6. Andrew Nevins
    Volunteer Moderator
    Posted 9 months ago #

    Could you do something like

    global $post;
    
    if ( $post->ID == 111 && $post->ID == 222 ) { 
    
     echo $post->post_slug;
     // I don't actually know the slug name
    
    }
  7. GrayHunter
    Member
    Posted 9 months ago #

    This is a part of my code (there are several such pieces of different posts):

    <?php
    $post_id_title = get_the_title(1);
    if (is_single($post_id_title)) { ?>
    <li><a href="<?php bloginfo('url'); ?>/#slug" class="selected"><span><?php the_title(); ?></span></a></li>
    <?php } else { ?>
    <li><a href="<?php bloginfo('url'); ?>/#slug"><span><?php echo get_the_title(1); ?></span></a></li>
    <?php } ?>

    Here is "#slug" - I do need to get the slug by ID. The code Andrew Nevins gives me above is difficult to add to this for me...

  8. GrayHunter
    Member
    Posted 9 months ago #

    <?php echo $post->post_slug(1); ?>
    does not work...

    [No bumping, thank you.]

Reply

You must log in to post.

About this Topic

Tags

No tags yet.