• fernandolawl

    (@fernandolawl)


    On my website I have the content displaying using the code:

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <h1 align="center"><?php the_title(); ?></h1>
    <?php the_content(); ?>
    <?php endwhile; ?>

    Then I have my news posts showing below it using the code:

    <?php
    	// Include WordPress
    	define('WP_USE_THEMES', false);
    	require('./wp-load.php');
    	query_posts('showposts=1&cat=18');
    ?>
     <?php if ( have_posts() ): ?>
    <h1>News Titles</h1>
    <?php while ( have_posts() ) : the_post(); ?>
    <h2 style="margin-bottom:10px;"><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    <p align="center"><?php the_post_thumbnail(array(480,480)); ?> </p>
    <?php the_excerpt(); ?>
    <div style="float:none;clear:both"></div>
    <?php endwhile; ?>
    <?php else: ?>
    <h2>No posts to display</h2>
    <?php endif; ?>

    My goal is to show my news posts on my page before my content so when I put my news posts coding above the content coding, the content coding gets all messed up and the displays my posts.

    Is there a way I can have them flip without having my content code show my news posts?

Viewing 1 replies (of 1 total)
  • The problem is likely because you aren’t calling wp_reset_query() after the last endif.

    However, you really shouldn’t be using query_posts(). Instead you should be using the WP_Query object to create a custom loop.

    The code would look pretty much the same:

    <?php $query = new WP_Query( $args ); ?>
    
    <?php if( $query->have_posts() ) : ?>
    <h1>News Titles</h1>
    <?php while( $query->have_posts() ) : $query->the_post(); ?>
    <h2 style="margin-bottom:10px;"><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    <p align="center"><?php the_post_thumbnail(array(480,480)); ?> </p>
    <?php the_excerpt(); ?>
    <div style="float:none;clear:both"></div>
    <?php endwhile; ?>
    <?php else: ?>
    <h2>No posts to display</h2>
    <?php endif; ?>
    
    <?php wp_reset_post_data(); ?>

    Make sure to include wp_reset_post_data() or you will have the same issue.

Viewing 1 replies (of 1 total)

The topic ‘Post before Content’ is closed to new replies.