Skip to content

Instantly share code, notes, and snippets.

@sultann
Forked from JiveDig/functions.php
Created November 30, 2018 07:48
Show Gist options
  • Select an option

  • Save sultann/e29c41a35dbfd1ba854cc48360667d2d to your computer and use it in GitHub Desktop.

Select an option

Save sultann/e29c41a35dbfd1ba854cc48360667d2d to your computer and use it in GitHub Desktop.

Revisions

  1. @JiveDig JiveDig revised this gist Oct 23, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion functions.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    * If Yoast Primary Term is used, return it,
    * otherwise fallback to the first term.
    *
    * @version 1.0.0
    * @version 1.1.0
    *
    * @link https://gist.github.com/JiveDig/5d1518f370b1605ae9c753f564b20b7f
    * @link https://gist.github.com/jawinn/1b44bf4e62e114dc341cd7d7cd8dce4c
  2. @JiveDig JiveDig revised this gist Oct 23, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion functions.php
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@
    *
    * @return WP_Term|bool The term object or false if no terms.
    */
    function jivedig_get_primary_term( $taxonomy, $post_id = false ) {
    function jivedig_get_primary_term( $taxonomy = 'category', $post_id = false ) {

    // Bail if no taxonomy.
    if ( ! $taxonomy ) {
  3. @JiveDig JiveDig revised this gist Oct 17, 2018. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,8 @@
    * otherwise fallback to the first term.
    *
    * @version 1.0.0
    * @link
    *
    * @link https://gist.github.com/JiveDig/5d1518f370b1605ae9c753f564b20b7f
    * @link https://gist.github.com/jawinn/1b44bf4e62e114dc341cd7d7cd8dce4c
    * @author Mike Hemberger @JiveDig.
    *
    @@ -15,7 +16,7 @@
    *
    * @return WP_Term|bool The term object or false if no terms.
    */
    function cmag_get_primary_term( $taxonomy, $post_id = false ) {
    function jivedig_get_primary_term( $taxonomy, $post_id = false ) {

    // Bail if no taxonomy.
    if ( ! $taxonomy ) {
  4. @JiveDig JiveDig created this gist Oct 17, 2018.
    53 changes: 53 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    <?php

    /**
    * Get the primary term of a post, by taxonomy.
    * If Yoast Primary Term is used, return it,
    * otherwise fallback to the first term.
    *
    * @version 1.0.0
    * @link
    * @link https://gist.github.com/jawinn/1b44bf4e62e114dc341cd7d7cd8dce4c
    * @author Mike Hemberger @JiveDig.
    *
    * @param string $taxonomy The taxonomy to get the primary term from.
    * @param int $post_id The post ID to check.
    *
    * @return WP_Term|bool The term object or false if no terms.
    */
    function cmag_get_primary_term( $taxonomy, $post_id = false ) {

    // Bail if no taxonomy.
    if ( ! $taxonomy ) {
    return false;
    }

    // If no post ID, set it.
    if ( ! $post_id ) {
    $post_id = get_the_ID();
    }

    // If checking for WPSEO.
    if ( class_exists( 'WPSEO_Primary_Term' ) ) {

    // Get the primary term.
    $wpseo_primary_term = new WPSEO_Primary_Term( $taxonomy, $post_id );
    $wpseo_primary_term = $wpseo_primary_term->get_primary_term();

    // If we have one, return it.
    if ( $wpseo_primary_term ) {
    return get_term( $wpseo_primary_term );
    }
    }

    // We don't have a primary, so let's get all the terms.
    $terms = get_the_terms( $post_id, $taxonomy );

    // Bail if no terms.
    if ( ! $terms || is_wp_error( $terms ) ) {
    return false;
    }

    // Return the first term.
    return $terms[0];
    }