自定义帖子类型出现在Archive.php上的前一个帖子类型中

时间:2019-04-08 作者:JakePowell

这是我的第一个自定义主题,所以我仍然在想这个循环,但我已经让每个连续的帖子围绕前一个出现了。我使用的是引导卡,所以每张新卡的边框都围绕着前一张卡,产生了嵌套效果。

如何停止此操作,并将每个CPT输出为新卡?

enter image description here

<?php
get_header();

if (have_posts()) :
   while (have_posts()) :
    ?>
    <div class="card container">
        <a href="<?php

        // this conditional outputs a different link url for CPT \'resources\'

            if (is_post_type_archive( $resources )) {
                echo \'https://\' .  get_field(\'website\');
            } else {
                echo the_permalink();
            }
        ?>">
        <div class="card-body row"><?php      
        the_post();

            ?>
            <div class="col-sm img-fluid"><?php 
                the_post_thumbnail(\'medium\');
            ?></div>
            <div class="col-sm p-2 m-0"> 
            <h2 class="card-title text-center"><?php the_title(); ?></h2>
            </a>
            <?php


// This section is supposed to get all custom fields and display them if they exist

    $fields = get_fields();
            if( $fields ): ?>
                <ul>
                    <?php foreach( $fields as $name => $value ): ?>
                    <li><?php
                    echo $value;
                    ?></li>
                    <?php endforeach; ?>
                </ul>
            <?php 
            endif;          
            ?><p class="card-text"><?php the_excerpt(); ?></p>
            </div><?php

    endwhile;
endif;

?>
        </div> <!-- card body -->
    </div><!-- card -->
<?php

get_footer();

?>
谢谢!

1 个回复
最合适的回答,由SO网友:mrben522 整理而成

卡和卡体div的结束标记位于循环之外。尝试以下操作:

<?php
get_header();

if (have_posts()) :
   while (have_posts()) :
    ?>
    <div class="card container">
        <a href="<?php

        // this conditional outputs a different link url for CPT \'resources\'

            if (is_post_type_archive( $resources )) {
                echo \'https://\' .  get_field(\'website\');
            } else {
                echo get_the_permalink();
            }
        ?>">
        <div class="card-body row"><?php      
        the_post();

            ?>
            <div class="col-sm img-fluid"><?php 
                the_post_thumbnail(\'medium\');
            ?></div>
            <div class="col-sm p-2 m-0"> 
            <h2 class="card-title text-center"><?php the_title(); ?></h2>
            </a>
            <?php


// This section is supposed to get all custom fields and display them if they exist

    $fields = get_fields();
            if( $fields ): ?>
                <ul>
                    <?php foreach( $fields as $name => $value ): ?>
                    <li><?php
                    echo $value;
                    ?></li>
                    <?php endforeach; ?>
                </ul>
            <?php 
            endif;          
            ?><p class="card-text"><?php the_excerpt(); ?></p>
            </div>
        </div> <!-- card body -->
    </div><!-- card -->
    <?php
    endwhile;
endif;

?>

<?php

get_footer();

?>

相关推荐

The Loop not looping?

在进行了大量的故障排除和搜索之后,我想我终于明白了如何进行循环。但我似乎无法让它真正循环!我创建了一个子主题,并添加了functions.php 文件中包含以下代码:<?php function my_theme_enqueue_styles() { $parent_style = \'grow-thinkup-style-minimal\'; // This is \'twentyfifteen-style\' for the Twenty Fifteen the