Skip to content

Instantly share code, notes, and snippets.

View tylerwiegand's full-sized avatar
👨‍💻

Tyler Wiegand tylerwiegand

👨‍💻
View GitHub Profile
<?php
// When you want to split strings by spaces without rounding up
function string_parts($string, $parts = 2)
{
$array = preg_split('/\s+/', $string);
if (! (count($array) > 1)) {
return $string;
}
@tylerwiegand
tylerwiegand / getPostThumbnail.php
Last active March 19, 2020 18:25
WP: Ultimate fallback solution to getting a thumbnail for a post
<?php
function nb_get_post_thumbnail( $post_id = null, $size = 'medium', $attr = [] ) {
if(!$post_id) {
$post_id = get_the_ID();
}
// Step 1: Use the featured image if it's there
if(!empty($image = get_the_post_thumbnail($post_id, $size, $attr))) {
return $image;