Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development Custom Post Type Templates in WordPress The Portfolio Homepage

lihaoquan
lihaoquan
12,045 Points

Portfolio images not showing : WP_Query for post-type portfolio gives error

I've rewatched this video and re-did the steps for making custom post types over and over again, however, when WP_Query-ing for portfolio items to display, it didn't give me anything.

I've used print_r function on $query and one section of it seems mysterious to me

[query_vars] => Array ( [post-type] => portfolio [error]

Does this means that there is error when querying for posts that are type of 'portfolio'?

Also, when I opened the chrome inspector, I saw "the_permalink();" function outputting the link to the wrong place. Instead of giving me the link for portfolio type posts, it gave me links for normal type posts, here is what it outputted :

<div class="small-6 medium-4 large-3 columns grid-item">
        <a href="http://localhost/wordpress/2015/05/12/hello-world/"></a>
    </div>

Here is my file for page-portfolio.php

<?php 
/*
    Template Name: Portfolio Page
*/
?>
<?php get_header(); ?>

    <section class="row">
      <div class="small-12 columns text-center">
        <div class="leader">

        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>

        <?php endwhile; endif; ?>

        </div>
      </div>
    </section>

<?php

    $args = array(
        'post-type' => 'portfolio'
    );

    $query = new WP_Query( $args );

?>

<section class="row no-max pad">

    <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();  ?>  

    <div class="small-6 medium-4 large-3 columns grid-item">
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('large'); ?></a>
    </div>

    <?php endwhile; endif; wp_reset_postdata(); ?>

</section>

<?php get_footer(); ?>

I'm not sure if there's anything wrong with this code. If there is nothing wrong, it means that there is something wrong with my wordpress settings. Can anyone help me?

1 Answer

You can use get_posts which give more speed and better to use

Learn More here https://codex.wordpress.org/Template_Tags/get_posts