ThemeShaper Forums » Thematic

[closed]

How to display posts from categories on pages using a second loop

(28 posts)
  • Started 4 years ago by Aaron
  • Latest reply from ash.matadeen
  • This topic is not a support question
  1. Hi there,

    I love thematic and as a designer I had to figure out this task over a few days by trial and error to get it running (still learning php...).
    As I am very thankful for the support in this forum I'd like to share this elegant solution with you as a copy-paste-delight:

    Task: display posts of one or more specific categories on specific pages in addition to the page's content. So you can have the page content as an introduction to the following category posts.

    Solution: add the query_posts(); with your desired categories below thematic_indexloop followed by another instance of thematic_indexloop and you're done:

    function childtheme_second_loop() {
    
    	if (is_page(153)) {
    		query_posts("cat=9&showposts=10");
    		thematic_index_loop();
    		} elseif (is_page(12)) {
    		query_posts("cat=4&showposts=10");
    		thematic_index_loop();
    		} elseif (is_page(7)) {
    		query_posts("cat=3&showposts=10");
    		thematic_index_loop();
    		} else {
    		return;
    	}
    }
    add_action('thematic_below_indexloop', 'childtheme_second_loop' );

    In my case it was very important to get the posts as full posts and not as excerpts. thematic_content() would deliver them as excerpts by default and that doesn't work very well with tables or flash galleries within the posts. Works quite simple, too:

    function childtheme_content($content) {
    	if (is_page()) {
    		$content= 'full';}
    }
    add_filter('thematic_content', 'childtheme_content');

    Hope you like it and thanks to Chris, Gene and grpsmglr00

    Posted 4 years ago #
  2. scottdeto
    Member

    Holy Cow this is timely! Thanks a bunch.

    Can you be a bit more specific about which files to add your code? Do I create a new Template for the Pages that I want to use this feature?

    Posted 4 years ago #
  3. I just realized that it works for me because I replaced the code of the page.php with the code of index.php.
    So index.php and page.php are exactly the same. I'm still learning and i think I just understood what the page template is good for.

    This code makes it work in my page template

    <?php thematic_above_indexloop() ?>
    
    <?php thematic_indexloop() ?>
    
    <?php thematic_below_indexloop() ?>

    The question would be how to hook it into the default page.php:

    <?php get_header() ?>
    
    	<div id="container">
    		<div id="content">
    
    <?php get_sidebar('page-top') ?>
    
    <?php the_post() ?>
    			<div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class() ?>">
        			<?php thematic_postheader(); ?>
    				<div class="entry-content">
    <?php the_content() ?>
    
    <?php wp_link_pages("\t\t\t\t\t<div class='page-link'>".__('Pages: ', 'thematic'), "</div>\n", 'number'); ?>
    
    <?php edit_post_link(__('Edit', 'thematic'),'<span class="edit-link">','</span>') ?>
    
    				</div>
    			</div><!-- .post -->
    
    <?php if ( get_post_custom_values('comments') ) thematic_comments_template() // Add a key+value of "comments" to enable comments on this page ?>
    
    <?php get_sidebar('page-bottom') ?>
    
    		</div><!-- #content -->
    	</div><!-- #container -->
    
    <?php thematic_sidebar() ?>
    <?php get_footer() ?>
    Posted 4 years ago #
  4. okay here is the one that works with thematic by default - just hook it to thematic_abovepagebottom

    function childtheme_second_loop() {
    
    	if (is_page(2)) {
    		query_posts("cat=9&showposts=10");
    		thematic_index_loop();
    		} elseif (is_page(12)) {
    		query_posts("cat=4&showposts=10");
    		thematic_index_loop();
    		} elseif (is_page(7)) {
    		query_posts("cat=3&showposts=10");
    		thematic_index_loop();
    		} else {
    		return;
    	}
    }
    add_action('thematic_abovepagebottom', 'childtheme_second_loop' );

    The credits for this go to Gene:
    http://forums.themeshaper.com/topic/how-to-bring-this-function-only-to-specific-pages#post-8999

    Confusion was mine ;)

    Posted 4 years ago #
  5. JerryRobinette
    Member

    Aaron,

    This is great! Thank you very much. :-)

    I've been trying to add "PageNavi" navigation at the bottom. It works but when you goto one of the other pages of posts it doesn't register the "current" page correctly.

    if (is_page(2)) {
    		query_posts("cat=9&showposts=10");
    		thematic_index_loop();
                    thematic_navigation_below();
    		} elseif (is_page(12)) {
    		query_posts("cat=4&showposts=10");
    		thematic_index_loop();
                    thematic_navigation_below();
    		} elseif (is_page(7)) {
    		query_posts("cat=3&showposts=10");
    		thematic_index_loop();
                    thematic_navigation_below();
    		} else {
    		return;
    	}
    }
    add_action('thematic_abovepagebottom', 'childtheme_second_loop' );

    Any guidance would be appreciated. Thanks again to you and Gene!

    Posted 4 years ago #
  6. JerryRobinette
    Member

    My mistake. It adds the navigation to the bottom but, it doesn't work at all.

    Posted 4 years ago #
  7. Hi Jerry-

    You have to enable pagination for the query here 's an example

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    	if (is_page(2)) {
    		query_posts("cat=1&showposts=2&paged=" . $paged);
    		thematic_index_loop();
    		thematic_navigation_below();
            }
    Posted 4 years ago #
  8. JerryRobinette
    Member

    Sweet as... Thanks! It worked perfectly.

    I'm new to PHP I really appreciate your help. :-)

    Jerry

    Posted 4 years ago #
  9. Thanks all - this thread has been very helpful!

    I am creating a 'Portfolio' page and want to list each portfolio piece in one big list on one page. I got it working (mostly), but I would like to tweak it so it does the following:

    1. I would like to disable the header so thet don't link to another page.

    2. I'd like to strip out both the 'By Admin | Published: JAN 1, 2010 | Edit' AND the 'Posted in Portfolio | Comments closed | Edit' so it doesn't act like a 'Post' BUT ONLY ON THIS PAGE! I am also going to have a regular blog with commenting that is under another page..

    Here's what I have :

    <?php
    /*
    Template Name: Portfolio
    */
    ?>
    
    <?php get_header() ?>
    
    	<div id="container">
    		<div id="content">
    
    <?php get_sidebar('page-top') ?>
    
    <?php the_post() ?>
    			<div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class() ?>">
        			<?php thematic_postheader(); ?>
    				<div class="entry-content">
    <?php the_content() ?>
    
    <?php thematic_below_indexloop() ?>
    
    <?php wp_link_pages("\t\t\t\t\t<div class='page-link'>".__('Pages: ', 'thematic'), "</div>\n", 'number'); ?>
    
    <?php edit_post_link(__('Edit', 'thematic'),'<span class="edit-link">','</span>') ?>
    
    				</div>
    			</div><!-- .post -->
    
    <?php if ( get_post_custom_values('comments') ) thematic_comments_template() // Add a key+value of "comments" to enable comments on this page ?>
    
    <?php get_sidebar('page-bottom') ?>
    
    		</div><!-- #content -->
    	</div><!-- #container -->
    
    <?php thematic_sidebar() ?>
    <?php get_footer() ?>

    Any ideas?

    Thanks for the help!

    Posted 4 years ago #
  10. Hi circleatseven!

    If you just want to throw these elements out the easiest way would be via the style.css file.

    .entry-meta {display: none;}

    If you want to throw them out only on a specific page dynamic classes are your friend

    .pageid-170 .entry-meta {display: none;}

    To comletely get rid of the title:

    h2.entry-title {display: none;}

    Page's title can be removed with:

    h2.entry-title {display: none;}

    Well, maybe that's a hint. Of course there may be ways to really filter these elements out via functions.php but I'm not skilled enough for that I think. CSS will just hide them in the rendered browser view, they are still there hidden in the source code...

    Posted 4 years ago #
  11. Thanks Aaron. The problem is that I want to keep the title - I just don't want it to be a link. Due to my deadline, I had to find another workaround, but I would love to know how to get this to work, if possible, for future use... I'll keep trying and post an answer here IF I ever figure it out.

    Thanks all!

    Posted 4 years ago #
  12. ben610
    Member

    Hi Aaron (et. al.),

    I'm attempting to convert the page of posts example here: http://codex.wordpress.org/Pages#A_Page_of_Posts to a template that works in thematic. It looks you found a solution, but I'm a bit of a n00b - where do I put the childtheme_second_loop() function? In in functions.php, or in the page template I'm making? And how exactly do I 'hook it to thematic_abovepagebottom'?

    I have a version that almost works, but the formatting gets all screwy as soon as a second post is added:

    <? /*
    Template Name: Events
    */
    ?>
    <?php get_header() ?>
    
    	<div id="container">
    		<div id="content">
    
    <?php get_sidebar('page-top') ?>
    
    <?php
    // page id 21 will get category ID 12 posts, page 16 will get category 32 posts, page 28 will get category 17 posts
    if (is_page('8') ) {
    $cat = array(15);
    } else {
    $cat = '';
    }
    
    $showposts = -1; // -1 shows all posts
    $do_not_show_stickies = 1; // 0 to show stickies
    $args=array(
       'category__in' => $cat,
       'showposts' => $showposts,
       'caller_get_posts' => $do_not_show_stickies
       );
    $my_query = new WP_Query($args);
    ?>
    
    <?php if( $my_query->have_posts() ) : ?>
    
    		<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    			<?php
    			//necessary to show the tags
    			global $wp_query;
    			$wp_query->in_the_loop = true;
    			?>
    
    <?php //the_post() ?>
    			<div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class() ?>">
        			<?php thematic_postheader(); ?>
    				<div class="entry-content">
    <?php the_content() ?>
    
    <?php endwhile; ?>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.
    
    		<?php //get_search_form(); ?>
    
    	<?php endif; ?>
    
    <?php wp_link_pages("\t\t\t\t\t<div class='page-link'>".__('Pages: ', 'thematic'), "</div>\n", 'number'); ?>
    
    <?php edit_post_link(__('Edit', 'thematic'),'<span class="edit-link">','</span>') ?>
    
    				</div>
    			</div><!-- .post -->
    
    <?php if ( get_post_custom_values('comments') ) thematic_comments_template() // Add a key+value of "comments" to enable comments on this page ?>
    
    <?php get_sidebar('page-bottom') ?>
    
    		</div><!-- #content -->
    	</div><!-- #container -->
    
    <?php thematic_sidebar() ?>
    <?php get_footer() ?>

    Thanks in advance for any help on this!

    -Ben

    Posted 4 years ago #
  13. Hi Ben,

    you just have to copy the code into your functions.php. The function is built up in a way that it hooks itself to thematic_abovepagebottom() on those pages that you define in the function.

    As thematic was made to make things easier you don't have to change anything in your template. It should work just by tweaking the page IDs and category IDs for your purpose in functions.php

    Posted 4 years ago #
  14. Side note: If you use this function, wordpress will interpret the particular page as a "category" page!
    Consequently wordpress will get the category.php Template for this page even if you choose another template in the wp-admin!

    So any further manipulations done with if (is_page())... won't get through to your page. You'll have to expand your clause to if (is_page() || is_category())...

    Maybe there is a way to prevent wordpress from changing the referenced template file...?

    cheers

    Posted 4 years ago #
  15. Aaron, did you ever find a solution to WordPress interpreting the page as a "category" page?

    Posted 4 years ago #
  16. wp_reset_query(); does the trick!

    function childtheme_second_loop() {
    
    	if (is_page(2)) {
    		query_posts("cat=9&showposts=10");
    		thematic_index_loop();
    		} elseif (is_page(12)) {
    		query_posts("cat=4&showposts=10");
    		thematic_index_loop();
    		} elseif (is_page(7)) {
    		query_posts("cat=3&showposts=10");
    		thematic_index_loop();
    		wp_reset_query();
    		} else {
    		return;
    	}
    }
    add_action('thematic_abovepagebottom', 'childtheme_second_loop' );
    Posted 4 years ago #
  17. philbenoit
    Member

    This works great but removes all the use of the "more" tag. Any ideas on how to keep that working.

    I use the more tag to hide content on a membership site and so its quite important its kept in there.

    Posted 4 years ago #
  18. acts7
    Member

    I FOUND A SOLUTION for the READ MORE tag

    Add this AFTER the above query

    //          LETS CHANGE THAT ... to a working read more link
    function excerpt_ellipse($text) {
       return str_replace('[...]', ' <a href="'.get_permalink().'">Read more ...</a>', $text);
    }
    add_filter('get_the_excerpt', 'excerpt_ellipse');
    Posted 3 years ago #
  19. treibstoff
    Member

    for more more....
    also works:

    <?php global $more; $more = false; ?>

    just before the entry-content
    and just after:

    <?php $more = true; ?>

    also if you like using nextpage:

    <?php global $multipage; $multipage = false;
         if ($multipage) {
              wp_link_pages(array('before' => '<div class="page-link" style="float:right">' . __('<span style="color:#004B91; font-weight:normal">»»» reed more on page:</span>', 'thematicchild') .'', 'after' => '</div>', 'next_or_number' => 'number'));
     }?><?php $multipage = true; ?>
    Posted 3 years ago #
  20. Thanks for the great post. I have one issue though. When I click on my posts from categories on pages using a second loop the current page item in the main navigation highlights my front page menu item i.e. the blog.

    I was thinking that I could filter in the access hook and change the css on the menu item in question.

    Any help would be appreciated.

    Posted 3 years ago #
  21. Tried this:

    function child_add_menu_items($output) {
    $my_search='current_page_parent';
    $my_replecement = '';
    return str_replace($my_search, $my_replecement, $output);
    }
    add_filter('wp_page_menu','child_add_menu_items');

    Which hid the any menu styles but not sure how to filter it if on a certain page or category of posts

    Posted 3 years ago #
  22. I tried the original code and it works like a charm. However, I would like to give the second loop a different look using CSS, and I have tried adding styles in the code, but that doesn't work.

    function childtheme_second_loop() {
    	if (is_home() || is_frontpage() ) {
    		query_posts("cat=21&showposts=4");
    		thematic_index_loop();
    		return;
    	}
    }
    add_action('thematic_below_indexloop', 'childtheme_second_loop' );

    Where in the code should one add a div-class element ?

    Posted 3 years ago #
  23. Just revisiting this thread and thought it might be useful for others creating a second loop using a category. If anyone can think of a better way let me know.

    I just filtered wp_page_menu to see which page I was on. If using second loop with posts from specific category (includes single and archive) then manually change the page item style to current_page_item.


    // Add a hardcoded menu if using a second loop for a specific category

    function childtheme_menu() { ?>
    <?php if (in_category('your-category') && is_single() || in_category('your-category') && is_archive() ){?>
    <div class="menu">
    <ul class="sf-menu">
    <li class="page_item page-item-11">Home
    <li class="page_item page-item-9">Link 1
    <li class="page_item page-item-18 current_page_item">Link 2
    <li class="page_item page-item-20">Link 3

    </div>

    <?php }else {?>
    <div class="menu">
    <ul class="sf-menu">
    <?php wp_list_pages('title_li='); ?>

    </div>
    <?php } ?>
    <?php }

    add_action('wp_page_menu','childtheme_menu');

    Posted 3 years ago #
  24. knowlsy
    Member

    Hi all,

    I've had a go with some of this, in general I have what I want, though I'm having a couple of minor issues.

    One is that I can't get the posts to appear in a shorter form with images/links and a 'read more' type button. I only get a chunck of text as plain text. I'm not sure where this problem comes from, but just noticed that the archive page for the catagory 'uncatagorized' shows up the same thing...

    The second problem is that, for some reason I get the latest post of the catagory below the three i requested in full. I don't know why that is there because I don't want it to be and never asked it to be!!

    Here is my page: http://www.salesianyouthministry.com/?page_id=47

    here is a link to the catagory 'uncatagorized' archive page: http://www.salesianyouthministry.com/?cat=1

    Any help would be appreciated!!

    Posted 3 years ago #
  25. How do i show the corresponding post thumbnails for each post?

    Posted 3 years ago #
  26. while this is perhaps not in line w/ the original topic (well not completely anyway) for everyone wanting to put loops of things outside of the normal loop please read:

    codex.wordpress.org/Function_Reference/get_posts

    there is a nice working example at the bottom and you can hack away from there and never worry about monkeying w/ your actual query.

    Posted 3 years ago #
  27. this is what is in my functions.php now (and thank you for the help!):


    //
    // Separate Cat to Page + Working Nav w/ Default
    //
    function childtheme_second_loop() {

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    if (is_page(509)) {
    query_posts("cat=25&showposts=4&paged=" . $paged);
    thematic_index_loop();
    thematic_navigation_below();
    } elseif (is_page(507)) {
    query_posts("cat=24&showposts=4&paged=" . $paged);
    thematic_index_loop();
    thematic_navigation_below();
    } else {
    return;
    }
    }
    add_action('thematic_abovepagebottom', 'childtheme_second_loop' );

    Posted 3 years ago #
  28. ash.matadeen
    Member

    Just in case anyone wants to refer to the page name and category name rather than the number:

    if (is_page(pagename)) {
    query_posts('category_name=categoryname&showposts=10');
    thematic_index_loop();
    }

    Simply replace pagename and categoryname with your own.

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.