How to display posts by tag
-
Hi everyone,
I am a complete noob, I was wondering if someone can please help me out. I would like to display posts based on a specific tag, such as ‘economy’, any ideas? thanks a lot for your help in advance.
-
If you add the Template Tag, the_tags(), to your theme, or add the Template Tag, wp_tag_cloud() to your sidebar.php, or add the Tag Cloud Widget to your sidebar, then you be presented with links that take you to a ‘view’ of your tag archives. Those tag archive ‘views’ could then be customized as described in Tag Templates. Also see Template Hierarchy to understand what WordPress uses to render posts in different situations.
To see the_tags in action, switch to the WordPress Default theme if your theme doesn’t offer that.
thanks for the reply, I’m not very sure to be honest how to do it…I have tags of the post showing on single pages, so the_tags function is there on single pages.
I was hoping on the category page such as “Technology”, to add on the sidebar a widget that displays posts (such as the latest 5 posts) from a specific tag such as “Google”.
I know how to add the widget + conditional tags for the pages in order for the widget to show up, I just have no idea what the php code is to show the tags.
I’ve tried this:
<?php
query_posts(‘tag=economy’);
?>but that doesn’t seem to do nothing (no errors, no nothing)
thanks!
This:
<?php $args=array( 'tag' => 'tag1', 'showposts'=>5, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { echo '5 recent Posts with tag1'; while ($my_query->have_posts()) : $my_query->the_post(); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php endwhile; } //if ($my_query) wp_reset_query(); // Restore global post data stomped by the_post(). ?>Also this plugin gives a widget to do that:
http://wordpress.org/extend/plugins/query-posts/See query_posts() for more arguments.
thanks a lot, it works perfectly! thanks for pointing me to that plugin as well, it seems great, but it doesn’t look like I can use it because it requires WP 2.8+
I just have one more question… this is the code I have so far:
<?php ((‘dynamic_sidebar’) && dynamic_sidebar(3)); ?>
<div class=”widget”>
<?php
$args=array(
‘tag’ => ‘google’,
‘showposts’=>4,
‘caller_get_posts’=>1
);$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo ‘<h2>Google</h2>’;
while ($my_query->have_posts()) : $my_query->the_post(); ?><?php
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
?></div>
<?php ?>Note: from the first code you provided, I changed the < p> to
< ul>… I’m trying to get it to style the posts as my sidebar in a list…but for some reason that is not working….my question: how can I get the widget to only display when it is the category page? I got the tag which is: if is_category(‘200’) , but I have no idea where to include it in the code above…if you can please point it out to me that would be awesome.
Thanks so much for your help, greatly appreciated, really.
UPDATE: I got the styling of the list complete, just wondering now where to put the code if is_category(‘200’) in order to show the widget only when that category page is active. thanks again.
In the code above:
<?php ((‘dynamic_sidebar’) && dynamic_sidebar(3)); ?>
I tried changing it to:
<?php if is_category(‘200’) ((‘dynamic_sidebar’) && dynamic_sidebar(3)); ?>
but unfortunately it doesn’t work…any ideas?
@michael, thx very much for all of your help!
any ideas? 🙂
Thanks, the only problem is that the part of my sidebar is not widgitized, so I must add the code manually…. not really sure where to put it 🙁
Hmm wonder if this works:
<?php if (is_category(‘200’)) {
((‘dynamic_sidebar’) && dynamic_sidebar(3));
}
?>unfortunately that does not work, the widget still show on every page.
The topic ‘How to display posts by tag’ is closed to new replies.