我有自定义字段my_cf
用于分类/术语。如何使用分类法/术语的自定义字段获取和输出值?
我尝试使用:
$variable = get_field(\'my_cf\', \'basic\');
echo $variable;
where basic—我的分类法的名称。但这行不通。有什么建议吗?
我有自定义字段my_cf
用于分类/术语。如何使用分类法/术语的自定义字段获取和输出值?
我尝试使用:
$variable = get_field(\'my_cf\', \'basic\');
echo $variable;
where basic—我的分类法的名称。但这行不通。有什么建议吗?
我真的无法解释ACF documentation page I posted in the comments:
所有API函数都可以与分类术语一起使用,但是,需要第二个参数来定位术语ID。这类似于通过post\\u ID来定位特定的post对象。
所需的$post\\u id是一个字符串,其中包含taxonomy name + the term ID 在此格式中:$TaxonomyName_$TermID
因此,如果您的自定义字段是my_cf
, 你的分类名称是basic
(不是术语名称)并且分类法中的术语ID是42,那么您需要:
$variable = get_field( \'my_cf\', \'basic_42\' );
现场数据是否存储在wp\\U选项中?如果是这样。。。
$term_id = 12345;
$term_meta = get_option( \'taxonomy_\' . $term_id );
$my_cf = $term_meta[ \'my_cf\' ];
echo $my_cf;
我使用CMB2 设置自定义字段,在许多情况下,逻辑与ACF没有太大区别。对于我的特定用例,我创建了一个非常简单但灵活的函数,以便对taxonomy 在显示之前custom field 价值
考虑到已经创建了一个名为my_cf
比如说taxonomy 已命名basic 根据您的示例,以下函数可能有助于回答您的问题,并可能会稍微扩展自定义字段的使用范围。
function get_taxonomy_terms_custom_fields( $taxonomy = \'\' ) {
global $post;
$terms = get_the_terms( $post->ID, $taxonomy );
// Check if we have a taxonomy and that it is valid. If not, return false
if ( !$taxonomy )
return false;
// Sanitize the taxonomy input
$taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
// keep playing safe
if ( !taxonomy_exists( $taxonomy ) )
return false;
foreach ( $terms as $term ) {
// Set a variable for taxonomy term_id
$tax_term_id = $term->term_id;
$my_field = get_term_meta( $tax_term_id, \'my_cf\', true );
// Make sure we do not have a WP_Error object, not really necessary, but better be safe
if ( is_wp_error( $term ) )
continue;
// escaping the returned value // esc_html(), esc_url(), esc_attr()
return esc_html($my_field);
}
}
简单使用<?php get_taxonomy_terms_custom_fields (\'basic\'); ?>
更换basic
使用您自己的分类名称。功能get_taxonomy_terms_custom_fields ()
将通过分配给post、post\\U类型的所有类别检查指定的分类法和循环类型,然后返回自定义字段值(如果存在),如果没有,则避免出错。还可以对其进行扩展,以检查是否有生成数组()的字段,例如可重复的字段。
我希望有帮助-祝你好运!
我有一个称为“水果”的自定义帖子类型,在这个类型下,我有一个称为“水果类别”的层次分类法当我在Fruit下创建一个新帖子时,我可以将其分配到一个“术语”,就像您在普通帖子类别下一样,但在Fruit Categories分类下。我在Fruit上增加了三个职位,每个职位分别分配给一个任期;“苹果”、“梨”和“香蕉”。所有这些都很好,但现在我想创建一个归档页面,简单地列出UL中的术语,用于自定义帖子类型,如下所示;我的网站。玉米/水果我面临的问题是,我使用的每个查询似乎只返回第一个词Apples。其他两个没有显
我有自定义字段my_cf
用于分类/术语。如何使用分类法/术语的自定义字段获取和输出值?
我尝试使用:
$variable = get_field(\'my_cf\', \'basic\');
echo $variable;
where basic—我的分类法的名称。但这行不通。有什么建议吗?
我真的无法解释ACF documentation page I posted in the comments:
所有API函数都可以与分类术语一起使用,但是,需要第二个参数来定位术语ID。这类似于通过post\\u ID来定位特定的post对象。
所需的$post\\u id是一个字符串,其中包含taxonomy name + the term ID 在此格式中:$TaxonomyName_$TermID
因此,如果您的自定义字段是my_cf
, 你的分类名称是basic
(不是术语名称)并且分类法中的术语ID是42,那么您需要:
$variable = get_field( \'my_cf\', \'basic_42\' );
现场数据是否存储在wp\\U选项中?如果是这样。。。
$term_id = 12345;
$term_meta = get_option( \'taxonomy_\' . $term_id );
$my_cf = $term_meta[ \'my_cf\' ];
echo $my_cf;
我使用CMB2 设置自定义字段,在许多情况下,逻辑与ACF没有太大区别。对于我的特定用例,我创建了一个非常简单但灵活的函数,以便对taxonomy 在显示之前custom field 价值
考虑到已经创建了一个名为my_cf
比如说taxonomy 已命名basic 根据您的示例,以下函数可能有助于回答您的问题,并可能会稍微扩展自定义字段的使用范围。
function get_taxonomy_terms_custom_fields( $taxonomy = \'\' ) {
global $post;
$terms = get_the_terms( $post->ID, $taxonomy );
// Check if we have a taxonomy and that it is valid. If not, return false
if ( !$taxonomy )
return false;
// Sanitize the taxonomy input
$taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
// keep playing safe
if ( !taxonomy_exists( $taxonomy ) )
return false;
foreach ( $terms as $term ) {
// Set a variable for taxonomy term_id
$tax_term_id = $term->term_id;
$my_field = get_term_meta( $tax_term_id, \'my_cf\', true );
// Make sure we do not have a WP_Error object, not really necessary, but better be safe
if ( is_wp_error( $term ) )
continue;
// escaping the returned value // esc_html(), esc_url(), esc_attr()
return esc_html($my_field);
}
}
简单使用<?php get_taxonomy_terms_custom_fields (\'basic\'); ?>
更换basic
使用您自己的分类名称。功能get_taxonomy_terms_custom_fields ()
将通过分配给post、post\\U类型的所有类别检查指定的分类法和循环类型,然后返回自定义字段值(如果存在),如果没有,则避免出错。还可以对其进行扩展,以检查是否有生成数组()的字段,例如可重复的字段。
我希望有帮助-祝你好运!
我的自定义帖子类型中有3篇帖子(\'careers\'). 我注册了自定义分类法(\'career_categories\').我创建了3个职位,其中两个职位属于“全职”,一个职位属于“兼职”。我在循环中使用以下代码:$terms = get_terms( \'career_categories\', array( \'hide_empty\' => true, ) ); $html_out .= \'<td class=\"column-\'. $col++ .\'\"&g