Some WordPress snippets to speed up the Dev process

WP Snippets:

1. List all taxonomies( Categories or other you created)
1.1 Normal Loop for Posts – Categories
<?php
$args = array(
 ‘orderby’ => ‘name’
 );
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo ‘<span class=”areaMap”><a href=”‘ . get_category_link( $category->term_id ) . ‘”>’ . $category->name . ‘</a><img class=”orangeArrow” src=”‘. get_template_directory_uri() . ‘/images/staffHover.png”></span>’;
}
?>

1.2 Custom Taxonomies:
<?php // Tax name here is ‘departments’ – Will exclude empty Taxonomy aswell
$depart = get_terms( ‘departments’, ‘orderby=count&hide_empty=0’ );
if  ($depart) {
 foreach ($depart as $taxonomy ) {
  if ($taxonomy->count != 0) {
echo ‘<li><a href=”‘. site_url() .’/departments/’ . $taxonomy->slug . ‘”>’ . $taxonomy->name . ‘</a></li>’;
  }
 }
}
?>

2. Create Pagination using a custom loop through any post type you choose:
<span class=”pageNum”>
<?php // Careers is the content type here
      $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
      echo $paged;
      $loop = new WP_Query(array(‘post_type’ => ‘careers’,
      ‘post_status’ => ‘published’,
      ‘paged’ => $paged,
      ‘posts_per_page’ => 2,
      ‘orderby’ => ‘menu_order’
      ));
      ?>
</span>
<div class=”nav-previous fLeft”><?php previous_posts_link(‘Previous Page’) ?></div>
<div class=”nav-next fRight”><?php next_posts_link(‘Next Page’) ?></div>

3. Count the amount of posts in a Custom Post Type with a certain Taxonomy
<?php
if(isset($_GET)) {
$args = array(
‘post_type’ => ‘careers’, // The Post Type we want
‘post_status’ => ‘publish’,
‘posts_per_page’ => 20,
‘tax_query’ => array(
‘taxonomy’ => ‘departments’, // The Taxonomy
‘field’    => ‘slug’,
‘terms’    => $filtervalue
)
);
};
$my_query = new WP_Query($args); // The Query with specified arguments
$count = $my_query->found_posts; // Count the Amount of Posts in Custom Post Type CAREERS
echo $count; // And ECHO!
wp_reset_query(); // Reset the Query
?>

4. If your home page is a Selected Page and not the Blog Roll
<?php if (is_front_page()) { ?>
    <body class=”home”>
<?php } else { ?>
    <body>
<?php } ?>

5. Url directory Basics
<?php echo site_url(); ?> = Domain URL
<?php bloginfo(‘stylesheet_directory’); ?> = Will print your Template directory
<?php echo get_permalink( $post->ID ); ?> = Print the Url of the current post/page
<?php echo the_title(); ?> = Print the Title of the current post/page

6. Basic Search form:
<form action=”<?php echo site_url(); ?>”>
<input class=”search-field pageUrl” type=”search” title=”Search for:” value=”” name=”s” id=”s” placeholder=”What are you searching for?” />
<button type=”submit” class=”searchButton”><i class=”fa fa-search”></i></button>
</form>

7. Get Head and Foot:
<?php get_header(); ?>
<?php get_footer(); ?>

8. Basic Content Loop:
<?php if (have_posts()) { ?> // If there are posts
<?php while (have_posts()) { the_post(); ?>
<?php the_title(); ?>
<?php the_author() ?>
<?php the_category(‘, ‘) ?>
<?php the_content(); ?>
<?php } ?>
<!– PAGINATION if required –>
<?php next_posts_link(‘Older Entries’) ?>
<?php previous_posts_link(‘Newer Entries’) ?>
<?php } else { // Else NOTHING FOUND ?>
Not Found
<?php } ?>

Leave a Reply

Your email address will not be published. Required fields are marked *