0

I am a newbie in developing wp themes and having troubles showing my cpt.

I added new cpt in my functions.php file:

// Creates Testimonials Custom Post Type
function testimonials_init() {
    $args = array(
      'label' => 'Testimonials',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'testimonials'),
        'query_var' => true,
        'menu_icon' => 'dashicons-format-quote',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',)
        );
    register_post_type( 'testimonials', $args );
}
add_action( 'init', 'testimonials_init' );

And I have created a new page template and added this code:

<?php 
 /*
 * Template Name: Testimonials
 */
 ?>

<?php get_header(); ?>
<div class="container">
<div class="content-page">
<h1><?php wp_title(); ?></h1>
<title><?php wp_title(); ?></title>
<?php
 $query = new WP_Query( array('post_type' => 'testimonials', 'posts_per_page' => 5 ) );
 while ( $query->have_posts() ) : $query->the_post(); ?>


<?php endwhile; ?>
</div>
</div>

<?php 
$args = array( 'post_type' => 'testimonials', 'posts_per_page' => 10 );
$the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?> 
</div>
<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>




<?php get_footer(); ?>

I want my page to show all the testimonials on the front end and I am not able to that and getting this error:

Parse error: syntax error, unexpected 'else' (T_ELSE)

Thanks guys!!!

  • Why do you have WordPress query with empty loop in there. – Robert hue Nov 5 '14 at 16:04
0

You are not closing the second while loop. You'll probably want something like this:

<?php 
 /*
 * Template Name: Testimonials
 */
 ?>

<?php get_header(); ?>

<div class="container">
    <div class="content-page">
        <h1><?php wp_title(); ?></h1>
        <title><?php wp_title(); ?></title>
        <?php
         $query = new WP_Query( array('post_type' => 'testimonials', 'posts_per_page' => 5 ) );
         while ( $query->have_posts() ) : $query->the_post(); ?>

        <?php endwhile; ?>
    </div>
</div>

<?php 
$args = array( 'post_type' => 'testimonials', 'posts_per_page' => 10 );
$the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <div class="entry-content">
            <?php the_content(); ?> 
        </div>
    <?php endwhile; ?>
<?php else:  ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

<?php get_footer(); ?>
  • 1
    wp_reset_postdata(); at the end of query. and endwhile; should be after the div container – Robert hue Nov 5 '14 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.