Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrew Killen andykillen

View GitHub Profile
@andykillen
andykillen / always-have-a-default-first.js
Created Jun 18, 2016
Always have a default, and always check before trying to use that the selector responds with something
View always-have-a-default-first.js
// you can do this
$('.class-to-find').each(function(index){
outputValue = $(this).text();
});
// but it will fail badly if there is nothing found by $('class-to-find').
//better to do
outputValue = ''; // set a default value
if($('.class-to-find').length){ // make sure that there is something to loop
$('.class-to-find').each(function(index){ // do the loop
@andykillen
andykillen / use-js-objects-for-performance.js
Last active Jun 18, 2016
Its gives better performance to use a javascript object than it does to use a jQuery selector.
View use-js-objects-for-performance.js
var el = document.getElementById('header'); // this is for #header not <header>
$(el).('.some-class-used-in-header').each(function(index){
// do something!! to each of the things with this class
});
$(el.getElementsByTagName('nav')).on('touchstart click', 'a', function(e){
// do something when an <A> inside a <NAV> inside #header is clicked.
e.preventDefault(); // prevents the default click action
});
View exclude using php
$ids = array();
// get current post id if a single (post or page)
if(is_single()){
global $post;
$ids[] = $post->ID;
}
// setup basic loop arguments
$args = array(
'post_type' => 'post',
View Change the Args of WP_Query.php
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '5'
'no_found_rows' => true, // turn off pagination information
'update_post_meta_cache' => false // don't do anything with post meta cache
);
View standard-loop-get-latest-posts.php
<?php
$id = false;
// get current post id if a single (post or page)
if(is_single()){
global $post;
$id = $post->ID;
}
// setup basic loop arguments
You can’t perform that action at this time.