【问题标题】:Wordpress displaying all posts from a custom post typeWordpress 显示来自自定义帖子类型的所有帖子
【发布时间】:2016-03-04 22:18:16
【问题描述】:

我是 wordpress 的新手,我创建了一个自定义帖子类型“projecten”,现在我想在我创建的页面上显示此类型的所有帖子。

我制作了一个页面模板,一切都很好,并创建了 1 个帖子。 这就是我的页面模板的样子。

<?php
/**
* Template Name: Projecten Template
*/

$projecten = new WP_Query(array('post_type' => 'project'));
wp_reset_postdata();
?>

<?php while (have_posts()) : the_post(); ?>
 <div class='full parallax' style='background-image: url(images/@stock/portfolio-header-bg.jpg); color: #fff;'>
    <div class='row'>
      <div class='twelve columns'>
        <div class='big mod modSectionHeader'>
          <div class='special-title centered-text'>
            <h2 style='color: #fff'>
              <?php the_title(); ?>
            </h2>
          </div>
          <h3 class='centered-text' style='color: #fff'><?php the_field('field_56d983a5a4788'); ?></h3>
        </div>
      </div>
    </div>
    <div class='four spacing'></div>
  </div>
  <div class='mod modGallery'>
      <?php if ( $projecten->have_posts()): ?>
       <?php while ( $projecten->have_posts()): the_post(): ?>
        <ul class='gallery large-block-grid-4 medium-block-grid-3 small-block-grid-2'>
          <li class='graphic-design'>
            <a href='portfolio-item.html'>
              <img width="400" height="400" alt="" src="" />
              <div class='overlay' >
                <div class='thumb-info'>
                  <h3><?php echo get_the_title(); ?></h3>
                  <p>hey</p>
                </div>
              </div>
            </a>
          </li>
        </ul>
       <?php endwhile; ?>
      <?php endif; ?>
     </div>
    <?php endwhile; ?>

对我来说看起来不错,但不幸的是它不起作用:(

【问题讨论】:

  • 你试过删除 wp_reset_postdata();
  • 当我这样做时它可以工作,但不使用 wp_reset_postdata(); 不是坏习惯?
  • 检查我编辑的答案。
  • 您说您的 CPT 是“投影”,但您使用的是 'post_type' =&gt; 'project'

标签: php wordpress


【解决方案1】:

删除 wp_reset_postdata();它会在关闭循环后出现。 你忘了这个

$projecten = new WP_Query(array('post_type' => 'project'));
while ($projecten->have_posts()) : $projecten->the_post(); 

【讨论】:

    【解决方案2】:

    这是对您的代码的编辑,我认为这会对您有所帮助。您错过了查询中“projecten”的“en”。

    我还将循环包裹在 li 标记而不是 ul 标记周围,并进行了一些其他调整。

    <?php
    /**
    * Template Name: Projecten Template
    */
    
    $projecten = new WP_Query(array('post_type' => 'projecten'));
    ?>
    <div class='full parallax' style='background-image: url(<?php  bloginfo('template_url');?>/stock/portfolio-header-bg.jpg); color: #fff;'>
        <div class='row'>
            <div class='twelve columns'>
                <div class='big mod modSectionHeader'>
                    <div class='special-title centered-text'>
                        <h2 style='color: #fff'><?php the_title(); ?></h2>
                    </div>
                    <h3 class='centered-text' style='color: #fff'><?php the_field('field_56d983a5a4788'); ?></h3>
                </div>
            </div>
        </div>
        <div class='four spacing'></div>
    </div>
    <div class='mod modGallery'>
        <?php if ( $projecten>have_posts() ) { ?>
        <ul class='gallery large-block-grid-4 medium-block-grid-3 small-block-grid-2'>
            <?php while ( $projecten->have_posts() ) { ?>
            <li class='graphic-design'>
                <a href='portfolio-item.html'>
                    <img width="400" height="400" alt="" src="" />
                    <div class='overlay' >
                        <div class='thumb-info'>
                            <h3><?php the_title(); ?></h3>
                            <p>hey</p>
                        </div>
                    </div>
                </a>
            </li>
            <?php endwhile; ?>
        </ul>
        <?php endif; ?>
        <?php wp_reset_postdata(); ?>
    </div>
    

    【讨论】:

    【解决方案3】:
    <?php
    /**
    * Template Name: Projecten Template
    */
    
    $projecten = new WP_Query(array('post_type' => 'project'));
    ?>
    
    <?php while (have_posts()) : the_post(); ?>
     <div class='full parallax' style='background-image: url(images/@stock/portfolio-header-bg.jpg); color: #fff;'>
        <div class='row'>
          <div class='twelve columns'>
            <div class='big mod modSectionHeader'>
              <div class='special-title centered-text'>
                <h2 style='color: #fff'>
                  <?php the_title(); ?>
                </h2>
              </div>
              <h3 class='centered-text' style='color: #fff'><?php the_field('field_56d983a5a4788'); ?></h3>
            </div>
          </div>
        </div>
        <div class='four spacing'></div>
      </div>
      <div class='mod modGallery'>
          <?php if ( $projecten->have_posts()): ?>
           <?php while ( $projecten->have_posts()): the_post(): ?>
            <ul class='gallery large-block-grid-4 medium-block-grid-3 small-block-grid-2'>
              <li class='graphic-design'>
                <a href='portfolio-item.html'>
                  <img width="400" height="400" alt="" src="" />
                  <div class='overlay' >
                    <div class='thumb-info'>
                      <h3><?php echo get_the_title(); ?></h3>
                      <p>hey</p>
                    </div>
                  </div>
                </a>
              </li>
            </ul>
           <?php endwhile; ?>
          <?php endif; ?>
         </div>
        <?php endwhile; 
    wp_reset_postdata();
    ?>
    

    并通过传递 post_id 获取如下特色图片

    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
    

    将您的单个页面创建为 single-.php,您将获得与该特定页面相关的页面中的所有相关内容。

    作为参考,您可以查看twentyfifteent主题中的single.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多