【问题标题】:Define WP Custom post type areas定义 WP 自定义帖子类型区域
【发布时间】:2016-09-30 19:47:28
【问题描述】:

希望我的描述会很清楚!

我基本上是在尝试创建一个展示作品集的区域。我在 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 个帖子?

【问题讨论】:

    标签: php jquery custom-post-type


    【解决方案1】:

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

    对于第一个自定义帖子类型正方形 - 它会:

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

    您的偏移量继续增加。在我看来,这将继续保持动态,并且对于四个帖子来说足够简单。否则,您将跳入其他方块的一堆附加逻辑中。

    【讨论】:

      【解决方案2】:
      <?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
      

      【讨论】:

        猜你喜欢
        • 2016-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-24
        • 2014-07-12
        相关资源
        最近更新 更多