• 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

Viewing 4 replies - 1 through 4 (of 4 total)
  • replace $post_slug = $post->post_name; with

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

    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;

    Thread Starter Andy

    (@andyt81)

    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]

    + for a ‘the_slug()’ function in the wordpress codex

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get current post slug and display inside query_posts’ is closed to new replies.