Static page with blog content
-
I am wantingt o create a static page, and display the 5 most recent blog entries. I know I can just set the default limit to 5, but this is on a dynamic page.
I want to create a static page, which I then add some blog entries to. I have looked into `<?php $my_query = “showposts=5”; $my_query = new WP_Query($my_query); ?>’ but I cant figure out how to use it.
I have looked at this previous topic, but it’s so old I thought I should create a new post.
I have tried to use the code that llamaman first suggested, although I get the full content of each post, rather than the content I get now with a Read more.
Can anybody please help me.
Thanks in advance!
-
Ok, I have tried to take
<?php get_template_part( 'content', get_post_format() ); ?>from the index, which cuts each post at the “Read more” point that I chose for the post, but when I use it for the wp_query, it displays the full post. I only want it to display the small first part, upto the Read more text.The code that I have so far is:
<?php /* Template Name: Test */ get_header(); ?> <div id="primary"> <div id="content" role="main"> <?php $my_query = "showposts=3"; $my_query = new WP_Query($my_query); ?> <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; // end of one post ?> <?php endif; //end of loop ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <p>Not sure when this would be executed</p> <?php endwhile; endif; ?> </div><!-- #content --> </div><!-- #primary --> <?php get_footer(); ?>Thanks
This is content.php, I think which is doing all the styling, and showing the “Continue reading” text.
<?php /** * Template for displaying content * * @package WordPress * @subpackage Twenty_Eleven * @since Twenty Eleven 1.0 */ ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <header class="entry-header"> <?php if ( is_sticky() ) : ?> <hgroup> <h2 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2> <h3 class="entry-format"><?php _e( 'Featured', 'twentyeleven' ); ?></h3> </hgroup> <?php else : ?> <h1 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h1> <?php endif; ?> <?php if ( 'post' == get_post_type() ) : ?> <div class="entry-meta"> <?php twentyeleven_posted_on(); ?> </div><!-- .entry-meta --> <?php endif; ?> <?php if ( comments_open() && ! post_password_required() ) : ?> <div class="comments-link"> <?php comments_popup_link( '<span class="leave-reply">' . __( 'Reply', 'twentyeleven' ) . '</span>', _x( '1', 'comments number', 'twentyeleven' ), _x( '%', 'comments number', 'twentyeleven' ) ); ?> </div> <?php endif; ?> </header><!-- .entry-header --> <?php if ( is_search() | is_home() | is_archive() ) : // Edit this to show excerpts in other areas of the theme?> <div class="entry-summary"> <!-- This adds the post thumbnail/featured image --> <div class="excerpt-thumb"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"> <?php the_post_thumbnail('medium', 'class=alignleft'); ?></a></div> <?php the_content('Continue reading '); ?> </div><!-- .entry-summary --> <?php else : ?> <div class="entry-content"> <?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?> <?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?> </div><!-- .entry-content --> <?php endif; ?> <footer class="entry-meta"> <?php $show_sep = false; ?> <?php if ( is_object_in_taxonomy( get_post_type(), 'category' ) ) : // Hide category text when not supported ?> <?php /* translators: used between list items, there is a space after the comma */ $categories_list = get_the_category_list( __( ', ', 'twentyeleven' ) ); if ( $categories_list ): ?> <span class="cat-links"> <?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyeleven' ), 'entry-utility-prep entry-utility-prep-cat-links', $categories_list ); $show_sep = true; ?> </span> <?php endif; // End if categories ?> <?php endif; // End if is_object_in_taxonomy( get_post_type(), 'category' ) ?> <?php if ( is_object_in_taxonomy( get_post_type(), 'post_tag' ) ) : // Hide tag text when not supported ?> <?php /* translators: used between list items, there is a space after the comma */ $tags_list = get_the_tag_list( '', __( ', ', 'twentyeleven' ) ); if ( $tags_list ): if ( $show_sep ) : ?> <span class="sep"> | </span> <?php endif; // End if $show_sep ?> <span class="tag-links"> <?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyeleven' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); $show_sep = true; ?> </span> <?php endif; // End if $tags_list ?> <?php endif; // End if is_object_in_taxonomy( get_post_type(), 'post_tag' ) ?> <?php if ( comments_open() ) : ?> <?php if ( $show_sep ) : ?> <span class="sep"> | </span> <?php endif; // End if $show_sep ?> <span class="comments-link"><?php comments_popup_link( '<span class="leave-reply">' . __( 'Leave a reply', 'twentyeleven' ) . '</span>', __( '<b>1</b> Reply', 'twentyeleven' ), __( '<b>%</b> Replies', 'twentyeleven' ) ); ?></span> <?php endif; // End if comments_open() ?> <?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?> </footer><!-- .entry-meta --> </article><!-- #post-<?php the_ID(); ?> -->I think the problem is on line 35, I need to tell it to display this on the new page template I’ve just created. testPage.php is the template for the ‘test’ page that I want to be static. Ive tried changing line 35 to include the ‘test’ page that I’ve created, so it shows on there also but that doesn’t work :/
<?php if ( is_search() | is_home() | is_archive() | is_page( 'test' ) ) : // Edit this to show excerpts in other areas of the theme?>Can anybody suggest any way I can display the content (up to the ‘Read more’) on the homepage, but limiting the display to 3 or 5?
You need to create your own custom Query. More information can be found here:
http://codex.wordpress.org/Class_Reference/WP_QueryBut I have created a custome query with
<?php $my_query = "showposts=3"; $my_query = new WP_Query($my_query); ?>Try to use the_excerpt()
<div class="entry-summary"> <div class="excerpt-thumb"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail('medium', 'class=alignleft'); ?></a></div> <?php the_excerpt(); ?> </div><!-- .entry-summary -->Also, use wp_reset_postdata() after the custom Query.
The topic ‘Static page with blog content’ is closed to new replies.