Displaying WordPress Post Parts
I switched this blog theme to “Photon” for a bit, very minimalist, where I had 10 posts per page. I just wanted to get some Google Analytic stats on specific article reads, and having full articles on a page doesn’t give that to me. I needed the articles to display like this on the navigational pages:
- Home Page (Page 1)
- Entire Recent Article
- First 128 character excerpt of next 9 articles.
- Page 2
- First 128 character excerpt of next 10 articles.
- Etc.
So I had to adjust index.php from the WordPress editor, and here’s what I did:
<?php get_header(); ?> <?php get_sidebar(); ?> <div id="wrapper"> <!-- $posts defaults to 10 but you can change it--> <?php /*$posts = get_posts('numberposts=10');*/ ?> <div id="posts" class="excerpts"> <ul> <!-- this section sets the length of the preview for older articles, using a WP filter --> <?php $firstElement = 0; function custom_excerpt_length( $length ) { return 128; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); ?> <?php foreach ($posts as $post) : ?> <?php setup_postdata ($post); ?> <!-- this specifies what hyperlinks show up in the title position --> <li><h2 class="post-title"><a href="<?php the_permalink (); ?>"><?php the_title(); ?></a></h2> <small><?php the_time('F jS, Y') ?> by <?php the_author() ?></small> <!-- reset the data query or there will be defects in the behavior --> <?php wp_reset_query(); ?> <!-- make sure the post is first one on first page, if so show all of it, else you just get a excerpt--> <?php if (get_option ('photon_index_post_length') == "full" && $firstElement == 0 && is_home() && '1' > $paged ) : ?> <?php the_content (); ?> <?php else: ?> <?php the_excerpt (); ?> <?php endif; ?> <p class="postmetadata"> Posted in <?php the_category(', ') ?> <strong>|</strong> <?php edit_post_link('Edit','','<strong> |</strong>'); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p> <?php $firstElement++; /* bil */ ?> </li> <?php endforeach; ?> <li> <p style="float: left"><?php previous_posts_link(); ?></p> <p style="float: right"><?php next_posts_link(); ?></p> <p style="clear: both"> </p> </li> </ul> </div> </div> <?php get_footer(); ?>