0

i'm building site on wordpress engine, and I need to display four loops on one page (every one from a different category). I try to write it smart, cause I don't want code to duplicate. Look at this:

http://codepad.org/aTh87QAN

As you can see, i don't' want to duplicate code that is in every loop, i mean lines 19-31.

<?php
    $events_sep = new WP_Query( array_merge($args_common, $args_sep) );
    while ($events_sep->have_posts()) : $events_sep->the_post();
?>
    <li class="event-entry date-<?php the_field('event-date'); ?>">
        <a href="index.php/<?php the_field('event-venue'); ?>">
            <img src="<?php bloginfo('template_url'); ?>/img/program/<?php the_field('event-venue'); ?>-22px.jpg" alt="">
        </a>
        <?php echo the_content(); ?>
        <a class="permalink" href="index.php/<?php the_field('event-venue'); ?>">więcej informacji</a>
        <div class="clearfix"></div>
    </li>
<?php endwhile; ?>

So i tried saving it as a php variable, but It doesn't worked. I'm not very experienced with php (not as good as javascript), but I can't look at this code, probably you know what I mean ;)

1 Answer 1

1

That's one of the things functions are for, doing repetitive tasks. Build one like this in your functions.php file:

function my_query_events( $common, $unique, $title, $class )
{
    echo "<h4>$title</h4>";
    echo "<ul class='events-list $class'>";
    $events_sep = new WP_Query( array_merge( $common, $unique ) );
    while ( $events_sep->have_posts() ) : $events_sep->the_post();
    ?>
    <li class="event-entry date-<?php the_field('event-date'); ?>">
        <a href="index.php/<?php the_field('event-venue'); ?>">
            <img src="<?php bloginfo('template_url'); ?>/img/program/<?php the_field('event-venue'); ?>-22px.jpg" alt="">
        </a>
        <?php echo the_content(); ?>
        <a class="permalink" href="index.php/<?php the_field('event-venue'); ?>">więcej informacji</a>
        <div class="clearfix"></div>
    </li>
    </ul>
    <?php 
    endwhile; 
}

And in your template call it a number of times passing your own specific arguments:

<ul class="program-info">
    <li class="pure-u-1-4 program-table">
        <?php my_query_events( $args_common, $args_sep, 'wrzesień', 'sep' ); ?>
        <?php my_query_events( $args_common, $args_oct, 'październik', 'oct' ); ?>

This example is untested, but gives you an idea on how to proceed and adapt to your needs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.