Wordpress-用于显示自定义帖子类型的自定义循环查询

时间:2018-08-01 11:13:38

标签: php wordpress

我正在弄乱我的代码。我的目标是在我创建的HTML布局的首页上显示4种自定义帖子类型。这是我的代码。实际上,我可以获取href,但是我无法循环代码甚至达不到我的范围。

<div class="roundedframe ">
<div class="container-fluid">
         <div class="row"> 
 <div class="col-lg-4 col-sm-6">
                        <a  class="portfolio-box" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                <div class="portfolio-box-caption">


 <div class="portfolio-box-caption-content">
                                        <div class="project-category text-faded">
                                        Category
                                        </div> 
<div style="background-image: url('<?php the_post_thumbnail_url(); ?>');">
                                          <div class="project-name"> <?php // WP_Query arguments
$args = array(
    'name'               => 'case-studies',
    'nopaging'               => true,
    'posts_per_page'         => '4',
);

// The Query
$query = new WP_Query( $args );
while ( $query->have_posts() ) :  $query->the_post();
?>
                                            Project Name
                                           </div>
                             </div>
                          </div>
                    </a>

                </div>
            </div>
    </div>

</div>

 

2 个答案:

答案 0 :(得分:0)

您应该将代码放入循环区域。我所看到的,你也错过了最后的时光。

<div class="roundedframe ">
<div class="container-fluid">
         <div class="row"> 

<?php // WP_Query arguments
$args = array(
    'name' => 'case-studies',
    'nopaging' => true,
    'posts_per_page' => '4'
);

    // The Query
    $query = new WP_Query($args);
    while ($query->have_posts()):
        $query->the_post(); ?>
        <div class="col-lg-4 col-sm-6">
          <a  class="portfolio-box" href="<?php
    get_the_permalink();
    ?>" title="<?php
    get_the_title();
    ?>">
          <div class="project-category text-faded">
          Category
          </div> 
          <div style="background-image: url('<?php
    the_post_thumbnail_url();
    ?>');">
            <div class="project-name"> 
              Project Name
            </div>
          </div>
          </a>
        </div>
    <?php
    endwhile;
    ?>
    </div>
  </div>
</div><!--.roundedframe-->

尝试一下,让我知道。它可能会帮助您。在此之前,您应该了解wp_query

https://codex.wordpress.org/Class_Reference/WP_Query

答案 1 :(得分:0)

假设您想要的帖子类型为case-studies,则应将键命名为post_type,而不是name。您还必须将列放置在循环中,然后将其关闭。您还错过了一个</div>标签。

<?php $query = new WP_Query( [
    'post_type'      => 'case-studies',
    'nopaging'       => true,
    'posts_per_page' => '4',
] ); ?>

<?php if ( $query->have_posts() ) : ?>
    <div class="roundedframe ">
        <div class="container-fluid">
            <div class="row">

                <?php while ( $query->have_posts() ) : $query->the_post(); ?>

                    <div class="col-lg-4 col-sm-6">
                        <a class="portfolio-box" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                            <div class="portfolio-box-caption">
                                <div class="portfolio-box-caption-content">
                                    <div class="project-category text-faded">
                                        Category
                                    </div>
                                    <div style="background-image: url('<?php the_post_thumbnail_url(); ?>');">
                                        <div class="project-name">
                                            <h2><?php the_title(); ?></h2>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </a>
                    </div>

                <?php endwhile; ?>

            </div>
        </div>
    </div>
<?php endif; ?>

<?php wp_reset_postdata(); ?>