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 trialJulian Ptak
30,920 PointsAll four projects still displaying - Make a Wordpress Theme - content-work.php file missing from project files?
After I followed the last step in this video, (Adding Content in Custom Places - Part 2), the recent project section of the front page should only hold one project. But on my front page, all four projects are still showing up but after deleting the div tag they just get larger. Any ideas? Thanks in advance!
Edit - Another Problem: While trying to debug this myself, I replaced my code in the front-page.php file with the code in Zac's front-page.php file. This resulted in another bug where none of the projects showed up but instead a small grey bar appeared in their place. I then noted some changes from his code in the video and the code in the file. So I reverted to the code from the video for the front-page.php file and decided to try and replace the code in my content-work.php file with the code in his content-work.php file. Upon trying this, I realized that there doesn't seem to be any content-work.php file in the project files for this project. This really confused me because we had just been working with it in the video in question. I moved to the next video and checked those project files and still didn't see it there.
I'm nervous about going in and changing too much more stuff on my own without the help of someone who is more knowledgable about Wordpress than myself because the other day I had a problem with this very same project and when I tried to fix the issue myself, I ended up losing connection with my database, and being unable to connect to it again, lost all my settings and had to start the project from scratch. I REALLY don't want to have to do that again. Does anyone have any suggestions? Can someone link a moderator or Zac on this?
I just want to be clear, I'm sure this is an error on my part at some stage of this project. But I too new to Wordpress to really know where to go to debug this kind of stuff. Anybody have any ideas?
<?php get_header(); ?>
<div class="container clearfix">
<div class="grid_12 omega">
<h5>Featured Post</h5>
</div>
<?php
$args = array(
'post_type' => 'post',
'cat' => '-1',
'posts_per_page' => 1
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="push_2 grid_10 omega clearfix">
<article>
<?php get_template_part( 'content', 'post' );?>
</article>
</div>
<?php endwhile; endif; ?>
<div class="grid_12 omega clearfix">
<div class="grid_6 recent-post">
<article>
<h5>Recent Post</h5>
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'featured',
'posts_per_page' => 1
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'post' ); ?>
<?php endwhile; endif; ?>
</article>
</div>
<?php
$args = array(
'post_type' => 'work'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'work' ); ?>
<?php endwhile; endif; ?>
</div>
</div>
<?php get_template_part( 'content', 'testimonials' ); ?>
<?php get_footer(); ?>
Julian Ptak
30,920 PointsSure thing, Andrew! Thanks for taking the time to take a look. I added the code above from the front-page.php file which was the one I was editing when things went haywire.
1 Answer
Andrew Shook
31,709 PointsThe problem is in you args array for the work post type:
<?php
$args = array(
'post_type' => 'work'
);
$the_query = new WP_Query( $args );
?>
You need to and a limit to the query so that is only returns the most recent post. Since it will return the posts from newest to oldest that means you need to limit you query to 1 like so:
<?php
$args = array(
'post_type' => 'work',
'posts_per_page' => 1
);
$the_query = new WP_Query( $args );
?>
Julian Ptak
30,920 PointsGosh thanks. As always, it was nowhere near as complicated as I thought it would be. Thanks, Andrew!
Andrew Shook
31,709 PointsJulian, I'm not trying to discourage you from asking questions on the forum, but next time just to a quick google search first. For instance, google "wordpress limit query to one post". It'll probably be faster and if you plan on doing this for work, you need to practice your Google-fu. I've been doing this for 3 years and I still use google daily.
Julian Ptak
30,920 PointsI understand, and normally I would. I've noticed some irregularities on this particular project though. If you follow the directions by downloading things like, "flexslider" from the website, the difference in updates translates to a difference in code. I was stumped on a flexslider.css file for hours yesterday until I realized I had a much more up-to-date file and the line of code I was looking for simply wasn't there anymore. I had to go and download the older version and work with that.
Needless to say, this has made me a little bit gun-shy from going to resources other than Treehouse now. I just spoke with a moderator yesterday who said I should stick solely with the project files. And like I mentioned above, I've already had to restart this project once. I'm on the clock at work and I'm just hoping this doesn't set me back too far.
When it comes to other languages and projects, I use Google quite a bit. Thanks very much for taking the time to answer though. I really appreciate your insight and I'll see if I can narrow down more of the faults I find in my code before retreating to the forums.
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsHi again Julian Ptak, can you post the code you originally used that showed all four of the projects? It's always helpful for to see the code when trying to fix a problem.