• Resolved dbenjamin

    (@dbenjamin)


    Hello,

    I would like to take benefit of the new get_template_part() function but i’m stuck with a case.

    On home page, i would like to do something similar to :

    ————————————
    News
    News loop

    ————————————
    Articles
    Other articles loop (not news)

    ————————————-

    News is a category and into articles loop i don’t want news.
    Into index.php file i would like to do something like :

    <section id="page">
        <section id="news">
            <h2>News</h2>
            <?php
                get_template_part('loop', '....');
            ?>
        </section>
        <section id="articles">
            <h2>Articles</h2>
            <?php
                get_template_part('loop', '....');
            ?>
        </section>
    </section>

    Is it possible to use the same loop with those conditions ? If yes, how please ? ??

    Thanks.

    br,
    Benjamin.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Not sure if you need two get_template_part things–why not just do that in your theme’s loop.php.

    The Twenty Ten theme’s loop.php has examples.

    Thread Starter dbenjamin

    (@dbenjamin)

    I can’t do that into the loop because the two sections are separated in real context.

    The home page is too complex and i need two different loops to get posts from “News” category and the others.

    I tried to do :

    <section id="page">
    			<section id="news" class="articles-bloc">
    				<h2>News</h2>
    				<ul>
    				<?php $posts = get_posts('category_name=news&numberposts=5&orderby=date'); ?>
    				<?php foreach($posts as $post) : ?>
    				<?php   setup_postdata($post); ?>
    						<li id="news-<?php the_ID(); ?>">
    							<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Voir : %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    						</li>
    				<?php endforeach; ?>
    				</ul>
    			</section>
    			<section id="articles" class="articles-bloc">
    				<h2>Articles</h2>
    				<?php
        			/* Run the loop to output the posts.
        			 * If you want to overload this in a child theme then include a file
        			 * called loop-index.php and that will be used instead.
        			 */
        			 get_template_part( 'loop', 'index' );
        			?>
    			</section>
    		</section>

    And into the loop :

    <?php while ( have_posts() ) : the_post(); ?>
    	<?php if ( ! in_category('news') ) : ?>
            <article .....
            <?php endif; ?>
    <?php endwhile; ?>

    I’ve got the news but nothing into “articles”. Don’t know why. Do i need to reset something before entering the loop ?

    Thanks.

    br,
    Benjamin.

    Well if you just want two loops then here’s one loop you could duplicate:

    <?php
    $cat_id = get_cat_ID('Articles');
    $args=array(
      'cat' => $cat_id,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Thread Starter dbenjamin

    (@dbenjamin)

    Hello,

    I’ll do that, thank you ??

    br,
    Benjamin.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Conditional get_template_part’ is closed to new replies.