![](/img/trans.png)
[英]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.