acf_field_relationship::render_field │ public │ ACF 3.6
Create the HTML interface for your field
Метод класса: acf_field_relationship{}
Хуков нет.
Возвращает
null. Ничего (null).
Использование
$acf_field_relationship = new acf_field_relationship(); $acf_field_relationship->render_field( $field );
- $field(обязательный)
- .
Список изменений
| С версии 3.6 | Введена. |
Код acf_field_relationship::render_field() acf field relationship::render field ACF 6.4.2
<?php
function render_field( $field ) {
// vars
$post_type = acf_get_array( $field['post_type'] );
$taxonomy = acf_get_array( $field['taxonomy'] );
$filters = acf_get_array( $field['filters'] );
// filters
$filter_count = count( $filters );
$filter_post_type_choices = array();
$filter_taxonomy_choices = array();
// post_type filter
if ( in_array( 'post_type', $filters ) ) {
$filter_post_type_choices = array(
'' => __( 'Select post type', 'acf' ),
) + acf_get_pretty_post_types( $post_type );
}
// taxonomy filter
if ( in_array( 'taxonomy', $filters ) ) {
$term_choices = array();
$filter_taxonomy_choices = array(
'' => __( 'Select taxonomy', 'acf' ),
);
// check for specific taxonomy setting
if ( $taxonomy ) {
$terms = acf_get_encoded_terms( $taxonomy );
$term_choices = acf_get_choices_from_terms( $terms, 'slug' );
// if no terms were specified, find all terms
} else {
// restrict taxonomies by the post_type selected
$term_args = array();
if ( $post_type ) {
$term_args['taxonomy'] = acf_get_taxonomies(
array(
'post_type' => $post_type,
)
);
}
// get terms
$terms = acf_get_grouped_terms( $term_args );
$term_choices = acf_get_choices_from_grouped_terms( $terms, 'slug' );
}
// append term choices
$filter_taxonomy_choices = $filter_taxonomy_choices + $term_choices;
}
// div attributes
$atts = array(
'id' => $field['id'],
'class' => "acf-relationship {$field['class']}",
'data-min' => $field['min'],
'data-max' => $field['max'],
'data-s' => '',
'data-paged' => 1,
'data-post_type' => '',
'data-taxonomy' => '',
'data-nonce' => wp_create_nonce( 'acf_field_' . $this->name . '_' . $field['key'] ),
);
?>
<div <?php echo acf_esc_attrs( $atts ); ?>>
<?php
acf_hidden_input(
array(
'name' => $field['name'],
'value' => '',
)
);
?>
<?php
/* filters */
if ( $filter_count ) :
?>
<div class="filters -f<?php echo esc_attr( $filter_count ); ?>">
<?php
/* search */
if ( in_array( 'search', $filters ) ) :
?>
<div class="filter -search">
<?php
acf_text_input(
array(
'placeholder' => __( 'Search...', 'acf' ),
'data-filter' => 's',
)
);
?>
</div>
<?php
endif;
/* post_type */
if ( in_array( 'post_type', $filters ) ) :
?>
<div class="filter -post_type">
<?php
acf_select_input(
array(
'choices' => $filter_post_type_choices,
'data-filter' => 'post_type',
)
);
?>
</div>
<?php
endif;
/* post_type */
if ( in_array( 'taxonomy', $filters ) ) :
?>
<div class="filter -taxonomy">
<?php
acf_select_input(
array(
'choices' => $filter_taxonomy_choices,
'data-filter' => 'taxonomy',
)
);
?>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<div class="selection">
<div class="choices">
<ul class="acf-bl list choices-list"></ul>
</div>
<div class="values">
<ul class="acf-bl list values-list">
<?php
if ( ! empty( $field['value'] ) ) :
// get posts
$posts = acf_get_posts(
array(
'post__in' => $field['value'],
'post_type' => $field['post_type'],
)
);
// loop
foreach ( $posts as $post ) :
?>
<li>
<?php
acf_hidden_input(
array(
'name' => $field['name'] . '[]',
'value' => $post->ID,
)
);
?>
<span tabindex="0" data-id="<?php echo esc_attr( $post->ID ); ?>" class="acf-rel-item acf-rel-item-remove">
<?php echo acf_esc_html( $this->get_post_title( $post, $field ) ); ?>
<a href="#" class="acf-icon -minus small dark" data-name="remove_item"></a>
</span>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div>
</div>
<?php
}