Not sure where to put this code for my custom post type?

I have a custom post type (called ‘discount’) that I am trying to display in the twentythirteen theme. I created a custom page file called discount-page.php to create my own template for this post type. I’m not sure where I need to place the wp_query code, however. Here’s what the default page.php looks like, with an addition of the new template name:

<div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">

        <?php /* The loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>


            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <header class="entry-header">
                    <?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
                    <div class="entry-thumbnail">
                        <?php the_post_thumbnail(); ?>
                    </div>
                    <?php endif; ?>

                    <h1 class="entry-title"><?php the_title(); ?></h1>
                </header><!-- .entry-header -->

                <div class="entry-content">
                    <?php the_content(); ?>
                    <?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
                </div><!-- .entry-content -->

                <footer class="entry-meta">
                    <?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
                </footer><!-- .entry-meta -->
            </article><!-- #post -->

            <?php comments_template(); ?>
        <?php endwhile; ?>

    </div><!-- #content -->
</div><!-- #primary -->

Now, I’m fairly sure that in order for my posts to actually show their content, I need to add this block of code to the discount-page.php:

Read More
<?php
 $query = new WP_Query( array('post_type' => 'discount', 'posts_per_page' => 5 ) );
 while ( $query->have_posts() ) : $query->the_post(); ?>
// Your code e.g. "the_content();"
<?php endif; wp_reset_postdata(); ?>
<?php endwhile; ?>

I just have no idea where to add it? It seems to break the page no matter where I add it.

Related posts

Leave a Reply

1 comment

  1. You just have to change a couple of lines of code for it to work.

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
    
            <?php /* The loop */ ?>
            <?php 
                $query = new WP_Query( array('post_type' => 'discount', 'posts_per_page' => 5 ) );
                while ( $query->have_posts() ) : $query->the_post(); ?> ?>
    
    
                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <header class="entry-header">
                        <?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
                        <div class="entry-thumbnail">
                            <?php the_post_thumbnail(); ?>
                        </div>
                        <?php endif; ?>
    
                        <h1 class="entry-title"><?php the_title(); ?></h1>
                    </header><!-- .entry-header -->
    
                    <div class="entry-content">
                        <?php the_content(); ?>
                        <?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
                    </div><!-- .entry-content -->
    
                    <footer class="entry-meta">
                        <?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
                    </footer><!-- .entry-meta -->
                </article><!-- #post -->
    
                <?php comments_template(); ?>
            <?php endwhile; wp_reset_postdata(); ?>
    
        </div><!-- #content -->
    </div><!-- #primary -->
    

    If you want the super simple version of it removing everything else. Try this:

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <?php 
                $query = new WP_Query( array('post_type' => 'discount', 'posts_per_page' => 5 ) );
                while ( $query->have_posts() ) : $query->the_post(); 
            ?>
                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <div class="entry-content">
                        <?php the_content(); ?>
                    </div>
                </article>
            <?php endwhile; wp_reset_postdata(); ?>
        </div>
    </div>