lists all the posts of two categories
-
Can I list all the posts of category A and caterory B (or, equivalently, tag A and tag B) in the same page?
Maybe I could use this plugin http://wordpress.org/extend/plugins/wp-category-meta/
but I wonder if there is something simpler.
Thanks.
-
Example of simple loop using category__in to return posts for category ids 4,6, and 30 (or use tag__in):
<?php $args=array( 'category__in' => array(4,6,30), 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1, 'caller_get_posts'=> 1 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { echo 'List of Posts'; 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; } wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Related:
How do I determine a Post, Page, Category, Tag, Link, Link Category, or User ID?
Stepping Into Template Tags
Stepping Into Templates
Template Hierarchy
query_posts()Thanks for your reply. Forgive me, I am a quasi-newbie.
I have quickly read the documentation you linked.
Can you tell me in which template I have to insert your php code? Or (I suppose equivalently) which is the template which displays all the posts from a certain category or from a certain tag?
Thanks.That’s this link:
Template HierarchyI have made some progress but I have two problems yet.
What I have made:
a) copied the template file category.php as page-mytemp.php
b) create a page mytemp.php
c) edited page-mytemp.php template inserting your code, after changing posts_per_page from -1 to 5
Now, when I ask for mytemp page, I see the list of the last 5 posts in my categories, but:
1) the whole post contents are displayes, even if I have inserted the <!–more–> tag
2) the [next posts] and [previous posts] links at the bottom are not displayed.
Notice that if I ask for a category, the teaser is respected and the navigation links are displayed.
Maybe there there any other parameters to define $args?
Thanks for your patience.Update: added the following lines to template but without success. 🙁
global $more;
// set $more to 0 in order to only get the first part of the post
$more = 0;1. Put that
global $more;
$more=0;
right before yourthe_content();
global $more; $more = 0; the_content('Read more...');
2. I’ve never quite gotten pagination to work on a Page.
1. Solved. Thank you.
2. Do you mean it’s impossible? I do not understand why category.php template displays navigation links and page-mytemp.php, which is identical apart the lines before
<?php while (have_posts()) : the_post(); ?>
doesn’t.
I have seen that function get_next_posts_link in link-template.php is called, but my limited knowledge does not allow me to understand the rest.Ok, I understand that if the [previous posts] link recalls the same page the query is re-executed …
I’m asking if the “Page” approach is suitable. Is any hack of category template possible?Maybe a suggestion.
If I modify index.php for Home Page, adding
<?php query_posts($query_string . '&cat=-3,-8'); ?>
line as made in
http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category,
navigation buttons are displayed.
So, is there any method to create a page which acts as Home Page?
In other words, can I have many different home pages?The page approach is fine but for the ‘pagination’ 😉 Though I’ve never tried a plugin like wp-pagenavi…
Ok, I have verified that the “hack” works even if I add the above code line in category-mycategory.php. So I will submit another topic to see if category posts can be recalled from pages menu.
Edit: I solved pointing my page to category/mycategory/ using Page Links To plugin. It would be perfect if I could use relative paths instead of absolute path, but the workaround is acceptable for my needings.
- The topic ‘lists all the posts of two categories’ is closed to new replies.