[英]WP How to display a field with a list of all assigned posts of this category in the admin panel of a category?
Please tell me how to display a list of posts to which the same category is assigned in the WordPress admin part of a custom category?请告诉我如何在自定义类别的 WordPress 管理部分中显示分配了相同类别的帖子列表? I used this code, but it's work wrong我使用了这个代码,但它工作错了
// Show posts for some vendor
add_action('admin_init', 'vendors_custom_fields', 1);
function vendors_custom_fields() {
add_action('vendors_edit_form_fields', 'category_custom_fields_form');
}
function category_custom_fields_form($tag) {?>
<tr class="form-field">
<th scope="row">Companies with current manufacturer:</th>
<td>
<?php
$t_id = $tag->term_id;
$cat_meta = get_option("category_$t_id");
$args = array(
'cat' => $t_id,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<p><?php echo get_the_title() ?></p>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'This manufacturer has no designated companies' ); ?></p>
<?php endif; ?>
</td>
</tr>
<?php
}
Right answer正确答案
// Show posts for some vendor
add_action('admin_init', 'vendors_custom_fields', 1);
function vendors_custom_fields() {
add_action('vendors_edit_form_fields', 'category_custom_fields_form');
}
function category_custom_fields_form($tag) {?>
<tr class="form-field">
<th scope="row">Companies with current manufacturer:</th>
<td>
<?php
$t_id = $tag->term_id;
$cat_meta = get_option("category_$t_id");
$args = [
'tax_query' => [
[
'taxonomy' => $tag->taxonomy,
'terms' => $t_id,
],
],
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<span><?php echo get_the_title() ?></span><span> | </span>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'This manufacturer has no designated companies' ); ?></p>
<?php endif; ?>
</td>
</tr>
<?php
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.