如果 post 属于多个类别并且我想在 Genesis post_meta 中显示主要类别

If a post belongs to a many categories and I want to show a primary category in Genesis post_meta

https://broadly.vice.com/en_us 上,如果他们的 post 属于多个类别,则他们会显示一个主要类别。

如何在 post 乘以 post 的基础上选择要在 Genesis Child 主题的 post_meta 中显示的主要类别?

从这个 post 日期开始,安装 Trunk version of CMB2(不是插件)。

See See Gist 备份此答案。

将以下内容放入您的 functions.php 或者,更好的是,您的函数插件。不要添加开头 php。在许多位置更改 yourprefix_

<?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' );

这是简单的解决方案。

使用 SEO Yoast 免费插件 https://wordpress.org/plugins/wordpress-seo/。当有多个类别分配给 post.

时,这有助于您将任何类别设为主要类别

主猫选择见插件界面:

然后在你的 WP 主题中,在函数文件中有以下函数,然后你可以使用在任何地方回显类别名称:<?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;
}