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 - Adding Content in Custom Places

Is there the possibility to include an excerpt of the work description on front page? I have edited custom field group work to display excerpt but have yet to see it appear on front page. I tried to figure what the correct code would be to use in front-page.php. I read wordpress codex "Function Reference/the excerpt" http://codex.wordpress.org/Template_Tags/the_excerpt

After reading I have surmised that "the_excerpt " can be used interchangeably with "content' from the get_template_part function.

<?php get_template_part( 'content', 'work'); ?>

I have not come across the correct format to make it work. I have used

<?php get_tempalte_part( 'excerpt', 'work');?>

<?php get_tempalte_part( 'the_excerpt', 'work');?>

Am I correct in my assumption that "the_template" can be used in this loop? Do I need to edit code else where in loop?

6 Answers

Jason Wendel
Jason Wendel
4,408 Points

Andrea, the get_template_part function is used to load a template part into your template, not the content or excerpt of a post (unless you are using 'the_content' or 'the_excerpt' in your template part). What your code is saying above is to pull in the code from this file: content-work.php into whatever file the code above sits.

Check out http://codex.wordpress.org/Function_Reference/get_template_part for more info about the get template part function.

Yes, I am trying to use the get_template function to load a template part into my front page. The part I want is the content from the work post. But I do not want the entire content displayed. The lengthy description is fine in the single work page but does not look good in the slider nor the recent work on front page.php. So my question is can I get an excerpt of the content from work post?

Jason Wendel
Jason Wendel
4,408 Points

Ok, I think I see what you are trying to do, but again, the get_template_part function pulls in a template part from your theme, not a post. If you want to pull in the excerpt from a specific post, you need to query the post with the post id of your work post. Something like this:

  $post_id = 1;  //this is the post id of the post you want to pull in.
  $queried_post = get_post($post_id);
  $title = $queried_post->post_title;
  echo $title;
  echo $queried_post->post_excerpt; //if you want to pull in the full post, change "post_excerpt" to "post_content"
Anthony Moore
Anthony Moore
2,282 Points


In your examples you would need to create a file called "content-work.php" in your theme folder.

So what you can do is create that file and inside write something like

<div class="excerpt">
<?php the_excerpt(); ?>

and then inside of your front-page.php file, wherever you want to display the post excerpts you add

<?php get_template_part( 'content', 'work'); ?>
Jason Wendel
Jason Wendel
4,408 Points

Ah, I see what you are trying to do. Sorry, I misunderstood, I thought you wanted to show one particular post on the front page. Yes, do what Anthony stated above.

Thank you Anthony and Jason! I will see if the above works for me.