2016-09-30 70 views
0

希望我的描述能夠清楚!定義WP自定義帖子類型區域

我基本上正在嘗試創建一個區域來顯示投資組合工作。我已經在Wordpress中創建了一個自定義的帖子類型,並且希望將其帶到front-page.php。我指定了要顯示作品的區域(see image)。 深灰色的地方是我想放置投資組合物品的地方。每個灰色區域應顯示1種組合項目

我使用這個腳本在自定義類型後拉:

<?php 
$args = array('post_type' => 'Portfolio', 'posts_per_page' => 4); 
    $loop = new WP_Query($args); 
     while ($loop->have_posts()) : $loop->the_post(); 
echo '<div class="home-recent-thumb">'; the_post_thumbnail(); echo '</div>'; 
echo '<div class="home-recent-title">'; the_title(); echo '</div>'; 
echo '<div class="home-recent-copy">'; the_excerpt(); echo '</div>'; 
endwhile; 
?> 

如何指定在PHP領域,使其顯示裏面4個員額正確的元素?

回答

0

由於您的佈局不一定有利於傳統的「循環」功能 - 意思是說,您不會將結果放在一起 - 而且您還沒有提到任何外部庫(如砌體或同位素) - 我只是針對四個方格中的每一個進行個別查詢。

對於第一個自定義後類型方 - 它想:

$query = new WP_Query('post_type' => 'Portfolio', 'posts_per_page' => 1); 

而第二個(到第n)看起來像:

$query = new WP_Query('post_type' => 'Portfolio', 'posts_per_page' => 1, 'offset=1'); 

如果您抵消不斷提高。在我看來,這繼續保持動態,並且對於四個帖子來說足夠簡單。除此之外,您還可以跳入其他方塊的其他邏輯。

0
<?php 
$portfolioPosts = get_posts([ 
    'post_type' => 'Portfolio', 
    'posts_per_page' => 4 
]); 
//first section 
?> 
<div class="home-recent-thumb"><?php the_post_thumbnail($portfolioPosts[0]->ID); ?></div> 
<div class="home-recent-title"><?php echo $portfolioPosts[0]->post_title ?></div> 
<div class="home-recent-copy"><?php echo $portfolioPosts[0]->post_excerpt; ?></div> 
<?php 
//later in code 
//second section 
?> 
<div class="home-recent-thumb"><?php the_post_thumbnail($portfolioPosts[1]->ID); ?></div> 
<div class="home-recent-title"><?php echo $portfolioPosts[1]->post_title ?></div> 
<div class="home-recent-copy"><?php echo $portfolioPosts[1]->post_excerpt; ?></div> 
//et cetera 
相關問題