get posts - A smarter way to display multiple loops inside a page template

I'm trying to make a page with multiple loops. I need each loop to display posts from a particular category. One ca

I'm trying to make a page with multiple loops. I need each loop to display posts from a particular category. One category after another.

At the moment I have the loop below displaying posts from a category named "one". To display posts from category "two" and so on, I'm repeating the entire loop just changing the category_name.

I imagine that this is a very bad approach taking into consideration the DRY principle and resources usage. However, I can't figure out a smarter way to write this loop.

$posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'         => 'post',
    'category_name'     => 'one',
    'orderby'           => 'rand'
));

if ($posts) : ?>

    <div class="dt-loop-container">

        <?php foreach ($posts as $post) :

            setup_postdata($post);

        ?>

            <div class="dt-loop-item">
                <div class="dt-loop-logo">
                    <?php if (get_field('logo')) : ?>
                        <img src="<?php the_field('logo'); ?>" alt="Logo">
                    <?php endif; ?>
                </div>
                <h3 class="dt-loop-title">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </h3>
                <ul class="dt-loop-especialidades">
                    <?php if (get_field('especialidade_1')) : ?>
                        <li><?php the_field('especialidade_1'); ?></li>
                    <?php endif; ?>
                    <?php if (get_field('especialidade_2')) : ?>
                        <li><?php the_field('especialidade_2'); ?></li>
                    <?php endif; ?>
                    <?php if (get_field('especialidade_2')) : ?>
                        <li><?php the_field('especialidade_3'); ?></li>
                    <?php endif; ?>
                </ul>
            </div>

        <?php endforeach; ?>

    </div>

    <?php wp_reset_postdata(); ?>

<?php endif; ?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744538116a4579552.html

相关推荐

  • get posts - A smarter way to display multiple loops inside a page template

    I'm trying to make a page with multiple loops. I need each loop to display posts from a particular category. One ca

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信