繁体   English   中英

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

[英]Wordpress - Custom loop query for displaying custom post type

我搞砸了我的代码。 我的目标是在我创建的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>

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

<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

假设所需的帖子类型是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(); ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM