WordPress.org

Ready to get started?Download WordPress

Forums

Get current post slug and display inside query_posts (5 posts)

  1. Andy
    Member
    Posted 3 years ago #

    Hey guys

    Been searching the forums for hours but still no luck with the problem I am having.

    Basically I use a query to show all the posts on my site that use the same tag as the current post title in my single.php page.

    I started out using this particular example where I manually type in the post slug and it works:

    <?php query_posts('tag=jambaroo-action-park');?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    But it does not help me use the current post title so I added the_slug hack to my functions.php and tried this with no luck:

    <?php query_posts('tag=' . echo the_slug() . '');?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    I kept up my search and found this but once again no luck:

    <?php $post_slug = $post->post_name;
    query_posts( 'tag=' . $post_slug . '');
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    I anyone has any idea how to do this I would be most appreciative

    Thanks in advance

    Andy

  2. chinmoy
    Member
    Posted 3 years ago #

    replace $post_slug = $post->post_name; with

    $post_slug = str_replace("-", "+",$post->post_name);

  3. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    where are you running your code?

    it might be in an area of the template, where the original querystring is already disturbed, maybe through plugins or other queries.

    try to add wp_reset_query(); before your lines;
    you could also echo the post slug to see if it contains what you think:
    after the first line add: echo $post_slug;

  4. Andy
    Member
    Posted 3 years ago #

    Hey guys thanks for the responses

    I am running the code on single.php (the default post template)

    I have gone through and deleted any other custom code and still not having any luck

    I replaced $post_slug = $post->post_name; with $post_slug = str_replace("-", "+",$post->post_name); but no luck

    I then tried this with and without wp_reset_query();

    <?php $post_slug = str_replace("-", "+",$post->post_name);
    query_posts( 'tag=' . $post_slug . '');
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    [Moderator note: snipped code block, please use the pastebin as per the Forum Guidelines]

  5. Naoise Golden Santos
    Member
    Posted 2 years ago #

    + for a 'the_slug()' function in the wordpress codex

Topic Closed

This topic has been closed to new replies.

About this Topic