I’m not trying to demonstrate that I’m some kind of php wizard of late (it takes so long to get the cogs turning properly inside my brain that it’s usually almost time to pack up before I get going as it is), but in this post I’d like to share a php routine of a few lines with you that has been and still proves to be extremely useful to me: -
–> Yes I’m aware that some people think it’s amateurish to use animated .gifs in blog posts.
– Call me an amateur – if you like, and read on: -
This short php routine (Displayed in full further down the page.) returns a listing of sequential blog entries; starting with the latest post. This could be a rather useful contents list, particularly for a small blog with a limited number of pages. It can be used on any blog as a contents-list, but the larger the amount of contents then the longer the load-time: I’m currently using it on this blog with currently between 150 and 200 posts and it takes up to 2 minutes to load. (‘Not really practical; but if readers want a full posts-listing then they’ll have to be patient for the time being.)
Line 1 assigns a new query-string and begins the loop. The numerical value that you replace ** with, following (‘showposts=, will be the numerical value of the number of posts returned. – For instance; if (‘showposts=20’) then the latest 20 posts, in reverse chronological order, will be listed.
Line 3 calls php functions into an HTML link which displays the linked title of the particular post.
Line 5 displays the post’s date and the number of comments made for that post. If you don’t want either or both of these functions then simply comment them out like so: -
5: <div class=”date”><span><strong><?php //the_time(‘F j, Y’); ?> ¦ <?php //comments_popup_link(’0′, ’1′, ‘% ‘); ?></strong></span></div>
Line 8 calls a snippet of the post or the description of the article; depending upon whether it reads <?php the_content(‘ or <?php the_excerpt(‘ . “the_content” will return a limited-sized snippet of the first few lines of the post – whereas “the_excerpt” will return the description you entered into the WordPress UI at some point whilst editing.
Line 9 ends the loop.
1: <?php $my_query = new WP_Query(‘showposts=**’); while ($my_query->have_posts()) : $my_query->the_post(); ?>
2: <div class=”clearboth”></div>
3: <h3 class=”h1″ id=”post-<?php the_ID(); ?>”><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><h2><?php the_title(); ?></h2></a></h3>
4: <div class=”post-meta-top”>
5: <div class=”date”><span><strong><?php the_time(‘F j, Y’); ?> ¦ <?php comments_popup_link(’0′, ’1′, ‘% ‘); ?></strong></span></div>
6: </div>
7: <div class=”clearboth”></div>
8: <?php the_excerpt(‘<strong>Click Here To Continue Reading »</strong>’); ?>
9: <?php endwhile; ?>
The above piece of php is based upon a routine originally crafted by David Risley and presented within BlogMasters Club forums.
Personally I use this, or an edited variation of this, not only for a full listings page, but also to create a limited listing of the latest posts on this blog’s Home Page: I’ve set the value of (‘showposts= to 8 in that particular case. I’ve used this routine with “showposts” values up to 200 in various places, with success.
As I said earlier; I’m not trying to demonstrate to you or fool you into believing that I’m some kind of genius php coder; in fact my php-coding skills are still fairly limited, despite having been coding using a combination of HTML – which I’m far better at coding in – and php, since the original inception of this blog in 2008. To tell the truth I’m still thinking in BASIC, and on mention of a loop my mind still immediately conjures up memories of the FOR X = … NEXT X loop. ![]()
Just to mention, in case you’re not aware; normally php only works from the template(s) of a blog theme unless you have a particular specialised plugin installed ( – as well as inside WordPress’ core of course, but this routine is meant for blog templates rather than otherwise: I advise you to leave the core well alone unless you’re a WordPress developer.), and not from inside a blog post. To be very frank; if you weren’t aware of that then I suggest that you read with interest only and do nothing more than study php for a while afterwards. – ‘Just saying.
- Anyway, that aside; if you’ve any comments, suggestions, rewrites, criticisms, please use the comment-form below to express them. – That’s it for this article. More content coming up soon with a bit of luck. ![]()
You’re looking at Kkomp.com – Beyond. <--Link to Home page.
The URL of what you see is http://kkomp.com/2011/11/16/a-php-contents-list-for-your-blog/
Why not share the linked URL by pasting it into Facebook or sending it by email?
Post Footer automatically generated by wp-posturl plugin for wordpress.
















Pingback: A php Loop to Display Only Your Latest Post | Kkomp.com - Beyond