The MU forums have moved to WordPress.org

Pulling recent posts depending on the tag and current page's title (5 posts)

  1. ciaravino
    Member
    Posted 13 years ago #

    I'm back :D

    Say I have a page that's about a video game and I want to pull the 5 most recent posts from the blog that have the same tag as the name of the page. For instance, if the page is called World of Warcraft, I want to pull the 5 most recent posts from the blog that have the tag World of Warcraft. Anyone have a clue as to how to accomplish this?

    So far, I'm using <?php wp_get_archives('title_li=&type=postbypost&limit=5'); ?> to get the recent posts, but I don't know how to filter out the ones with the same tag as the page's title :(

    Maybe there will be something like this:

    ****this gets the tag of the post I think****
    $current_tag = single_tag_title("", false);
    ****if post tag is the same as current page's title****
    if ($current_tag == the_title()) {
    wp_get_archives('title_li=&type=postbypost&limit=5');
    }

    Something like that. I don't really know :( I need a way to get a list of posts and only filter out the ones that have a tag that matches the title of the current page :(

  2. dsader
    Member
    Posted 13 years ago #

  3. ciaravino
    Member
    Posted 13 years ago #

    I've read all of those, but I haven't figured it out. Here are some of the things I'm trying:

    $slug = the_title();
    if (is_tag($slug)) {
         <!--I still don't know how to get all of the posts from the tag archive with the tag of $slug-->
    }

    Would that work though, if I knew how to get the 5 most recent posts from the $slug archive?

    I also tried:

    $slug = the_title();
    $my_query = new WP_Query('tag_name=$slug'); ?>
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
      	<?php wp_get_archives('title_li=&type=postbypost&limit=5'); ?>
    <?php endwhile; ?>

    But I probably messed it up too much. I don't think there is even a such thing as tag_name. I was trying to do it like this example but change some of the stuff:

    <?php $my_query = new WP_Query('category_name=special_cat&posts_per_page=10'); ?>
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <!-- Do special_cat stuff... -->
    <?php endwhile; ?>
  4. ciaravino
    Member
    Posted 13 years ago #

    So far I'm at this point:

    <?php $page_title = wp_title('',false,''); ?>
    <ul>
    <?php
    global $post;
    $myposts = get_posts('posts_per_page=5&tag=' . $page_title);
    foreach($myposts as $post) :
    	setup_postdata($post);
    ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>

    A moderator on the normal WordPress forums gave me this. This current code doesn't display anything. I've tried echoing $myposts just to see what's in it and the only thing that comes up is the word "Array", so I'm guessing at some point the array isn't being filled? I've also tried replacing wp_title('',false,''); with the_title(); but it just displays the 5 most recent posts (not filtered by tag).

  5. ciaravino
    Member
    Posted 13 years ago #

    Okay, I got it. get_posts(tag=) gets the tag's slug, not the actual tag. So, I needed to get the page's slug, not the page's title.

    Instead of wp_title() I use basename(get_permalink()); to get the page's slug.

About this Topic

  • Started 13 years ago by ciaravino
  • Latest reply from ciaravino