Skip to content

Instantly share code, notes, and snippets.

@smchenrybc
Created July 28, 2023 14:38
Show Gist options
  • Save smchenrybc/0d20f5e47426bf4e12e12eae1416e39b to your computer and use it in GitHub Desktop.
Save smchenrybc/0d20f5e47426bf4e12e12eae1416e39b to your computer and use it in GitHub Desktop.
Context functions for RiverWoods theme
<?php
/**
* context-functions.php
*
* @package bc_starter
*/
/**
* Get the primary category set in Yoast SEO
*
* @link https://gist.github.com/tarecord/4c7171b9955aee41d91db133af3e1d8f
*/
function bc_get_primary_cat( $post = 0, $taxonomy = 'category' ) {
if ( !$post ) {
$post = get_the_ID();
}
$terms = get_the_terms( $post, $taxonomy );
$primary_term = array();
if ( $terms ) {
if ( class_exists( 'WPSEO_Primary_Term' ) ) {
$wpseo_primary_term = new WPSEO_Primary_Term( $taxonomy, $post );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
if ( is_wp_error( $term ) ) {
$primary_term['name'] = $terms[0]->name;
$primary_term['slug'] = $terms[0]->slug;
$primary_term['link'] = get_term_link( $terms[0]->term_id );
} else {
$primary_term['name'] = $term->name;
$primary_term['slug'] = $term->slug;
$primary_term['link'] = get_term_link( $term->term_id );
}
} else {
$primary_term['name'] = $terms[0]->name;
$primary_term['slug'] = $terms[0]->slug;
$primary_term['link'] = get_term_link( $terms[0]->term_id );
}
}
return $primary_term;
}
/**
* Get primary category name
*/
function bc_get_primary_cat_name( $post = 0, $taxonomy = 'category' ) {
$cat = bc_get_primary_cat( $post, $taxonomy );
$cat_name = $cat['name'];
return $cat_name;
}
/**
* Get primary category slug
*/
function bc_get_primary_cat_slug( $post = 0, $taxonomy = 'category' ) {
$cat = bc_get_primary_cat( $post, $taxonomy );
$cat_slug = $cat['slug'];
return $cat_slug;
}
/**
* Get primary category link
*/
function bc_get_primary_cat_link( $post = 0, $taxonomy = 'category' ) {
$cat = bc_get_primary_cat( $post, $taxonomy );
$cat_link = $cat['link'];
return $cat_link;
}
/**
* Get post category IDs
*/
function bc_get_post_cat_ids( $post_id = false ) {
$cats = wp_get_post_categories( $post_id );
return $cats;
}
/**
* Get post category slugs
*/
function bc_get_post_cat_slugs( $post_id = false ) {
$cat_slugs = array();
$cat_ids = bc_get_post_cat_ids( $post_id );
foreach ( $cat_ids as $id ) {
$cat = get_category( $id );
$cat_slug = $cat->slug;
$cat_slugs[] = $cat_slug;
}
return $cat_slugs;
}
/**
* Get community category from post
*/
function bc_get_community_cat( $post_id = false ) {
$slugs = bc_get_post_cat_slugs( $post_id );
$communities = array( 'exeter', 'manchester', 'durham' );
$filtered = array_intersect( $communities, $slugs );
return $filtered;
}
/**
* Get page ancestor IDs
*/
function bc_get_ancestor_ids( $post_id ) {
$ancestors = get_post_ancestors( $post_id );
return $ancestors;
}
/**
* Get top ancestor ID
*/
function bc_get_top_ancestor_id( $post_id ) {
$top_ancestor = end( bc_get_ancestor_ids( $post_id ) );
return $top_ancestor;
}
/**
* Get top ancestor name
*/
function bc_get_top_ancestor_name( $post_id ) {
$ancestor_id = bc_get_top_ancestor_id( $post_id );
$ancestor_name = get_the_title( $ancestor_id );
return $ancestor_name;
}
/**
* What community are we in? (whether page or post)
*/
function bc_get_community_name( $post_id = false ) {
$community = false;
// are we on a page?
if ( is_page() ) {
$community = strtolower( bc_get_top_ancestor_name( $post_id ) );
}
// or are we on a post?
elseif ( is_single() ) {
$community_cat = bc_get_community_cat( $post_id );
$community = implode( ' ', $community_cat );
}
return $community;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment