php 获取该术语的指定元数据值,或从其中一个父术语获取。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 获取该术语的指定元数据值,或从其中一个父术语获取。相关的知识,希望对你有一定的参考价值。

<?php

/**
 * Get the specified metadata value for the term or from
 * one of it's parent terms.
 *
 * @param  WP_Term  $term           Term object
 * @param  string   $key            The meta key to retrieve.
 * @param  bool     $check_enabled  Whether to check if custom archive settings are enabled.
 *
 * @return mixed|null
 */
function mai_get_hierarchichal_term_meta( WP_Term $term, $key, $check_enabled ) {
	if ( ! is_taxonomy_hierarchical( $term->taxonomy ) ) {
		return;
	}
	if ( ! mai_has_parent_term( $term ) ) {
		return;
	}
	return mai_get_term_meta_recursively( $term, $key, $check_enabled );
}

/**
 * Recursively get the term metadata by the specified meta key.
 *
 * This function walks up the term hierarchical tree, searching for
 * a valid metadata value for the given meta key.
 *
 * The recursive action stops when:
 *      1. The current term level has the metadata value.
 *      2. The current term level does not have a parent term.
 *
 * @param  WP_Term     $term           Term object
 * @param  string      $key            The meta key to retrieve.
 * @param  bool        $check_enabled  Whether to check if custom archive settings are enabled.
 * @param  mixed|null  $meta
 *
 * @return mixed|null
 */
function mai_get_term_meta_recursively( WP_Term $term, $key, $check_enabled, $meta = null ) {
	// Setup level.
	static $level = 1;
	/**
	 * If we're over the 4th level, we've gone too far.
	 * If we allow too many levels things could get slow.
	 */
	if ( $level > 4 ) {
		// Reset.
		$level = 1;
		// Return.
		return $meta;
	}
	// Increment the level.
	$level++;
	// We need to checking whether archive settings are enabled.
	if ( $check_enabled ) {
		// Enabled.
		if ( $enabled = get_term_meta( $term->term_id, 'enable_content_archive_settings', true ) ) {
			// Reset.
			$level = 1;
			// Return this level's meta.
			return get_term_meta( $term->term_id, $key, true );
		}
		// Not enabled.
		else {
			// Try the parent(s)
			$meta = mai_get_parent_term_meta_recursively( $term, $key, $check_enabled, $meta );
		}
	}
	// Don't check if archive settings are enabled.
	else {
		$meta = get_term_meta( $term->term_id, $key, true );
	}
	if ( ! $meta ) {
		// Try the parent(s)
		return mai_get_parent_term_meta_recursively( $term, $key, $check_enabled, $meta );
	}
	// Reset.
	$level = 1;
	// Return.
	return $meta;
}

/**
 * Continue the recursive term meta function, on a parent.
 *
 * @param  WP_Term     $term           Term object
 * @param  string      $key            The meta key to retrieve.
 * @param  bool        $check_enabled  Whether to check if custom archive settings are enabled.
 * @param  mixed|null  $meta
 *
 * @return mixed|null
 */
function mai_get_parent_term_meta_recursively( $term, $key, $check_enabled, $meta ) {
	// If no parent, use what we have.
	if ( ! mai_has_parent_term( $term ) ) {
		// Reset.
		$level = 1;
		// Return.
		return $meta;
	}
	// Get the parent term.
	$parent_term = get_term_by( 'id', $term->parent, $term->taxonomy );
	if ( false === $parent_term ) {
		// Reset.
		$level = 1;
		// Return.
		return $meta;
	}
	// Try again
	return mai_get_term_meta_recursively( $parent_term, $key, $check_enabled, $meta );
}

/**
 * Checks if the term has a parent.
 *
 * @param  WP_Term  $term Term object.
 *
 * @return bool
 */
function mai_has_parent_term( WP_Term $term ) {
	return ( $term->parent > 0 );
}

以上是关于php 获取该术语的指定元数据值,或从其中一个父术语获取。的主要内容,如果未能解决你的问题,请参考以下文章

pandas读取csv数据index_col参数指定作为行索引的数据列索引列表形成复合(多层)行索引loc基于行层索引元组范围筛选数据(指定起始元组和结束元组其中结束元组只包含最外侧索引值)

pandas读取csv数据index_col参数指定作为行索引的数据列索引列表形成复合(多层)行索引loc基于行层索引元组范围筛选数据(指定起始元组和结束元组其中结束元组只包含最外侧索引值)

ffmpeg 在 php 或 python 中获取元数据

Access SQL:从插入语句或从 DAO.QueryDef 获取受影响记录的标识值

PHP:如何获取当前的 innerHTML?

从 google plus 个人资料中获取经验值和等级或从 google plus 个人资料中获取数据