• Resolved Matt

    (@nib101)


    Hi all,

    Something weird is going on. I’m trying to set up some theme pages and no content is showing up within the loop. See code below for the text that’s displaying and not displaying on the archive.php page.

    This Text Displays
    	<?php if (have_posts() ) :
    	?>
    	<h2 class="post-title">This text doesn't display</h2>
    	<?php
    	(have_posts() ) : the_post(); ?>
    	<?php endwhile; else : ?>
    		<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    	<?php endif; ?>
    This Text Displays

    This problem is affecting content on other pages too. Any help would be greatly appreciated!

    Thanks in advance!

Viewing 14 replies - 1 through 14 (of 14 total)
  • Are there any errors showing on your page? On line 6 of your snippet, you’ve forgotten the “while” at the start of the line:

    while ( have_posts() ) : the_post(); ?>

    Thread Starter Matt

    (@nib101)

    Oops sorry I deleted that by mistake. This is the code that isn’t working.

    This Text Displays
    			<?php if (have_posts() ) :
    			?>
    			<h2 class="post-title">This text isn't displaying</h2>
    			<?php
    			while (have_posts() ) : the_post(); ?>
    			<?php endwhile; else : ?>
    				<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    			<?php endif; ?>
    		This Text Displays

    Can you post the entirety of the code to Pastebin and post the link here? Can you enable debugging and see if any errors appear?

    Thread Starter Matt

    (@nib101)

    Really sorry Stephen, you’re going to have to be patient with me as i’m a total noob.

    I’ve enabled debugging and there doesn’t seem to be any errors that I can see.

    The code is pretty much it other than some standard HTML. as I was following this tutorial and hit a problem at the first hurdle.

    That code above is just in a content div and there’s no other php on the archive.php page other that get get_header and get_footer functions.

    Am I missing something silly?

    Can you post it to Pastebin anyway?

    Thread Starter Matt

    (@nib101)

    Sure no problem, here it is.

    Do you have posts in the archive page that you’re displaying? Do your posts appear if you visit your home page?

    Thread Starter Matt

    (@nib101)

    I have the standard ‘Hello World’ post still and a couple of posts as a custom post type called “blog”

    Custom post types are an interesting beast sometimes. Does it work if you make a copy of your archive.php and name it archive-blog.php? Alternatively, does it work if you edit archive.php and use this code instead:

    This Text Displays
    <?php $args = array( 'post_type' => 'blog', 'posts_per_page' => 5 ); ?>
    <?php $loop = new WP_Query( $args );
    <?php if ( $loop->have_posts() ) : ?>
      <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <h2 class="post-title">Post Title</h2>
      <?php endwhile; else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    This Text Displays
    Thread Starter Matt

    (@nib101)

    Hmm, it seems that using archive-blog.php defaults the page back to index.php and using the code above parses and error on line 37 which is this one

    <?php if ( $loop->have_posts() ) : ?>

    I accidentally left off the closing PHP tag on the previous line, but it doesn’t matter because that code won’t do what you want, anyway. You should revert your archive.php back to what you had before.

    When you created your custom post type, did you pass 'has_archive' => true to register_post_type()? When you created the file archive-blog.php, did you go to Dashboard > Settings > Permalinks and hit the “Save Changes” button to rebuild your rewrite rules? Can you post the code you used to create your custom post type?

    Thread Starter Matt

    (@nib101)

    Ok thanks, i’ve reverted the page back to how it was.

    I created the custom post type using this plugin. Would this have been taken care of when installing or do I need to check that somewhere?

    Thread Starter Matt

    (@nib101)

    I’ve found the answer!!

    I had to create a wp_query with the arguments for the custom post type and apply that to the loop.

    Here is the updated code

    <?php
           $args = array( 'post_type' => 'blog','cat'=> 5,);
           $myblogPosts = new WP_Query($args);
    
           if ($myblogPosts->have_posts() ) :
           ?>
               <h2 class="post-title">This text now displays!!</h2>
           <?php
           while ($myblogPosts->have_posts() ) : $myblogPosts->the_post(); ?>
               <div><?php the_title(); the_content(); ?>
           <?php endwhile; else : ?>
           <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
           <?php endif; ?>

    Stephen – thank you so much for your help!! I’m learning all the time with the great support from the forums!!

    Thread Starter Matt

    (@nib101)

    Resolved!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Content within the loop is not displaying’ is closed to new replies.