[英]Wordpress: Match custom field to page title and display related posts
[英]How to loop through custom posts in Wordpress and display according to page title
有人知道您将根据自己所处的职位类型显示自定义职位类型吗?
例如,如果我转到www.url.com/services/digital页面,我想显示数字资产组合;或者,如果我转到www.url.com/services/audio页面,我想显示音频资产。
为了澄清我有两种职位类型-
我编写的代码可以正常工作,但是肯定有一种更简单的方法可以实现,因为我有六个类别,我不想在同一页面上六次写出此代码
<?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();
}
?>
在我看来,您想使用6x存档模板( archive-services.php
, archive-digital.php
等),因为这是呈现CPT的标准方法,它消除了对条件和查询部分的需要。 然后,在每个模板中 ,您都可以在循环中使用模板部分的标记; 这样,您可以使用以下方法轻松地对所有6x模板重用相同的标记:
<?php get_template_part( $slug, $name ); ?>
这样做可以为您提供一定的灵活性,同时避免所有重复。 因此,您的6x存档模板可能如下所示:
<?php
get_header();
if(have_posts()) : while(have_posts()) : the_post();
get_template_part( 'loop', 'preview' );
endwhile; endif;
get_footer();
?>
这将加载loop-preview.php
,其中将包含loop-preview.php
中的标记。
编辑: @AndrewBartel的评论使我意识到,我对此考虑过多。 如果您提到的6x类别仅用于自定义帖子类型,则可以改为创建6x类别模板( category-digital.php
, category-audio.php
等)。这些模板将显示该类别内的所有帖子。 那可能是处理它的最直接的方法。 模板部分位仍然有效。 有关模板的更多信息,请检查WP Codex条目 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.