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 trialKonrad Pilch
2,435 PointsHow can i use WP_Query to.. asign a template?
HI, how can i use WP_Query to assign a template when clicked? So imagine you have a sidebar, and you are looping thorw your custome fields, the sidebar is holding servvices like business consulting, digital marketing etc.. they all need to have the same layout. Its like with page.php. I have a template called page-services.php for all the services , but when i click its blank. But it loops in the sidebar and show them. SO how can i assign the website link so when i click it, it will bring me to the template with the corresponding information?
<aside class="col-xs-12 col-sm-12 col-md-3 margin-bottom-20-mobile">
<div class="service-component">
<?php
if($post->post_parent) {
$titlenamer = get_the_title($post->post_parent);
}
else {
$titlenamer = get_the_title($post->ID);
}
?>
<h3 class="services-heading"><?php echo $titlenamer; ?> </h3>
<ul class="list-unstyled vertical-services">
<?php
$list_args = array(
'post_type' => 'services',
'posts_per_page' => -1
);
$list_query = new WP_Query( $list_args );
// Loop
if ( $list_query->have_posts() ) : while ( $list_query->have_posts() ) : $list_query->the_post(); ?>
<li>
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
<span class="glyphicon glyphicon-menu-right service-arrow"></span>
</a>
</li>
<?php endwhile; else : ?>
<h3>Nothing to</h3>
<?php endif; ?>
</ul>
</div>
</aside>
1 Answer
Kevin Korte
28,149 PointsRefer to the template hierarchy: https://developer.wordpress.org/files/2014/10/template-hierarchy.png
WP is going to look for a certain template since this is a custom post type. What template is WP going to look for first?
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 Points$custom.php i beleive. since its a custome post type , theres a custome template and then goes to the slug and or id.
Kevin Korte
28,149 PointsKevin Korte
28,149 PointsNo, because these are not pages you created, these are posts. You are try to show a single post.
So if you follow the template hierarchy, we have a singular page >> custom post >> single-$posttype.php >> single.php >> singluar.php >> index.php
That's the order.
And if your custom post type is services, than the page template you should use is single-services.php
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsIt doesn't work : p
It's still blank page. Its only showing html, head and body tags that are empty. Like before.
I named it single-services.php
Kevin Korte
28,149 PointsKevin Korte
28,149 PointsWhat template is it trying to use?
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 Pointssingle.services.php its the same one when we created the page-services.php that page should be single-services.php.
But it displays nothing. In inspector it displays html, body and head tag, seem like something doesn't work or its not connected. I mean, the code on the sidebar, works, it just doesnt get the links to the template , ts just blank.
I did research it as well, and did some code there, but it doesn't work neither.
Kevin Korte
28,149 PointsKevin Korte
28,149 PointsWhile you're debugging this, I would put the template name in the html of the file, that way you can see what template wordpress is using. If it's trying to use post.php, or index.php than you have a problem with your naming conventions.
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsWhat do you mean?
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsNothing works on that site on that page after doing anything.