定义WP自定义帖子类型区域


Define WP Custom post type areas

希望我的描述将是清楚的!

我基本上试图创建一个区域的投资组合工作要显示。我在Wordpress中创建了一个自定义的帖子类型,我想把它带到首页。php。我已经指定了我想要展示作品的区域(见图)。深灰色区域是我只想放置投资组合项目的地方。每个灰色区域应该显示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个帖子内的正确元素?

由于您的布局不一定有利于传统的"循环"功能-意思是,您没有将结果放在彼此旁边-并且您没有提到任何外部库(如砌体或同位素)-我将只对四个正方形中的每个进行单独查询。

对于第一个自定义post类型square -它应该是:

$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' );

偏移量继续增加的地方。在我看来,这仍然是动态的,并且对于四篇文章来说足够简单。否则你就会跳到一堆额外的逻辑与其他方块。

<?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