CodeHQ

Archive for the ‘PHP’ Category

« Older Entries |

Responsive WordPress images using SRCSET and ACF

Tuesday, June 26th, 2018

Make sure your image field is set to an array in ACF and then use the below.

‘150px’ is the maximum width your image will ever display.

Click the link to view the code

Tags: , , , ,
Posted in PHP, WordPress | No Comments »

Custom query on taxonomy page | WordPress

Thursday, June 14th, 2018

This will go in taxnonomy.php or taxonomy-name.php templates.

$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') ); 

$customField = get_field('custom_field_name')];
$args = array(
	'posts_per_page' => '-1',
	'post_type' 	 => 'service_centres',
	'tax_query' => array (
		array (
			'taxonomy' => 'regions',
			'field' => 'slug',
			'terms' =>  $term->slug
		)
	),
	'meta_key'	 => 'custom_field_name',
	'meta_value'	 => 'custom_field_value_value'
);

Tags: , , , , ,
Posted in PHP, WordPress | No Comments »

Grab first image from post

Monday, April 16th, 2018

Function for grabbing image from post and using it instead of a featured image for example.

Add this to functions.php

// Get URL of first image in a post
function catch_that_image() {
	global $post, $posts;
		$first_img = '';
		ob_start();
		ob_end_clean();
		$matches[1][0] = '';
		if(preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches)){
		  	$first_img = $matches[1][0];
		}

	// no image found display default image instead
	return $first_img;
}

And call in the theme with

<img src="<?php echo catch_that_image() ?>" onerror="this.style.display = 'none';"/> 

Tags: , , , , , ,
Posted in PHP, WordPress | No Comments »

Query posts by current post category

Monday, April 16th, 2018

Great for showing ‘related posts’ by current post category. The current post has an ID, the query grabs that ID, checks with categories are associated with that post, creates an array and passes these through to the query, also excluding this current post from the loop.

Output is posts from the same category/categories as the current post (active page).

<?php 
	$args = array(
		'posts_per_page' => 3,
		'post__not_in'   => array( get_the_ID() ),
		'no_found_rows'  => true, //Np pagination needed so query is quicker
		'post__not_in' 	 => array($post->ID)  //Excludes current post
	);
 
	$cats = wp_get_post_terms( get_the_ID(), 'category' ); 
	$cats_ids = array();  
	foreach( $cats as $wpex_related_cat ) {
		$cats_ids[] = $wpex_related_cat->term_id; 
	}
	if ( ! empty( $cats_ids ) ) {
		$args['category__in'] = $cats_ids;
	}
	$wpex_query = new wp_query( $args );
 
	foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
 
	//CONTENT HERE i.e. post data and title
 
<?php endforeach; wp_reset_postdata();?>

Tags: , , ,
Posted in PHP, WordPress | No Comments »

Limit WordPress Revisions

Tuesday, January 9th, 2018

Add this to you wp-config.php file and change the number for how many revisions you want to limit your pages to:

define('WP_POST_REVISIONS', 10);

Or use the below to globally turn off post revisions:

define('WP_POST_REVISIONS', false);

Tags: , , , , , , , , ,
Posted in PHP, WordPress | No Comments »

Get author Twitter handle | WordPress

Friday, December 30th, 2016

$twitterHandle = get_the_author_meta('twitter');

echo $twitterHandle

Tags: , , ,
Posted in PHP, WordPress | No Comments »

Custom fields in custom taxonomy archive page | WordPress

Thursday, December 1st, 2016

$queried_object = get_queried_object(); 
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
the_field('custom_field_name', $queried_object);

Tags: , , , , , ,
Posted in PHP, WordPress | No Comments »

jQuery in the footer

Thursday, December 1st, 2016

Add this to your functions.php

function my_init()   
{  
    if (!is_admin())   
    {  
        wp_deregister_script('jquery');  
  
        // Load a copy of jQuery from the Google API CDN  
        // The last parameter set to TRUE states that it should be loaded  
        // in the footer.  
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js', FALSE, '1.11.0', TRUE);  
  
        wp_enqueue_script('jquery');  
    }  
}  
add_action('init', 'my_init');  

Tags: , , , , ,
Posted in jQuery, PHP, WordPress | No Comments »

Remove script and style version numbers | WordPress

Wednesday, November 30th, 2016

Add this to functions.php

// Pick out the version number from scripts and styles
function remove_version_from_style_js( $src ) {
    if ( strpos( $src, 'ver=' . get_bloginfo( 'version' ) ) )
        $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'remove_version_from_style_js');
add_filter( 'script_loader_src', 'remove_version_from_style_js');

Tags: , , , , , , ,
Posted in PHP, WordPress | No Comments »

Echo thumbnail URL and size | WordPress

Monday, October 24th, 2016

Use the PHP in the query below – and then the HTML example below to echo the URL out.

the_post_thumbnail_url();                  // without parameter -> 'post-thumbnail'
 
the_post_thumbnail_url( 'thumbnail' );       // Thumbnail (default 150px x 150px max)
the_post_thumbnail_url( 'medium' );          // Medium resolution (default 300px x 300px max)
the_post_thumbnail_url( 'large' );           // Large resolution (default 640px x 640px max)
the_post_thumbnail_url( 'full' );            // Full resolution (original size uploaded)
 
the_post_thumbnail_url( array(100, 100) );  // Other resolutions

Tags: , , , ,
Posted in PHP, WordPress | No Comments »

Next Page »