1

On https://broadly.vice.com/en_us they show a single, primary category, if their post belongs to many categories.

How can you choose, on a post by post basis, what primary category to show in the post_meta in a Genesis Child theme?

Christina
  • 34,296
  • 17
  • 83
  • 119

2 Answers2

3

Here is simple solution.

Use SEO Yoast free plugin https://wordpress.org/plugins/wordpress-seo/ . Which help you make any category primary when there are multiple categories assigned to a post.

See Plugin's interface for Primary cat selection: Select Primary Category for the post

Then in your WP theme have following function in functions file and then you can use to echo the category name anywhere as: <?php echo taxo_primary_term_name; ?>

// wp seo yoast get primary category name 

function taxo_primary_term_name($taxo){

  $wpseo_primary_term = new WPSEO_Primary_Term($taxo, get_the_ID());
  $wpseo_primary_term = $wpseo_primary_term->get_primary_term();

  return $wpseo_primary_term = get_term($wpseo_primary_term)->name;
}
Muhammad Asadullah
  • 3,735
  • 1
  • 22
  • 38
  • +1 For some reason, the person who prompted this question didn't want to rely on a plugin. He may use another SEO plugin, though other plugins don't keep up on it like Yoast does... – Christina Jan 31 '17 at 20:09
0

As of this post date, install the Trunk version of CMB2 (not the plugin).

enter image description here

See See Gist for a backup of this answer.

Put the following inside your functions.php or, better, your functions plugin. Don't add the opening php. Change yourprefix_ in many locations.

<?php
//don't add

/**
 *
 * Create Metabox and Primary Category Select Field
 * Requires CMB2 TRUNK branch NOT the plugin from the repo
 * Install CMB2-trunk.zip via plugin interface or incorporate in your theme, read their docs
 * https://github.com/WebDevStudios/CMB2/branches
 *
 */
function yourprefix_primary_category_selection() {

    $prefix = 'yourprefix_';

    $cmb_demo = new_cmb2_box( array(
        'id'            => $prefix . 'metabox',
        'title'         => esc_html__( 'Primary Category', 'yourtextdomain' ),
        'object_types'  => array( 'post', ),
            'context'       => 'side',
        'priority'      => 'low',
        'show_names'    => false, 
    ) );

    $cmb_demo->add_field( array(
        'name'           => esc_html__( 'Choose Primary Category', 'yourtextdomain' ),
        'desc'           => esc_html__( 'Choose primary category for display in post_meta', 'yourtextdomain' ),
        'id'             => $prefix . 'category_list',
        'taxonomy'       => 'category',
        'type'           => 'taxonomy_select',
    ) );    

}
add_action( 'cmb2_admin_init', 'yourprefix_primary_category_selection' );


/**
 *
 * Add Primary to [post_categories] shortcode replacing the genesis shortcode of the same name
 *
 */
function yourprefix_post_primary_category_shortcode( $atts ) {

    //* get our CMB2 field and category stuff
    $prefix        = 'yourprefix_';
    $primary_cat   = get_post_meta( get_the_ID(), $prefix . 'category_list', true );
    $category_id   = get_cat_ID( $primary_cat );
    $category_link = get_category_link( $category_id );
    $category_name = get_cat_name( $category_id );

    $defaults = array(
        'sep'    => ', ',
        'before' => __( 'Filed Under: ', 'yourtextdomain' ),
        'after'  => '',
    );

    $atts = shortcode_atts( $defaults, $atts, 'post_categories' );

    //* fallback to the standard array if the choice in the primary metabox is not set
    if( empty( $primary_cat ) ) {
        $cats = get_the_category_list( trim( $atts['sep'] ) . ' ' );
    } else {
        $cats = '<a href="' . $category_link . '">' . $category_name . '</a>';
    }

    //* Do nothing if no cats
    if ( ! $cats ) {
        return '';
    }

    if ( genesis_html5() )
        $output = sprintf( '<span %s>', genesis_attr( 'entry-categories' ) ) . $atts['before'] . $cats . $atts['after'] . '</span>';
    else
        $output = '<span class="categories">' . $atts['before'] . $cats . $atts['after'] . '</span>';

    return apply_filters( 'genesis_post_categories_shortcode', $output, $atts );

}
add_shortcode( 'post_categories', 'yourprefix_post_primary_category_shortcode' );
Christina
  • 34,296
  • 17
  • 83
  • 119