Skip to content

Instantly share code, notes, and snippets.

@davidvandenbor
Forked from bencooling/Wordpress Loops
Last active August 29, 2015 14:03
Show Gist options
  • Save davidvandenbor/5be5f15dd888bcd3fba1 to your computer and use it in GitHub Desktop.
Save davidvandenbor/5be5f15dd888bcd3fba1 to your computer and use it in GitHub Desktop.
WordPress: Loops
<?php /* Main loop ------------------------------------------*/ ?>
<?php if (have_posts()) while (have_posts()): the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php // get_template_part( 'content', 'home' ); ?>
<?php endwhile; ?>
<?php /* Simple alter main loop ------------------------------------------*/ ?>
<?php query_posts('posts_per_page=1&post_type=locations'); ?>
<?php if(have_posts()) while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php // get_template_part( 'content', 'home' ); ?>
<?php the_content('read more...'); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
Modifying the query on Category Page
<?php $cat = get_query_var('cat'); query_posts('orderby=menu_order&order=ASC&cat='.$cat); ?>
<?php /* Simple create multiple loops ------------------------------------------*/ ?>
<?php
global $post;
$args = array( 'posts_per_page' => 5 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
<h1><?php the_title(); ?></h1>
<?php endforeach; wp_reset_postdata(); ?>
<?php /* Multiple loops with pagination ------------------------------------------*/ ?>
<?php
// sidebar fix
global $paged;
// home page fix
if ( get_query_var('paged') ) $paged = get_query_var('paged');
else if ( get_query_var('page') ) $paged = get_query_var('page');
else $paged = 1;
$ppp = get_option('posts_per_page');
$tmp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query(array( 'posts_per_page' => 2, 'paged' => $paged ));
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<h1><?php the_title(); ?></h1>
<?php // get_template_part( 'content', 'home' ); ?>
<?php endwhile; ?>
<?php // previous_posts_link('&larr; Previous Post'); ?>
<?php // next_posts_link('Next Post &rarr;'); ?>
<?php // wp_pagenavi( array( 'query' => $wp_query ) ); ?>
<?php
$wp_query = null;
$wp_query = $tmp;
?>
<?php /* Various Looping-related fixes ------------------------------------------*/ ?>
<?php
/**
* Use 'read more' in a post listing on a page
* See: http://codex.wordpress.org/Customizing_the_Read_More#How_to_use_Read_More_in_Pages
*/
?>
<?php
global $more;
query_posts('posts_per_page=1&post_type=locations');
$more=0;
// ...
?>
<?php /* Get Single page ------------------------------------------*/ ?>
<?php
// Get front | home pages
$frontpage_id = get_option('page_on_front');
$homepage_id = get_option('page_for_posts');
$page = get_page($frontpage_id);
// Get page by Title
$page = get_page_by_title('Our Service Areas in Sydney');
?>
<h2><?php echo $page->post_title; ?></h2>
<?php echo apply_filters('the_content', $page->post_content); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment