Created
September 8, 2015 17:38
-
-
Save montrealist/0f4a7649ebeb2ef79a2f to your computer and use it in GitHub Desktop.
WordPress Options Framework, and Polylang - given a post ID, return a URL for a default image specified in admin options.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unction get_default_image_for_post($post_id){ | |
if(!$post_id) { return false; } | |
global $smof_data; | |
//error_log('smof_data:'); | |
//error_log(print_r($smof_data,true)); | |
$thumb_url = false; | |
$prefix = 'mxp_'; | |
$key_cat_news = $prefix . 'category_news'; | |
$key_cat_blog = $prefix . 'category_blog'; | |
$key_img_news = $prefix . 'img_default_news'; | |
$key_img_blog = $prefix . 'img_default_blog'; | |
$keys_to_search = [ $key_cat_news => $key_img_news, $key_cat_blog => $key_img_blog ]; | |
$post_categories = wp_get_post_categories( $post_id ); | |
foreach ($keys_to_search as $key_category => $key_image) { | |
// do we have a category set as a 'blog' category in the admin? | |
if(array_key_exists($key_category, $smof_data) && $smof_data[$key_category]) { | |
error_log('news category slug: ' . $smof_data[$key_category]); | |
// get ID of the category in question | |
$theCatId = get_term_by( 'slug', $smof_data[$key_category], 'category' ); | |
if(property_exists($theCatId, 'term_id')) { | |
error_log('category object based on slug:'); | |
error_log(print_r($theCatId->term_id, true)); | |
// get ID of the translation: | |
$translated_term_id = get_id_of_translated_category($theCatId->term_id); | |
error_log('translated term: ' . $translated_term_id); | |
$first_check = array_search($theCatId->term_id, $post_categories); | |
// error_log(print_r($first_check, true)); | |
if( array_search($theCatId->term_id, $post_categories) !== false || ($translated_term_id !== false && array_search($translated_term_id, $post_categories)) ) { | |
error_log('the post has a category that we are interested in.'); | |
// see if have default image for articles of this category | |
if( array_key_exists($key_image, $smof_data) ) { | |
error_log( 'have ' . $key_image . ' in smof_data...' ); | |
if( $smof_data[$key_image] ) { | |
error_log( $key_image . ' in smof_data is not empty!' ); | |
$thumb_url = $smof_data[$key_image]; | |
//error_log('default image does exist: ' . $thumb_url); | |
} else { | |
error_log('nope 5'); | |
$thumb_url = get_stylesheet_directory_uri() . '/img/' . $key_image . '.jpg'; | |
} | |
} else { | |
$thumb_url = get_stylesheet_directory_uri() . '/img/' . $key_image . '.jpg'; | |
error_log('nope 4'); | |
} | |
} else { | |
error_log('nope 3'); | |
} | |
} else { | |
error_log('nope 2'); | |
} | |
} else { | |
//error_log('nope 1'); | |
} | |
} | |
return $thumb_url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment