1

Can anyone spot anything wrong with this code(see below). No content is showing up on my page. Many thanks in advance.

<?php 
    $args = array(
    'posts_per_page' => 3,
    'orderby' => 'rand',
    'category_name' => 'testimonials'
    );

    $query = new WP_Query( $args );
        // The Loop
        if ( $query->have_posts() ) {
            echo '<ul class="testimonials">';
        while ( $query->have_posts() ) {
            $query->the_post();
            echo '<li class="clear">';
            echo '<figure class="testimonial-thumb">';
            the_post_thumbnail('testimonial-mug');
            echo '</figure>';
            echo '<aside class="testimonial-text">';
            echo '<h3 class="testimonial-name">' . get_the_title() . '</h3>';
            echo '<div class="testimonial-excerpt">';
            the_content('');
            echo '</div>';
            echo '</aside>';
            echo '</li>';
        }
        echo '</ul>';
    }
    wp_reset_postdata();
?>
1

There aren't any errors in your code. I'm able to copy / paste it into functions.php and return 3 posts categorized under "testimonials" without issue.

By default this WP_Query will retrieve only 'post' matches so if you are using pages or a custom post type you'll need to specify that in your query args as:

$args['post_type'] = 'page';

or

$args['post_type'] = 'custom_post_type_name';

The only other thing to check is to ensure you've added posts to the "testimonials" category in the WordPress dashboard.

I hope this helps.

|improve this answer|||||
0

I have done the same with get_posts() method. Here is the code i used to get posts on my page. I was filtering my Posts according to their categories, so i gave the category slug in category_name, you can leave it blank if you want all your posts to be shown.

<?php $args = array(
    'posts_per_page'   => 5,
    'offset'           => 0,
    'category'         => '',
    'category_name'    => '{category slug}',
    'orderby'          => 'date',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'author'       => '',
    'post_status'      => 'publish',
    'suppress_filters' => true 
);
$lastposts = get_posts( $args );
        foreach($lastposts as $post) :
        setup_postdata($post); ?>

        <li<?php if ( $post->ID == $wp_query->post->ID )
         { 
            echo ' class="current"'; 

         } else {} ?>>

            <a href="<?php the_permalink() ?>"><?php 
            the_title();


             ?></a>
             <?php
             the_excerpt();
             ?>

        </li>

    <?php endforeach; ?>
|improve this answer|||||

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.