Static page with recent blog posts
-
I created a new page template using the code from index.php but it seems I can’t use a page template for the static page.. and I tried making it a normal page but no recent posts appear.. I guess I’m trying to find out what php code displays posts like in the blog page.. Please help me
-
This will display 5 posts–put it in your Page template. If you want the post content you will need to add the template tag, the_content().
<?php $args=array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 5, '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(). ?>
Thank you for teh code! I added it and I get a list. I tried adding the_content but nothing happens. This is the page template
<?php /* Template Name: Homepage */ get_header(); ?> <div id="content"> <div class="post"> <?php $args=array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 5, '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(). ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2> <?php the_content(__('Read more'));?> <?php endwhile; else: ?> <p><strong>There has been a glitch in the Matrix.</strong><br /> There is nothing to see here.</p> <p>Please try somewhere else.</p> <?php endif; ?> </div><!-- end #post--> </div><!-- end #content--> <?php get_sidebar(); ?> <?php get_footer(); ?>
Not sure what I’m doing wrong there..
That 2nd loop will display the Page. So add this
<?php the_content(__('Read more'));?>
after this:
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
Thank you so much! It works. I can even add the_excerpt ??
I hope I’m not asking too much (I probably am..) but here goes another question… I wouldn’t ask this much but I have been trying to fix this for 2 hours now and I just can;t figure it out.. I’m new to php ??
So.. to my singlepost.php I added this to the author meta tag
<?php printf(__('Written by %1$s in %2$s'),'<a href="'. get_author_posts_url(get_the_author_ID()) .'" title="'. sprintf(__("Posts by %s"), attribute_escape(get_the_author())).' ">'. get_the_author() .'</a>',$category_link); ?>
so it will take you to all the posts by the author.
Then I made.. actually borrowed a php from another theme called author.php
<?php /* Mystique/digitalnature */ get_header(); ?> <!-- main content: primary + sidebar(s) --> <div id="main"> <div id="main-inside" class="clearfix"> <!-- primary content --> <div id="primary-content"> <?php // global $wp_query; // $curauth = $wp_query->get_queried_object(); if(isset($_GET['author_name'])): $curauth = get_userdatabylogin($author_name); else : $curauth = get_userdata(intval($author)); endif; ?> <h1 class="title"><?php echo $curauth->display_name; ?></h1> <div class="clearfix"> <div class="alignleft"><?php echo get_avatar($curauth->user_email, '128', $avatar); ?></div> <div> <p> <?php if($curauth->user_description<>''): echo $curauth->user_description; else: _e("This user hasn't shared any biographical information","mystique"); endif; ?> </p> <?php if(($curauth->user_url<>'https://') && ($curauth->user_url<>'')) echo '<p class="im www">'.__('Homepage:','mystique').' <a href="'.$curauth->user_url.'">'.$curauth->user_url.'</a></p>'; if($curauth->yim<>'') echo '<p class="im yahoo">'.__('Yahoo Messenger:','mystique').' <a href="ymsgr:sendIM?'.$curauth->yim.'">'.$curauth->yim.'</a></p>'; if($curauth->jabber<>'') echo '<p class="im gtalk">'.__('Jabber/GTalk:','mystique').' <a href="gtalk:chat?jid='.$curauth->jabber.'">'.$curauth->jabber.'</a></p>'; if($curauth->aim<>'') echo '<p class="im aim">'.__('AIM:','mystique').' <a href="aim:goIM?screenname='.$curauth->aim.'">'.$curauth->aim.'</a></p>'; ?> </div> </div> <div class="divider"></div> <br /> <?php if (have_posts()): ?> <h3 class="title"><?php printf(__('Posts by %s', 'mystique'), $curauth->display_name); ?></h3> <div class="divider"></div> <?php while (have_posts()): the_post(); include(TEMPLATEPATH . '/post.php'); endwhile; ?> <div class="page-navigation clearfix"> <?php if(function_exists('wp_pagenavi')): wp_pagenavi(); else: ?> <div class="alignleft"><?php next_posts_link(__('« Older Entries','mystique')) ?></div> <div class="alignright"><?php previous_posts_link(__('Newer Entries »','mystique')) ?></div> <?php endif; ?> </div> <?php else : ?> <p class="error"><?php _e('No posts found by this author.','mystique'); ?></p> <?php endif; ?> </div> <!-- /primary content --> <?php get_sidebar(); ?> </div> </div> <!-- /main content --> <?php get_footer(); ?>
..and there’s a
include(TEMPLATEPATH . '/post.php');
in that code. The post.php is the code you gave me
<?php $args=array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 5, '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 the_excerpt(__('Read more'));?> <?php endwhile; } wp_reset_query(); // Restore global post data stomped by the_post(). ?>
It takes you to the author page and shows all posts by the author but the “list of posts” appears two times. I don’t know what to erase to have the list appear just once.
Not sure why you would need that code I gave you because that has a loop to display posts and all that other stuff is another loop to display posts.
yes, I fixed it.
- The topic ‘Static page with recent blog posts’ is closed to new replies.