Need Serious Help With Custom Loop – Need advice on best practices
-
I’m designing a custom page template that displays the latest five posts, each with a post thumbnail to the left (using the new thumbnail display feature in 2.9.1.
I grabbed the index.php template, saved it as “Blog” and added my custom coding (see below). I’m concerned that my loop query isn’t up to best practice standards. The page looks beautiful and looks exactly as I want it to, but there are some functional problems.
1) I’m using the query posts method and can’t get my pagination to work.
2) I’ve tried to change the coding to the standard loop, and can’t get posts to display correctly.
3) Do I need to switch templates?
4) Am I out of touch with reality in thinking this setup will work?I’ve been researching the Codex and searching on Google for two days now. Officially stumped…
Here’s my coding:
<?php /* Template Name: Blog */ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml" <?php language_attributes(); ?>> <head profile="https://gmpg.org/xfn/11"> <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" /> <title><?php wp_title('«', true, 'right'); ?> <?php bloginfo('name'); ?></title> <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" /> <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" /> <?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?> <?php wp_head(); ?> </head> <body> <div id="blog_wrapper"> <div id="header"> </div> <div id="blog_body_wrap"> <div id="navigation"> <div id="links"><ul> <li><?php wp_list_pages('orderby=id&include=175,177,310,182,184,186&title_li=&hierarchical=0'); ?> </li> </ul></div><!--end links--> </div><!--end navigation--> <div id="blog_wrap"> <div id="post_wrap"> <!--This section is currently pulling category ID #1, and can be switched by changing the cat=1 to show whatever category ID you would like in this area.--> <div class="post_wrap"> <h2 class="heading">Recent Posts</h2> <?php // page id 21 will get category ID 12 posts, page 16 will get category 32 posts, page 28 will get category 17 posts if (is_page('5') ) { $cat = array(3); } elseif ( is_page('') ) { $cat = array(); } elseif ( is_page('') ) { $cat = array(); } else { $cat = ''; } $showposts = 5; // -1 shows all posts $do_not_show_stickies = 1; // 0 to show stickies $args=array( 'category__in' => $cat, 'showposts' => $showposts, 'caller_get_posts' => $do_not_show_stickies ); $my_query = new WP_Query($args); ?> <?php if( $my_query->have_posts() ) : ?> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> <?php //necessary to show the tags global $wp_query; $wp_query->in_the_loop = true; ?> <div class="divider"></div> <div class="post_img"> <?php if ( has_post_thumbnail() ) { // the current post has a thumbnail } else { // the current post lacks a thumbnail } ?> <?php the_post_thumbnail(); ?> </div><!--end post_img--> <div class="post_content"> <b><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></b> <div class="stamp"><?php the_time('F jS, Y') ?><!-- by <?php the_author() ?> --> | <?php comments_popup_link( 'No Comments »', '1 Comment »', '% Comments »'); ?></div> <div class="excerpt"><?php if ($post->post_excerpt != "" ) {the_excerpt();}else {the_content_rss('', FALSE, '', 60);} ?><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> READ MORE</a> </div><!--end excerpt--> </div><!--end post-content--> <?php endwhile; ?> <?php else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php get_search_form(); ?> <?php endif; ?> </div> </div> </div><!--end homepage_left_wrap--> <div id="sidebar"> <ul> <?php /* Widgetized sidebar, if you have the plugin installed. */ if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?> <li> <?php get_search_form(); ?> </li> <!-- Author information is disabled per default. Uncomment and fill in your details if you want to use it. <li><h2>Author</h2> <p>A little something about you, the author. Nothing lengthy, just an overview.</p> </li> --> <?php if ( is_404() || is_category() || is_day() || is_month() || is_year() || is_search() || is_paged() ) { ?> <li> <?php /* If this is a 404 page */ if (is_404()) { ?> <?php /* If this is a category archive */ } elseif (is_category()) { ?> <p>You are currently browsing the archives for the <?php single_cat_title(''); ?> category.</p> <?php /* If this is a yearly archive */ } elseif (is_day()) { ?> <p>You are currently browsing the <a href="<?php bloginfo('url'); ?>/"><?php echo bloginfo('name'); ?></a> blog archives for the day <?php the_time('l, F jS, Y'); ?>.</p> <?php /* If this is a monthly archive */ } elseif (is_month()) { ?> <p>You are currently browsing the <a href="<?php bloginfo('url'); ?>/"><?php echo bloginfo('name'); ?></a> blog archives for <?php the_time('F, Y'); ?>.</p> <?php /* If this is a yearly archive */ } elseif (is_year()) { ?> <p>You are currently browsing the <a href="<?php bloginfo('url'); ?>/"><?php echo bloginfo('name'); ?></a> blog archives for the year <?php the_time('Y'); ?>.</p> <?php /* If this is a monthly archive */ } elseif (is_search()) { ?> <p>You have searched the <a href="<?php echo bloginfo('url'); ?>/"><?php echo bloginfo('name'); ?></a> blog archives for <strong>'<?php the_search_query(); ?>'</strong>. If you are unable to find anything in these search results, you can try one of these links.</p> <?php /* If this is a monthly archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?> <p>You are currently browsing the <a href="<?php echo bloginfo('url'); ?>/"><?php echo bloginfo('name'); ?></a> blog archives.</p> <?php } ?> </li> <?php }?> </ul> <ul> <?php wp_list_pages('title_li=<h2>Pages</h2>' ); ?> <li><h2>Archives</h2> <ul> <?php wp_get_archives('type=monthly'); ?> </ul> </li> <?php wp_list_categories('show_count=1&title_li=<h2>Categories</h2>'); ?> </ul> <ul> <?php /* If this is the frontpage */ if ( is_home() || is_page() ) { ?> <?php wp_list_bookmarks(); ?> <li> <h2>Meta</h2> <ul> <?php wp_register(); ?> <li><?php wp_loginout(); ?></li> <li><a href="https://validator.w3.org/check/referer" title="This page validates as XHTML 1.0 Transitional">Valid <abbr title="eXtensible HyperText Markup Language">XHTML</abbr></a></li> <li><a href="https://gmpg.org/xfn/"><abbr title="XHTML Friends Network">XFN</abbr></a></li> <li><a href="https://www.ads-software.com/" title="Powered by WordPress, state-of-the-art semantic personal publishing platform.">WordPress</a></li> <?php wp_meta(); ?> </ul> </li> <?php } ?> <?php endif; ?> </ul> </div><!--end sidebar--> </div><!--end blog wrapper--> <?php get_footer(); ?>
- The topic ‘Need Serious Help With Custom Loop – Need advice on best practices’ is closed to new replies.