Skip to content

Instantly share code, notes, and snippets.

@chikaibeneme
Created May 6, 2026 06:25
Show Gist options
  • Select an option

  • Save chikaibeneme/3859094fe7aabd62cc9d1215a87b7481 to your computer and use it in GitHub Desktop.

Select an option

Save chikaibeneme/3859094fe7aabd62cc9d1215a87b7481 to your computer and use it in GitHub Desktop.
script for kadence
function kt_get_aioseo_primary_category( $post_id = 0 ) {
$post_id = $post_id ? absint( $post_id ) : get_the_ID();
if ( ! $post_id ) {
return false;
}
if ( function_exists( 'aioseo' ) ) {
$aioseo = aioseo();
if (
isset( $aioseo->standalone->primaryTerm ) &&
is_callable( array( $aioseo->standalone->primaryTerm, 'getPrimaryTerm' ) )
) {
$primary_term = $aioseo->standalone->primaryTerm->getPrimaryTerm( $post_id, 'category' );
if ( $primary_term instanceof WP_Term && ! is_wp_error( $primary_term ) ) {
return $primary_term;
}
}
}
$categories = get_the_terms( $post_id, 'category' );
if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
return array_shift( $categories );
}
return false;
}
function kt_main_category_only() {
$category = kt_get_aioseo_primary_category();
if ( ! $category ) {
return;
}
$cat_link = get_term_link( $category );
if ( is_wp_error( $cat_link ) ) {
return;
}
?>
<div class="entry-taxonomies">
<span class="category-links">
<a href="<?php echo esc_url( $cat_link ); ?>" rel="category tag"><?php echo esc_html( $category->name ); ?></a>
</span>
</div>
<?php
}
function kt_show_only_aioseo_main_category() {
remove_action( 'kadence_loop_entry_header', 'Kadence\loop_entry_taxonomies', 10 );
add_action( 'kadence_single_before_entry_title', 'kt_main_category_only', 5 );
add_action( 'kadence_loop_entry_header', 'kt_main_category_only', 10 );
}
add_action( 'wp', 'kt_show_only_aioseo_main_category' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment