Question

How could optimize the following code which is made on wordpress query_posts.

The general idea is to show all the posts in each category (also show all categories), with your name (title) of each category.

Note that there is also in the div added a class "alt"

    <div id="item">
        <div class="post">
            <div class="width">
                <?php query_posts("&cat=3"); ?>
                    <h2><?php echo single_cat_title('', false); ?></h2>
                    <div id="templates">
                        <?php $cont=0; while (have_posts()) : the_post(); ?><?php $cont++; ?>
                            <div class="item <?php my_post_class(); ?>">
                                <h3>Número de Código #<?php echo $cont; ?></h3>
                                <div class="cover">
                                    <a href="<?php the_permalink() ?>" target="_blank"><img src="<?php the_field('imagen'); ?>" /></a>
                                </div>
                                <p><a href="<?php the_permalink() ?>" target="_blank">Ver Demostración</a></p>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php wp_reset_query(); ?>
            </div>
        </div>
        <div class="post alt">
            <div class="width">
                <?php query_posts("&cat=2"); ?>
                    <h2><?php echo single_cat_title('', false); ?></h2>
                    <div id="templates">
                        <?php $cont=0; while (have_posts()) : the_post(); ?><?php $cont++; ?>
                            <div class="item <?php my_post_class(); ?>">
                                <h3>Número de Código #<?php echo $cont; ?></h3>
                                <div class="cover">
                                    <a href="<?php the_permalink() ?>" target="_blank"><img src="<?php the_field('imagen'); ?>" /></a>
                                </div>
                                <p><a href="<?php the_permalink() ?>" target="_blank">Ver Demostración</a></p>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php wp_reset_query(); ?>
            </div>
        </div>
        <div class="post">
            <div class="width">
                <?php query_posts("&cat=7"); ?>
                    <h2><?php echo single_cat_title('', false); ?></h2>
                    <div id="templates">
                        <?php $cont=0; while (have_posts()) : the_post(); ?><?php $cont++; ?>
                            <div class="item <?php my_post_class(); ?>">
                                <h3>Número de Código #<?php echo $cont; ?></h3>
                                <div class="cover">
                                    <a href="<?php the_permalink() ?>" target="_blank"><img src="<?php the_field('imagen'); ?>" /></a>
                                </div>
                                <p><a href="<?php the_permalink() ?>" target="_blank">Ver Demostración</a></p>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php wp_reset_query(); ?>
            </div>
        </div>
        <div class="post alt">
            <div class="width">
                <?php query_posts("&cat=8"); ?>
                    <h2><?php echo single_cat_title('', false); ?></h2>
                    <div id="templates">
                        <?php $cont=0; while (have_posts()) : the_post(); ?><?php $cont++; ?>
                            <div class="item <?php my_post_class(); ?>">
                                <h3>Número de Código #<?php echo $cont; ?></h3>
                                <div class="cover">
                                    <a href="<?php the_permalink() ?>" target="_blank"><img src="<?php the_field('imagen'); ?>" /></a>
                                </div>
                                <p><a href="<?php the_permalink() ?>" target="_blank">Ver Demostración</a></p>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php wp_reset_query(); ?>
            </div>
        </div>
        <div class="post">
            <div class="width">
                <?php query_posts("&cat=4"); ?>
                    <h2><?php echo single_cat_title('', false); ?></h2>
                    <div id="templates">
                        <?php $cont=0; while (have_posts()) : the_post(); ?><?php $cont++; ?>
                            <div class="item <?php my_post_class(); ?>">
                                <h3>Número de Código #<?php echo $cont; ?></h3>
                                <div class="cover">
                                    <a href="<?php the_permalink() ?>" target="_blank"><img src="<?php the_field('imagen'); ?>" /></a>
                                </div>
                                <p><a href="<?php the_permalink() ?>" target="_blank">Ver Demostración</a></p>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php wp_reset_query(); ?>
            </div>
        </div>
        <div class="post alt">
            <div class="width">
                <?php query_posts("&cat=5"); ?>
                    <h2><?php echo single_cat_title('', false); ?></h2>
                    <div id="templates">
                        <?php $cont=0; while (have_posts()) : the_post(); ?><?php $cont++; ?>
                            <div class="item <?php my_post_class(); ?>">
                                <h3>Número de Código #<?php echo $cont; ?></h3>
                                <div class="cover">
                                    <a href="<?php the_permalink() ?>" target="_blank"><img src="<?php the_field('imagen'); ?>" /></a>
                                </div>
                                <p><a href="<?php the_permalink() ?>" target="_blank">Ver Demostración</a></p>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php wp_reset_query(); ?>
            </div>
        </div>
        <div class="post">
            <div class="width">
                <?php query_posts("&cat=16"); ?>
                    <h2><?php echo single_cat_title('', false); ?></h2>
                    <div id="templates">
                        <?php $cont=0; while (have_posts()) : the_post(); ?><?php $cont++; ?>
                            <div class="item <?php my_post_class(); ?>">
                                <h3>Número de Código #<?php echo $cont; ?></h3>
                                <div class="cover">
                                    <a href="<?php the_permalink() ?>" target="_blank"><img src="<?php the_field('imagen'); ?>" /></a>
                                </div>
                                <p><a href="<?php the_permalink() ?>" target="_blank">Ver Demostración</a></p>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php wp_reset_query(); ?>
            </div>
        </div>
        <div class="post alt">
            <div class="width">
                <?php query_posts("&cat=15"); ?>
                    <h2><?php echo single_cat_title('', false); ?></h2>
                    <div id="templates">
                        <?php $cont=0; while (have_posts()) : the_post(); ?><?php $cont++; ?>
                            <div class="item <?php my_post_class(); ?>">
                                <h3>Número de Código #<?php echo $cont; ?></h3>
                                <div class="cover">
                                    <a href="<?php the_permalink() ?>" target="_blank"><img src="<?php the_field('imagen'); ?>" /></a>
                                </div>
                                <p><a href="<?php the_permalink() ?>" target="_blank">Ver Demostración</a></p>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php wp_reset_query(); ?>
            </div>
        </div>
        <div class="post">
            <div class="width">
                <?php query_posts("&cat=9"); ?>
                    <h2><?php echo single_cat_title('', false); ?></h2>
                    <div id="templates">
                        <?php $cont=0; while (have_posts()) : the_post(); ?><?php $cont++; ?>
                            <div class="item <?php my_post_class(); ?>">
                                <h3>Número de Código #<?php echo $cont; ?></h3>
                                <div class="cover">
                                    <a href="<?php the_permalink() ?>" target="_blank"><img src="<?php the_field('imagen'); ?>" /></a>
                                </div>
                                <p><a href="<?php the_permalink() ?>" target="_blank">Ver Demostración</a></p>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php wp_reset_query(); ?>
            </div>
        </div>
        <div class="post alt">
            <div class="width">
                <?php query_posts("&cat=6"); ?>
                    <h2><?php echo single_cat_title('', false); ?></h2>
                    <div id="templates">
                        <?php $cont=0; while (have_posts()) : the_post(); ?><?php $cont++; ?>
                            <div class="item <?php my_post_class(); ?>">
                                <h3>Número de Código #<?php echo $cont; ?></h3>
                                <div class="cover">
                                    <a href="<?php the_permalink() ?>" target="_blank"><img src="<?php the_field('imagen'); ?>" /></a>
                                </div>
                                <p><a href="<?php the_permalink() ?>" target="_blank">Ver Demostración</a></p>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php wp_reset_query(); ?>
            </div>
        </div>
    </div>

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top