首页 > 解决方案 > 在 Wordpress 上显示来自特定类别的随机帖子

问题描述

我正在使用以下代码在每次刷新时显示来自“主页”类别中的随机帖子的两个图像(我已使用高级自定义字段设置)。但是它似乎不起作用,当我用谷歌搜索它时,有很多不同的方法可以做到!有人知道我的看起来是否正确吗?我担心我可能会丢失一个结束标签或一些小东西。

<div class="feature-container">

    <?php 
    $args = array(
    'posts_per_page' => 1, 
    'orderby' => 'rand', 
    'category_name' => 'homepage',
    );

    $loop = new WP_Query($args); 

    while ($loop->have_posts()) : $loop->the_post();

    if( have_rows('images') ):

        while ( have_rows("images") ) : 
            the_row();

            if ( get_sub_field( 'image' ) ): ?>

                <a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>">
                    <div class="image feature-item">
                        <div class="overlay" style="background-color:<?php the_sub_field('hover_colour');?>">
                            <div class="caption pm-big">

                                <?php 

                                $link = get_sub_field('link');

                                if( $link ): ?>

                                    <?php echo $link['title']; ?>

                                <?php endif; ?>

                            </div>
                        </div>
                        <img src="<?php the_sub_field('image');?>" alt="" style="display: block;">
                    </div>
                </a>

        <?php else: ?>
            <!-- no images found -->
        <?php endif; 

            endwhile;

        else :

        // no images found

        endif;

    endwhile; ?>
</div>

标签: htmlwordpress

解决方案


推荐阅读