[英]How to loop through custom posts in Wordpress and display according to page title
Does anyone know how you would display a custom post type according to what post type you are on? 有人知道您将根据自己所处的职位类型显示自定义职位类型吗?
For example if I go to my www.url.com/services/digital page I want to show the digital portfolio or if I go to my www.url.com/services/audio page I want to show the audio portfolio. 例如,如果我转到www.url.com/services/digital页面,我想显示数字资产组合;或者,如果我转到www.url.com/services/audio页面,我想显示音频资产。
Just to clarify I have two post types - 为了澄清我有两种职位类型-
The code I have wrote works fine but surely there must be a simpler way to do it as I have six categories I don't want to write out this code six different times on the same page 我编写的代码可以正常工作,但是肯定有一种更简单的方法可以实现,因为我有六个类别,我不想在同一页面上六次写出此代码
<?php
if ( is_single( 'digital' ) ) {
$the_query = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page'=>'3', cat=>'9') );
while ( $the_query->have_posts() ) : $the_query->the_post();
echo'<div class="group service_portfolio">';
echo'<div class="service_portfolio_left">';
echo '<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'Client'; echo'</h2>';
echo'<p style="color:#757573; margin-bottom:5%;">'; echo the_title(); echo'</p>';
echo '<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'What we done'; echo'</h2>';
echo '<p style="color:#757573; margin-bottom:5%;">'; echo the_field('what_was_done'); echo'</p>';
echo'<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'See for yourself'; echo'</h2>';
echo '<p class"bottom_p">'; echo'<a href="http://'; the_field('portfolio_url'); echo'">'; echo the_field('portfolio_url'); echo' </a>'; echo '</p>';
echo'</div>';
echo'<div class="service_portfolio_left" style="text-align:right;">';
echo the_post_thumbnail();
echo'</div>';
echo'</div>';
endwhile;
wp_reset_postdata();
}
?>
<!-- Output print work -->
<?php
if ( is_single( 'print' ) ) {
$the_query = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page'=>'3', cat=>'10') );
while ( $the_query->have_posts() ) : $the_query->the_post();
echo'<div class="group service_portfolio">';
echo'<div class="service_portfolio_left">';
echo '<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'Client'; echo'</h2>';
echo'<p style="color:#757573; margin-bottom:5%;">'; echo the_title(); echo'</p>';
echo '<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'What we done'; echo'</h2>';
echo '<p style="color:#757573; margin-bottom:5%;">'; echo the_field('what_was_done'); echo'</p>';
echo'<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'See for yourself'; echo'</h2>';
echo '<p class"bottom_p">'; echo'<a href="http://'; the_field('portfolio_url'); echo'">'; echo the_field('portfolio_url'); echo' </a>'; echo '</p>';
echo'</div>';
echo'<div class="service_portfolio_left" style="text-align:right;">';
echo the_post_thumbnail();
echo'</div>';
echo'</div>';
endwhile;
wp_reset_postdata();
}
?>
Sounds to me that you want to use a 6x archive templates ( archive-services.php
, archive-digital.php
etc.) since that is the standard way of presenting CPTs and it removes the need to the conditional and query parts. 在我看来,您想使用6x存档模板(
archive-services.php
, archive-digital.php
等),因为这是呈现CPT的标准方法,它消除了对条件和查询部分的需要。 Then in each one of those you can use a template part the markup within the loop; 然后,在每个模板中 ,您都可以在循环中使用模板部分的标记; this way you can reuse the same markup for all 6x templates easily, using:
这样,您可以使用以下方法轻松地对所有6x模板重用相同的标记:
<?php get_template_part( $slug, $name ); ?>
Doing it this way gives you some flexibility whilst avoiding all the repetition. 这样做可以为您提供一定的灵活性,同时避免所有重复。 So your 6x archive templates might look like this:
因此,您的6x存档模板可能如下所示:
<?php
get_header();
if(have_posts()) : while(have_posts()) : the_post();
get_template_part( 'loop', 'preview' );
endwhile; endif;
get_footer();
?>
That will load loop-preview.php
, which will contain the mark up to go within the loop. 这将加载
loop-preview.php
,其中将包含loop-preview.php
中的标记。
EDIT: @AndrewBartel's comment made me realised that I was over-thinking this. 编辑: @AndrewBartel的评论使我意识到,我对此考虑过多。 If the 6x categories you mentioned are only being used for your custom post types, then you can create 6x category templates instead (
category-digital.php
, category-audio.php
etc.) These will display all posts that are within that category. 如果您提到的6x类别仅用于自定义帖子类型,则可以改为创建6x类别模板(
category-digital.php
, category-audio.php
等)。这些模板将显示该类别内的所有帖子。 That will probably be the most straightforward way of handling it; 那可能是处理它的最直接的方法。 the template part bit is still valid.
模板部分位仍然有效。 Check the WP Codex entry for templates for more info.
有关模板的更多信息,请检查WP Codex条目 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.