treehouse : what would you like to learn today?
Web Design Web Development iOS Development

WordPress 3.1 - How can loop be modified to show only content of page?

  • I've created a page through WP admin and I want page to only display what I put in the content area. I'm pretty sure I should use something like this - <?php the_content(); ?> - but I'm not sure of the specifics.

    If I use the Loop from index.php (starkers) the content of the page displays but the "page name," "posted on, "author," "comments," etc are all included. I just want the content from page without the other stuff.

    I've tried this:
    			
    <?php query_posts(); the_post(); ?>

    <?php the_content(); ?>

    <?php wp_reset_query(); ?>


    It seems to work, but I'm not sure if it's the "correct" way to do it. I don't want to use something sloppy if there's a better way.

    Thank you,
    Charlie
  • Do you think the wp_reset_query should be used?
  • remove the other calls to php if you want to eliminate the author, comments, etc in index.php in any theme

    Al
  • The loop in the starkers blank theme I'm using is in a file called loop.php (see below). I understand the basics of how the loop works but I'm not sure what the bare minimum I need to use to show only the content on a single page...

    Is there is a "simpler" version I can use on just one page?


    <?php
    /**
    * The loop that displays posts.
    *
    * The loop displays the posts and the post content. See
    * http://codex.wordpress.org/The_Loop to understand it and
    * http://codex.wordpress.org/Template_Tags to understand
    * the tags used in it.
    *
    * This can be overridden in child themes with loop.php or
    * loop-template.php, where 'template' is the loop context
    * requested by a template. For example, loop-index.php would
    * be used if it exists and we ask for the loop with:
    * <code>get_template_part( 'loop', 'index' );

    *
    * @package WordPress
    * @subpackage Starkers
    * @since Starkers 3.0
    */
    ?>

    <?php /* Display navigation to next/previous pages when applicable */ ?>
    <?php if ( $wp_query->max_num_pages > 1 ) : ?>
    <?php next_posts_link( __( '← Older posts', 'twentyten' ) ); ?>
    <?php previous_posts_link( __( 'Newer posts →', 'twentyten' ) ); ?>
    <?php endif; ?>

    <?php /* If there are no posts to display, such as an empty archive page */ ?>
    <?php if ( ! have_posts() ) : ?>
    <?php _e( 'Not Found', 'twentyten' ); ?>
    <?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyten' ); ?>
    <?php get_search_form(); ?>

    <?php endif; ?>

    <?php<br /> /* Start the Loop.
    *
    * In Twenty Ten we use the same loop in multiple contexts.
    * It is broken into three main parts: when we're displaying
    * posts that are in the gallery category, when we're displaying
    * posts in the asides category, and finally all other posts.
    *
    * Additionally, we sometimes check for whether we are on an
    * archive page, a search page, etc., allowing for small differences
    * in the loop on each template without actually duplicating
    * the rest of the loop that is shared.
    *
    * Without further ado, the loop:
    */ ?>
    <?php while ( have_posts() ) : the_post(); ?>

    <?php /* How to display posts in the Gallery category. */ ?>

    <?php if ( in_category( _x('gallery', 'gallery category slug', 'twentyten') ) ) : ?>
    " title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?>
    <?php twentyten_posted_on(); ?>

    <?php if ( post_password_required() ) : ?>
    <?php the_content(); ?>
    <?php else : ?>
    <?php<br /> $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
    $total_images = count( $images );
    $image = array_shift( $images );
    $image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' );
    ?>
    "><?php echo $image_img_tag; ?>

    <?php printf( __( 'This gallery contains <a %1$s>%2$s photos.', 'twentyten' ),
    'href="' . get_permalink() . '" title="' . sprintf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ) . '" rel="bookmark"',
    $total_images
    ); ?>

    <?php the_excerpt(); ?>
    <?php endif; ?>

    " title="<?php esc_attr_e( 'View posts in the Gallery category', 'twentyten' ); ?>"><?php _e( 'More Galleries', 'twentyten' ); ?>
    |
    <?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?>
    <?php edit_post_link( __( 'Edit', 'twentyten' ), '|', '' ); ?>

    <?php /* How to display posts in the asides category */ ?>

    <?php elseif ( in_category( _x('asides', 'asides category slug', 'twentyten') ) ) : ?>

    <?php if ( is_archive() || is_search() ) : // Display excerpts for archives and search. ?>
    <?php the_excerpt(); ?>
    <?php else : ?>
    <?php the_content( __( 'Continue reading →', 'twentyten' ) ); ?>
    <?php endif; ?>

    <?php twentyten_posted_on(); ?>
    |
    <?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?>
    <?php edit_post_link( __( 'Edit', 'twentyten' ), '| ', '' ); ?>

    <?php /* How to display all other posts. */ ?>

    <?php else : ?>
    " title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?>
    <?php twentyten_posted_on(); ?>

    <?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?>
    <?php the_excerpt(); ?>
    <?php else : ?>
    <?php the_content( __( 'Continue reading →', 'twentyten' ) ); ?>
    <?php wp_link_pages( array( 'before' => '' . __( 'Pages:', 'twentyten' ), 'after' => '' ) ); ?>
    <?php endif; ?>

    <?php if ( count( get_the_category() ) ) : ?>
    <?php printf( __( 'Posted in %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
    |
    <?php endif; ?>
    <?php<br /> $tags_list = get_the_tag_list( '', ', ' );
    if ( $tags_list ):
    ?>
    <?php printf( __( 'Tagged %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
    |
    <?php endif; ?>
    <?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?>
    <?php edit_post_link( __( 'Edit', 'twentyten' ), '| ', '' ); ?>

    <?php comments_template( '', true ); ?>

    <?php endif; // This was the if statement that broke the loop into three parts based on categories. ?>

    <?php endwhile; // End the loop. Whew. ?>

    <?php /* Display navigation to next/previous pages when applicable */ ?>
    <?php if ( $wp_query->max_num_pages > 1 ) : ?>
    <?php next_posts_link( __( '← Older posts', 'twentyten' ) ); ?>
    <?php previous_posts_link( __( 'Newer posts →', 'twentyten' ) ); ?>
    <?php endif; ?>
  • I think I'm going to go with...

    <?php query_posts(); the_post(); ?>

    <?php the_content(); ?>

    <?php wp_reset_query(); ?>


    OR THIS...


    <?php the_post(); ?>

    <?php the_content(); ?>

    <?php wp_reset_query(); ?>


    I tested both and they seem to work the same. Are there any advantages to one over the other?
  • Yeah, man. Starker's loop.php is frigging insane. That was not what I expected at all after following Chris's WP theme video tutorial.

    One thing I realized, though, is when Chris deleted a lot of Starker's extra stuff, he deleted some things that might be pretty important to you... for example, the very first thing I wanted to do couldn't be done because Chris had removed the Featured Image functionality on his Personal Homepage Theme. I had to look around for a few hours to find out how to fix that.

    BTW, in case you're wondering, this is how you fix it... add these two lines to the top of your functions.php file under the opening <?php tag:<br />
    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 80, 55, true ); // W x H, hard crop if you want the thumbnails to automatically crop... adjust the size or just comment this line out if you want to adjust them with CSS.
  • Is there an alternative to Starkers?
  • Starkers is a great tool because it has everything you would need for a basic wordpress site to function on. You just need to read up on the wordpress codex some more to understand what certain functions are doing. I would suggest using this for a generic page grab in your theme.

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>


    The query posts allows you to alter it so that you can find a certain post or posts that have the selections you want like tags, custom fields, dates etc.
  • Thank you for the info.