saragreenlaw
3/16/2017 - 1:46 AM

Custom Loops

Custom Loops


// Add our custom loop
add_action( 'genesis_loop', 'claire_homepage_loop' );

function claire_homepage_loop() {

	$args = array(
		'orderby'       => 'post_date',
		'order'         => 'DESC',
		'posts_per_page'=> '2', // overrides posts per page in theme settings
	);

	$loop = new WP_Query( $args );
	if( $loop->have_posts() ) {

		// this is shown before the loop
		echo '<h2>Read my latest blog posts</h2>';

		// loop through posts
		while( $loop->have_posts() ): $loop->the_post();

		echo '<h3><a href="' . get_the_permalink() .'">'. get_the_title() . '</a></h3>';
		echo '<p>' . get_the_date() . '</p>';
		echo '<p>' . get_the_excerpt() . '</p>';


		endwhile;

		// link to more posts is shown after the loop
		echo '<a href="./blog/">More from the blog</a>';
	}

	wp_reset_postdata();

}