Blog

WordPress Categories Template

If WordPress category template file is not working, add the following in category.php, before the loop:

$cat_id = get_query_var(‘cat’);
query_posts(“post_type=&cat=$cat_id”);


Page Navigation by Category

$terms = get_the_terms( $post->ID, ‘mediaitem’ );
$args = array(
‘in_same_term’ => true,
‘category’ => $term->slug,
‘screen_reader_text’ => ‘Post navigation’
);
the_post_navigation($args);


WordPress: Loop All Categories showing All Posts

$post_type = ‘category’;
$args = array(
‘true’ => false,
‘orderby’ => ‘term_id’,
‘order’ => ‘ASC’
);
$tax = get_terms( ‘category’ , $args);
if ( have_posts() ) {
foreach( $tax as $cat ) {
echo $cat->name;
while ( have_posts() ) : the_post();
the_title();
the_content();
endwhile;
}
}


Useful WordPress Development Plugins

Replace WordPress Media with Enable Media Replace

Audit WordPress changes using Stream

Change WordPress administrator menu options with Admin Menu Editor

Simplify Look and feel for WordPress Editors with White Label CMS

Easy WordPress Updates Manager with Easy Updates Manager


WordPress: Easy to remember template hierarchy

Aide memoir for the WordPress template hiearchy…

Default for everything – archive pages, single posts and single pages (as well as all other content types):

1st: index.php

Select template by page for specific archive displays, like category and taxonomy archives or templates for different page layouts:

1st: template_{$custom-post-type}.php or template_{NOSIDEBAR}.php
2nd: index.php

For all pages that do not exist:

1st: 404.php
2nd: index.php

The user submits a search:

1st: search.php
2nd: index.php

Latest blog posts:

1st: front-page.php
2nd: home.php
3rd: index.php

For whichever page is assigned as the front page:

1st: front-page.php
2nd: home.php
3rd: index.php

For a single post page:

1st: single-{$post-type}-{$slug}.php
2nd: single-{$post-type}.php
3rd: single.php
4th: singular.php (fallback for single.php AND page.php)
3rd: index.php

For a single page:

1st: custom_template.php
2nd: page-{$slug}.php
3rd: page-{$id}.php
4th: page.php
5th: singular.php (fallback for single.php AND page.php)
6th: index.php

For a category page:

1st: category-{$slug}.php
2nd: category-{$id}.php
3rd: category.php
4th: archive.php
5th: index.php

for a tag page:

1st: tag-{$slug}.php
2nd: tag-{$id}.php
3rd: tag.php
4th: archive.php
5th: index.php

For custom taxonomy pages:

1st: taxonomy-{$taxonomy}-{$term}.php
2nd: taxonomy-{$taxonomy}.php
3rd: taxonomy.php
4th: archive.php
5th: index.php

For Custom Post Types:

1st: archive-{post_type}.php
2nd: archive.php
3rd: index.php

For an author info page:

1st: author-{$author-nicename}.php
2nd: author-{$author-id}.php
3rd: author.php
4th: archive.php
5th: index.php

For a date page archive:

1st: date.php
2nd: archive.php
3rd: index.php

For an attachment page:

1st: {$MIME-type}.php (one of: image.php, video.php, pdf.php, application.php, etc.)
2nd: attachment.php
3rd: single-attachment-{$slug}.php
4th: single-attachment.php
5th: single.php
6th: singular.php
7th: index.php

For embeds:
1st: embed-{$post-type}-{$post_format}.php
2nd: embed-{$post-type}.php
3rd: embed.php
4th: wp-includes/theme-compat/embed.php


WordPress: useful additions to functions.php

/*
* Useful snippets for functions.php
/*

/*
* Add support for the following:
*/
add_theme_support( ‘title-tag’ );
add_theme_support( ‘post-thumbnails’ );
add_theme_support( ‘html5’, array( ‘search-form’, ‘comment-form’, ‘comment-list’ ) );

/*
* Register Menus
*/
function register_my_menus(){
register_nav_menus(
array( ‘primary’ => __(‘Primary Menu’, ‘AD’), ) );
}
add_action(‘init’, ‘register_my_menus’);

/*
* Allow php in widget
*/
function howman_php_execute( $html ) {
if ( strpos($html,”<“.”?php”) !==false ) {
ob_start();
eval(“?”.”>”.$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
add_filter(‘widget_text’,’howman_php_execute’, 100);

/*
*Enqueue Datepicker + jQuery UI CSS
*/
wp_enqueue_script( ‘jquery-ui-datepicker’ );
wp_enqueue_style( ‘jquery-ui-style’, ‘//ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/smoothness/jquery-ui.css’, true);

/*
* Add widgets
*/
function bootstrapstarter_widgets_init() {

register_sidebar( array(
‘name’ => ‘Footer – Copyright Text’,
‘id’ => ‘footer-copyright-text’,
‘before_widget’ => ‘<div class=”footer_copyright_text”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h4>’,
‘after_title’ => ‘</h4>’,
) );

register_sidebar( array(
‘name’ => ‘Sidebar – Inset’,
‘id’ => ‘sidebar-1’,
‘before_widget’ => ‘<div class=”sidebar-module sidebar-module-inset”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h4>’,
‘after_title’ => ‘</h4>’,
) );

register_sidebar( array(
‘name’ => ‘Sidebar – Default’,
‘id’ => ‘sidebar-2’,
‘before_widget’ => ‘<div class=”sidebar-module”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h4>’,
‘after_title’ => ‘</h4>’,
) );

}
add_action( ‘widgets_init’, ‘bootstrapstarter_widgets_init’ );

/*
* Register jQuery
*/
if (!is_admin()) {
wp_deregister_script(‘jquery’);
wp_register_script(‘jquery’, (“https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js”), false);
wp_enqueue_script(‘jquery’);
}

/*
* Custom Excerpt Length
*/
function custom_excerpt_length($length) {
return 25;
}
add_filter(‘excerpt_length’, ‘custom_excerpt_length’);

/*
* Add scripts, stylesheets, fonts, etc.
*/
function mysite_enqueue_styles() {
wp_register_style(‘bootstrap’, get_template_directory_uri() . ‘/bootstrap/css/bootstrap.min.css’ );
$dependencies = array(‘bootstrap’);
wp_enqueue_style( ‘mysite-style’, get_stylesheet_uri(), $dependencies );
}
add_action( ‘wp_enqueue_scripts’, ‘mysite_enqueue_styles’ );

function startwordpress_scripts() {
wp_enqueue_script( ‘bootstrap’, get_template_directory_uri() . ‘/js/bootstrap.min.js’, array( ‘jquery’ ), ‘3.3.6’, true );
wp_enqueue_script( ‘validator’, get_template_directory_uri() . ‘/js/jquery.validate.min.js’, array ( ‘jquery’ ), ‘3.3.6’, true);
}
add_action( ‘wp_enqueue_scripts’, ‘startwordpress_scripts’ );

function startwordpress_google_fonts() {
wp_register_style(‘Lato’, ‘http://fonts.googleapis.com/css?family=Lato:100,200,400,600,700,800’);
wp_enqueue_style( ‘Lato’);
wp_register_style(‘Nunito’, ‘https://fonts.googleapis.com/css?family=Nunito+Sans” rel=”stylesheet”>’);
wp_enqueue_style( ‘Nunito’);
// wp_register_style(‘OpenSans’, ‘https://fonts.googleapis.com/css?family=Open+Sans” rel=”stylesheet”>’);
// wp_enqueue_style( ‘OpenSans’);
}
add_action(‘wp_print_styles’, ‘startwordpress_google_fonts’);


WordPress: Must have plugins, updated

Must Haves

All In One SEO Pack
Out-of-the-box SEO for your WordPress blog.

WordFence
THE security plugin.

WP Optimize
Clean Up / Optimise your WordPress installation.

WP Super Cache
WordPress Caching.

WP Smush It
Optimise graphics files.

WP Migrate DB
WordPress database migration.

Used in Specific Circumstances

Advanced Custom Fields
Provides simple flexibility.

Post Types Order
Provides simple and easy sorting.

Superceded

These we still use occassionally, but simpler to build on the fly:

Contact Form 7

These we no longer used as they’re all done by All in One SEO:

Google Analytics for WordPress
Google XML Sitemaps


WordPress: Show title and alt on images

To add image alt and title attributes to images, add the following to functions.php:
function add_image_title( $attr, $attachment= null ) {
    $img_title= trim( strip_tags( $attachment->post_title ) );
    $attr['title'] = $img_title;
    $attr['alt'] = $img_title;
    return$attr;
}
add_filter( 'wp_get_attachment_image_attributes','add_imgage_title', 10, 2 );
or replace $attr[‘title’] with:
$attr['title'] = the_title_attribute( 'echo=0' );

WordPress: Two Columns using More button

1) Ensure you have added <!–more–>  in page content. Use the more button!

2) Use the following code to display content in two columns:
global $more;
$more = 0;
echo ‘<div id=”column_one”>’;
echo ‘&nbsp;’;
the_content(”);
echo ‘</div>’;

$more = 1;
echo ‘<div id=”column_two”>’;
the_content(‘read more’, true);
echo ‘</div>’;

3) CSS to tidy it up:
#column_one, #column_two {
float: left;
width: 35%;
margin: 0px 7.5%;
}
#column_one {
padding-left: 5%;
}
#column_two {
padding-right: 5%;
}
#column_one h6, #column_two h6 {
display: none;
}
#column_one p, #column_two p {
text-align: center;
}


WordPress: List all queries on the page

This code snippet will list all the queries executed on your WordPress page
Add this to the bottom of the page:

<?php
if (current_user_can(‘administrator’)){
global $wpdb;
echo “<pre>”;
print_r($wpdb->queries);
echo “</pre>”;
}
?>