• Resolved ciaravino

    (@ciaravino)


    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 🙁

Viewing 7 replies - 1 through 7 (of 7 total)
  • <?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; ?>
    </ul>
    Thread Starter ciaravino

    (@ciaravino)

    Thanks for replying, I have no idea how to accomplish this 🙁 I tried your code, but how I’m doing it doesn’t work. Here’s how I’m using it:

    <article>
    	<div class="blog-article">
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<section>
    			<div class="post" id="post-<?php the_ID(); ?>">
    				<header>
    					<h1 id="page-list-header">
    						<?php the_title(); ?>
    					</h1>
    				</header>
    			</div>
    		</section>
    		<section>
    			<?php the_content(); ?>
    		</section>
    		<?php endwhile; endif; ?>
    
    		<?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; ?>
    		</ul>

    Nothing shows up for that area of code.

    Try moving the new code to just before <?php endwhile; endif; ?>

    Thread Starter ciaravino

    (@ciaravino)

    That didn’t change anything on the page (nothing was added or subtracted).

    When I use the_title() instead of wp_title(”,false,”) it displays the 5 most recent posts (but they aren’t filtered by tag).

    Also, when I echo $myposts, the only thing that gets displayed is the word “Array”. Should it also have each post as an array item at that point?

    Here’s the code:

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

    The only thing that is displaying is the word “Array”.

    Thread Starter ciaravino

    (@ciaravino)

    I think the problem may be that get_posts(tag=) gets the tag’s slug, so I need to get the page’s slug, not the title? I don’t know how to get the page’s slug, but I’m looking.

    Try changing:

    <?php $page_title = wp_title('',false,''); ?>

    to:

    <?php $page_title = the_title('',false,''); ?>

    Thread Starter ciaravino

    (@ciaravino)

    I tried that, it didn’t work :(. I found the solution though.

    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.

    Thanks :D!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Pulling recent posts depending on the tag and current page’s title’ is closed to new replies.