experience

经验

zw-1988.com

home > 经验 > wordpress 自定义文章类型返回父级分类/输出父级名字(不带连接)

2017.7.12 wordpress 自定义文章类型返回父级分类/输出父级名字(不带连接)

zwnet

返回父级分类

在function里加入

function wpdocs_custom_taxonomies_terms_links() {
	// Get post by post ID.
	$post = get_post( $post->ID );

	// Get post type by post.
	$post_type = $post->post_type;

	// Get post type taxonomies.
	$taxonomies = get_object_taxonomies( $post_type, 'objects' );

	$out = array();

	foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {

		// Get the terms related to post.
		$terms = get_the_terms( $post->ID, $taxonomy_slug );

		if ( !empty( $terms ) ) {

			foreach ( $terms as $term ) {
				$out[] = sprintf( '<a  class="cont-all"  href="%1$s">返回</a>',
					esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
					esc_html( $term->name )
				);
			}

		}
	}
	return implode( '', $out );
}

然后在自定义的模板需要的地方

<?php wp_reset_query(); $output= wpdocs_custom_taxonomies_terms_links();?>

输出父级名字(不带连接)

在function里加入

function wpdocs_custom_taxonomies_terms_name() {
	// Get post by post ID.
	$post = get_post( $post->ID );

	// Get post type by post.
	$post_type = $post->post_type;

	// Get post type taxonomies.
	$taxonomies = get_object_taxonomies( $post_type, 'objects' );

	$out = array();

	foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {

		// Get the terms related to post.
		$terms = get_the_terms( $post->ID, $taxonomy_slug );

		if ( !empty( $terms ) ) {

			foreach ( $terms as $term ) {
				$out[] = sprintf( '%1$s',
					esc_html( $term->name )
				);
			}

		}
	}
	return implode( '', $out );
}

然后在自定义的模板需要的地方

<?php wp_reset_query(); $output= wpdocs_custom_taxonomies_terms_name();?>

 

«
相关文章
wordpress (25)java script (9)tool (8)css (2)攻略 (1)第一次人体 (1)插件 (1)ECharts (1)food (1)雪糕 (1)动画 (1)book (1)标签云 (1)生活 (1)一言难尽的画稿 (1)
辽ICP备16002031号

Copyright © 2016 - 2020 1988-zw.com