1

in a custom plugin I have a WPQuery, which looks like the following:

$sticky = get_option( 'sticky_posts' );
    $args = array(
        'post_type' => 'mission',
        'ignore_sticky_posts' => 1,
        'orderby' => 'date',
        'post__not_in' => $sticky,
        'posts_per_page' => $count
    );

    $latest_missions = new WP_Query( $args );
    if ( $latest_missions->have_posts() ) {
        while ( $latest_missions->have_posts() ) {
            $latest_missions->the_post();
            get_template_part( 'content-list', get_post_format() );
        }

        wp_reset_postdata();
    }

and the corresponding template part:

<li  id="post-<?php the_ID(); ?> " class="future-event"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></li>

The only problem I have is, that this will no produce semantic correct HTML, because the list is not sematically correct.

Is there a way, how I can create an <ul></ul> which is wrapped around the list, when my loop is finished?

BR, mybecks

2

Of course there is. Just do it like this:

$sticky = get_option( 'sticky_posts' );
$args = array(
    'post_type' => 'mission',
    'ignore_sticky_posts' => 1,
    'orderby' => 'date',
    'post__not_in' => $sticky,
    'posts_per_page' => $count
);

$latest_missions = new WP_Query( $args );
if ( $latest_missions->have_posts() ) {
    echo '<ul>';  // prints opening ul tag before list items
    while ( $latest_missions->have_posts() ) {
        $latest_missions->the_post();
        get_template_part( 'content-list', get_post_format() );
    }
    echo '</ul>';  // prints closing ul tag after list items

    wp_reset_postdata();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.