ThemeShaper Forums » Thematic

[closed]

displaying posts from categories on pages using a second loop

(13 posts)
  • Started 4 years ago by knowlsy
  • Latest reply from krowchuk
  • This topic is not resolved
  1. knowlsy
    Member

    Hi all,

    I'm having trouble with this, I've got something but it isn't working properly and I'm not sure why...

    I lifted my code from here http://forums.themeshaper.com/topic/how-to-display-posts-from-categories-on-pages-using-a-second-loop

    If you look on my page here http://www.salesianyouthministry.com/?page_id=47 you can see the problems I'm having, I get catagories repeating under the static content in my page, but I only have a certain amount visible (idealy i would be able to have that much, but have a working 'read more' at the bottom and have the image next to it. One of the oddest things is that the final post shown is the first in full... I'm a bit confused...

    Thanks for any help in advance!!

    Chris

    Posted 4 years ago #
  2. Hi knowlsy,

    It might be helpful if you can explain what it is you are trying to do and the steps you have taken so far, the expected result and the actual result (including any relevant code snippets). I'm new to using thematic, so you might have to spell some stuff out for me, but I'm more than willing to dig in and help out.

    Posted 4 years ago #
  3. why not just copy the blog page template from the thematic folder into your folder. template-page-blog.php or whatever it is called. rename the file, rename the template (in the comments at the top) and then adjust the query parameters. apply this template to the static page you'd like to display the categories in question.

    see the WP codex for appropriate query params
    http://codex.wordpress.org/Function_Reference/query_posts

    Posted 4 years ago #
  4. knowlsy
    Member

    Thanks for this Helga,

    I've just had a go with this but am not too sure where i'm putting things in the template.

    Would doing it this way allow me to have page content? So far, trying different queries in different places in the template I'm only able to have either blog posts or my static page?

    If so, can you tell me where in my standard blog template i should be putting the:

    <?php
    
    //The Query
    query_posts('posts_per_page=3');
    
    ?>

    to have my static page content followed by posts from catagory 3 for example?

    thanks again!

    Chris

    Posted 3 years ago #
  5. i guess i didn't understand exactly what you were trying to do. i'd probably still create a template.... but instead of what i suggested earlier you will need a second loop after all. i usually use get_posts.

    http://codex.wordpress.org/Template_Tags/get_posts

    you can use that anywhere in your template and not mess up the regular query (the page's static content). i've made metaboxes in the past that control the get_posts arguments (uses the same as the query function) but you could also do it w/ hidden fields (maybe simpler/faster) and then you'd only need 1 template to show posts from any category.

    Posted 3 years ago #
  6. knowlsy
    Member

    Thanks Helga,

    I'm still not having much joy. I've tried putting it into all the places in a page template I can think of, here is the code of the template I've tried to put it into, followed by two seperate codes i've worked out from the site you gave in your post. Am i going wrong somewhere really obvious?!

    <?php
    
    /**
     * Template Name: testcat3
     *
     * This template allows you to display the latest posts on any page of the site.
     *
     */
    
        // calling the header.php
        get_header();
    
        // action hook for placing content above #container
        thematic_abovecontainer();
    
    ?>
    
    		<div id="container">
    
    			<?php thematic_abovecontent(); ?>
    
    			<div id="content">
    
    	            <?php
    
    	            // calling the widget area 'page-top'
    	            get_sidebar('page-top');
    
    	            the_post();
    
    	            thematic_abovepost();
    
    	            ?>
    
    				<div id="post-<?php the_ID();
    					echo '" ';
    					if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
    						post_class();
    						echo '>';
    					} else {
    						echo 'class="';
    						thematic_post_class();
    						echo '">';
    					}
    
    	                // creating the post header
    	                thematic_postheader();
    
    	                ?>
    
    					<div class="entry-content">
    
    	                    <?php
    
    	                    the_content();
    
    	                    wp_link_pages("\t\t\t\t\t<div class='page-link'>".__('Pages: ', 'thematic'), "</div>\n", 'number');
    
    	                    edit_post_link(__('Edit', 'thematic'),'<span class="edit-link">','</span>') ;
    
    				 ?>
    
    	</div><!-- .entry-content -->
    	</div><!-- #post -->
    
    	        <?php
    
    	        thematic_belowpost();
    
    	        // calling the comments template
           		if (THEMATIC_COMPATIBLE_COMMENT_HANDLING) {
    				if ( get_post_custom_values('comments') ) {
    					// Add a key/value of "comments" to enable comments on pages!
    					thematic_comments_template();
    				}
    			} else {
    				thematic_comments_template();
    			}
    
    	        // calling the widget area 'page-bottom'
    	        get_sidebar('page-bottom');
    
    	        ?>
    
    				</div><!-- #content -->
    
    			<?php thematic_belowcontent();
    
    			get_posts('cat=3')
    			 ?> 
    
    		</div><!-- #container -->
    
    <?php 
    
     // action hook for placing content below #container
        thematic_belowcontainer();
    
        // calling the standard sidebar
        thematic_sidebar();
    
        // calling footer.php
        get_footer();
    
    ?>

    attempt 1:

    <?php get_posts('cat=3')?>

    attempt 2:

    <?php $args = array('category' => 3,); ?>

    Thanks!!

    Posted 3 years ago #
  7. knowlsy
    Member

    If anyone is trying to do something similar to me, i've now found the plugin list catagory posts, http://wordpress.org/extend/plugins/list-category-posts/.

    It's not quite what I wanted, but it'll do the job for now! You can use the different paramiters to show just the heading, a bit of the post, or all of it

    Posted 3 years ago #
  8. @knowlsy- you need to put your get_posts call IN the page template somewhere. then you need to reference some of the examples in the codex that will set up a foreach loop.

    <?php
    
    /**
     * Template Name: testcat3
     *
     * This template allows you to display the latest posts on any page of the site.
     *
     */
    
        // calling the header.php
        get_header();
    
        // action hook for placing content above #container
        thematic_abovecontainer();
    
    // the get_posts and foreach loop below will pick 5 random posts and put them in a list
    
    ?>
    
    <ul>
    <?php
    $args = array( 'numberposts' => 5, 'orderby' => 'rand' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    
    		<div id="container">
    
    			<?php thematic_abovecontent(); ?>
    
    			<div id="content">
    
    	            <?php
    
    	            // calling the widget area 'page-top'
    	            get_sidebar('page-top');
    
    	            the_post();
    
    	            thematic_abovepost();
    
    	            ?>
    
    				<div id="post-<?php the_ID();
    					echo '" ';
    					if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
    						post_class();
    						echo '>';
    					} else {
    						echo 'class="';
    						thematic_post_class();
    						echo '">';
    					}
    
    	                // creating the post header
    	                thematic_postheader();
    
    	                ?>
    
    					<div class="entry-content">
    
    	                    <?php
    
    	                    the_content();
    
    	                    wp_link_pages("\t\t\t\t\t<div class='page-link'>".__('Pages: ', 'thematic'), "</div>\n", 'number');
    
    	                    edit_post_link(__('Edit', 'thematic'),'<span class="edit-link">','</span>') ;
    
    				 ?>
    
    	</div><!-- .entry-content -->
    	</div><!-- #post -->
    
    	        <?php
    
    	        thematic_belowpost();
    
    	        // calling the comments template
           		if (THEMATIC_COMPATIBLE_COMMENT_HANDLING) {
    				if ( get_post_custom_values('comments') ) {
    					// Add a key/value of "comments" to enable comments on pages!
    					thematic_comments_template();
    				}
    			} else {
    				thematic_comments_template();
    			}
    
    	        // calling the widget area 'page-bottom'
    	        get_sidebar('page-bottom');
    
    	        ?>
    
    				</div><!-- #content -->
    
    			<?php thematic_belowcontent();
    
    			get_posts('cat=3')
    			 ?> 
    
    		</div><!-- #container -->
    
    <?php 
    
     // action hook for placing content below #container
        thematic_belowcontainer();
    
        // calling the standard sidebar
        thematic_sidebar();
    
        // calling footer.php
        get_footer();
    
    ?>

    i just copied a sample loop, but you could change the get_posts arguments to anything

    Posted 3 years ago #
  9. Chris,

    I think I understand what it is you are trying to accomplish and if I understand you correctly, I have done something similar.

    On my site I wanted to add a second loop on specific static pages that grabs the posts from specific categories and displays them in addition to the content of the static pages. I have them displaying as on page 1 show x# of post excerpts from category A with the remainder (if any) as a simple list of linkable titles beside the excerpts. Additionally, I have set up conditional statements in my function that will show excerpts from a different category on different pages and even one that lists excerpts from multiple categories.

    If this is along the line of what you want to accomplish, have a look at my site and let me know. I would be happy to share the code.

    eg:
    On my about page I show the 2 latest posts from Category News as excerpts and the remainders as a title list - see: http://www.krowchukdressage.com/about/
    On my lessons page I show 2 latest posts from Category Training Articles as excerpts and the remainders as a title list - see: http://www.krowchukdressage.com/lessons/
    On my contact page I don't show any, etc.

    Let me know if this is helpful.

    Cheers!
    Chris

    Posted 3 years ago #
  10. knowlsy
    Member

    thanks chris and helga,

    I've got most of it working (code below), but the only thing I'm now stuck on is how to get the posts to follow the normal rules (I want it formatted like other posts so it isn't pushed right up to the edges, and want to be able to use the 'read more' thingy). Here it is in action

    http://www.salesianyouthministry.com/?page_id=86

    <?php
    
    /**
     * Template Name: testcat3b
     *
     * This template allows you to display the latest posts on any page of the site.
     *
     */
    
        // calling the header.php
        get_header();
    
        // action hook for placing content above #container
        thematic_abovecontainer();
    
    // the get_posts and foreach loop below will pick 5 random posts and put them in a list
    
    ?>
    
    		<div id="container">
    
    			<?php thematic_abovecontent(); ?>
    
    			<div id="content">
    
    	            <?php
    
    	            // calling the widget area 'page-top'
    	            get_sidebar('page-top');
    
    	            the_post();
    
    	            thematic_abovepost();
    
    	            ?>
    
    				<div id="post-<?php the_ID();
    					echo '" ';
    					if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
    						post_class();
    						echo '>';
    					} else {
    						echo 'class="';
    						thematic_post_class();
    						echo '">';
    					}
    
    	                // creating the post header
    	                thematic_postheader();
    
    	                ?>
    
    					<div class="entry-content">
    
    	                    <?php
    
    	                    the_content();
    
    	                    wp_link_pages("\t\t\t\t\t<div class='page-link'>".__('Pages: ', 'thematic'), "</div>\n", 'number');
    
    	                    edit_post_link(__('Edit', 'thematic'),'<span class="edit-link">','</span>') ;
    
    				 ?>
    
    	</div><!-- .entry-content -->
    	</div><!-- #post -->
    
    	        <?php
    
    	        thematic_belowpost();
    
    	        // calling the comments template
           		if (THEMATIC_COMPATIBLE_COMMENT_HANDLING) {
    				if ( get_post_custom_values('comments') ) {
    					// Add a key/value of "comments" to enable comments on pages!
    					thematic_comments_template();
    				}
    			} else {
    				thematic_comments_template();
    			}
    
    	        // calling the widget area 'page-bottom'
    	        get_sidebar('page-bottom');
    
    	        ?>
    
    				</div><!-- #content -->
    
    			<?php thematic_belowcontent();
    
    ?><?php
     $lastposts = get_posts('numberposts=3&category=3');
     foreach($lastposts as $post) :
        setup_postdata($post);
     ?>
    <br /> <h1><strong>
    <b><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></b></strong></h1><br /><br />
     <?php the_content(); ?>
     <?php endforeach; ?>
    
    		</div><!-- #container -->
    
    <?php 
    
     // action hook for placing content below #container
        thematic_belowcontainer();
    
        // calling the standard sidebar
        thematic_sidebar();
    
        // calling footer.php
        get_footer();
    
    ?>

    Thanks again!!

    Posted 3 years ago #
  11. Hi Chris,

    From a quick peek, you have a multitude of issues that need to be addressed.

    A good starting point...

    (I want it formatted like other posts so it isn't pushed right up to the edges

    I think you want your loop to output inside the div id="content"? This should pick up the css styling of the content above the loop, so first move your closing div for content below the custom loop, above the closing div for container, see if that helps. Or, create a new div inside your custom loop with a different id and style that however you wish, if you want the loop output to be different than the styling of #content. try that and see what happens. You also have the option to place this custom loop in your function file and hook into thematic_belowcontent() as opposed to placing it in your template file. You could add a conditional statement to have this loop show only on specific pages, perhaps negating the need for this template completely.

    Second, (probably not related to the issue at hand but just a note) you are wrapping your H1 tag with both strong and b. You might want to address that.

    want to be able to use the 'read more' thingy)

    Well, you are telling the loop to use php the_content(). This is a choice to be made on your backend. Do you want these posts to use the content and manually use the "more" tag in your post editing? There is a setting in the admin panel for that, or do you want to use an excerpt? If you want to use excerpt then change the_content to the_excerpt. You can write custom excerpts for each post which could display different content than the actual post if you wish.

    So, a couple things to think about but first try and address the closing content div location and see if that sets you on the right path.

    Cheers!
    Chris

    Posted 3 years ago #
  12. knowlsy
    Member

    Thanks Chris!!

    I've sorted out my formatting, so it looks better!

    I have the 'read more' setting on as you can see on my 'news' page, it automatically comes up there. Is there some reason it wouldn't automatically come up??

    Thanks for all your help!!

    Chris

    Posted 3 years ago #
  13. Chris,

    The Codex covers all the techniques for the "read more" link in the_content(), here: http://codex.wordpress.org/Template_Tags/the_content and here: http://codex.wordpress.org/Customizing_the_Read_More
    Have a peek, pick an option that works for you as there are plenty to choose from!

    Cheers!
    Chris

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.