WordPress Loops

Custom Loop

$args = array(
    'posts_per_page' => '-1',
    'post_type' => 'form',
);

$myForms = new WP_Query($args);
while ($myForms->have_posts()){
    $myForms->the_post();

    the_title();
    the_content();
}


Standard loop

if (have_posts()) {
    while (have_posts()) {
        echo '<br>';
        the_title();
        echo '<br>';
        the_post();
        echo '<br>';
        the_time('F j, y');
        echo 'at: ';
        the_time('g:i a');
        echo '<br>';
        the_content();
        echo '<br>';
    }
} else {
    // if no posts
    echo 'there are no posts';
}

Template Hierarchy

WordPress uses temlates to display posts, pages and custom post types. It will decide what template to use by matching up the .php file name with a variety of post attributes. You may need to use flush_rewrite_rules() before a new template will properly register. The templates lower down on the list are less specific, worpress will fall back on these if it doesn’t find a more specific template first.

  • category-[category].php ex: if “news” then category-news.php
  • single-[post-type].php
  • tag[slug].php
  • taxonomy-[taxonomy].php
  • page-[slug].php
  • date.php
  • search.php
  • 404.php
  • home.php
  • front-page.php
  • single.php
  • page.php
  • index.php

WordPress functions.php

<?php

//bundles enqueueing of script and style into one function
function add_josh_styles_and_scripts() {

 wp_enqueue_style( 'josh.css', get_stylesheet_directory_uri() . 
 '/css/josh.css');

 wp_enqueue_script( 'josh.js', get_template_directory_uri() . 
 '/js/josh.js', array(),'1.0', true);
}

// calls above function during the enqueue hook
add_action( 'wp_enqueue_scripts', 'add_josh_styles_and_scripts' );

// bundles enabling and registering menus
function setup_josh_theme() {
    add_theme_support('menus');
    register_nav_menu( 'primary', 'primary menu' );
    register_nav_menu( 'sidebar', 'sidebar menu' );
}

// calls the above function during the init hook
add_action('init','setup_josh_theme');

?>