按条款显示自定义帖子
Display the custom posts by the terms
我想在我的 WP 前端显示具有特定术语的自定义 posts,但我无法使用 foreach 循环获得特定分类法的 term_id。我m using fresh WP installation and I
正在使用二十二十主题。
因此,首先我创建自定义 post 在我的 function.php 中键入“设备”,然后我创建分类法“copy_device”,其中包含术语“新”、“二手”、“颜色” ”,“wb”。所以我想显示例如所有“新的彩色复印设备”而不是“用过的彩色复印设备”。
我正在尝试在前端显示带有特定条款的自定义 post 类型,因此我执行了以下代码。但是我什么也得不到...如果我在 page.php 中删除 if 语句“if ( $terms && ! is_wp_error( $terms ) ){” 我会收到错误消息“警告:为 foreach 提供的参数无效( ) 在第 43 行的 /(...)/public_html/wp-content/themes/twentytwenty/page.php 中
数组
(
)
第 43 行是“foreach ( $terms as $term ) {”
FUNCTION.php
function td_devices_posttype() {
$labels = array(
'name' => _x( 'Devices', 'Post Type General Name', 'textdomain' ),
'singular_name' => _x( 'Device', 'Post Type Singular Name', 'textdomain' ),
'menu_name' => esc_html__( 'Devices', 'textdomain' ),
'parent_item_colon' => esc_html__( 'Parent Device', 'textdomain' ),
'all_items' => esc_html__( 'All Devices', 'textdomain' ),
'view_item' => esc_html__( 'View Devices', 'textdomain' ),
'add_new_item' => esc_html__( 'Add New Devices', 'textdomain' ),
'add_new' => esc_html__( 'Add New', 'textdomain' ),
'edit_item' => esc_html__( 'Edit Devices', 'textdomain' ),
'update_item' => esc_html__( 'Update Devices', 'textdomain' ),
'search_items' => esc_html__( 'Search Devices', 'textdomain' ),
'not_found' => esc_html__( 'Not found', 'textdomain' ),
'not_found_in_trash' => esc_html__( 'Not found in Trash', 'textdomain' )
);
$args = array(
'label' => esc_html__( 'devices', 'textdomain' ),
'description' => esc_html__( 'All devices you have', 'textdomain' ),
'labels' => $labels,
'taxonomies' => array( 'copy_device'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 100,
'can_export' => true,
'has_archive' => esc_html__( 'devices' ),
'exclude_from_search' => false,
'publicly_queryable' => true,
'query_var' => true,
'show_admin_column' => true,
'capability_type' => 'post',
'rewrite' => array('slug' => 'devices'),
'supports' => array( 'title','editor','thumbnail', 'custom-fields')
);
register_post_type( 'devices', $args );
}
add_action( 'init', 'td_devices_posttype' );
// custom taxonomies Copy devices
function td_posttype_taxonomy() {
$labels = array(
'name' => 'Copy Devices',
'singular_name' => 'Copy Device',
'search_items' => 'Search items',
'all_items' => 'All items',
'parent_item' => 'Parent item',
'parent_item_colon' => 'Parent item colon:',
'edit_item' => 'Edit item',
'update_item' => 'Update item',
'add_new_item' => 'Add new item',
'new_item_name' => 'New item name',
'menu_name' => 'Copy Devices'
);
$args = array (
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'copy-device')
);
register_taxonomy( 'copy_device', array( 'devices' ), $args );
}
add_action('init', 'td_posttype_taxonomy');
在PAGE.php中我有以下代码:
// trying to hook terms ID`s (Not working?)
$termID = array();
$terms = get_the_terms($post->ID, 'copy_device');
if ( $terms && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo '<pre>'; print_r(array_values($termID)); echo '</pre>';
}
// Loop and return terms
foreach ( $categories as $categorie ):
// set up a new query
$args = new WP_Query(
array(
'post_type' => 'devices',
'tax_query' => array(
array(
'taxonomy' => 'copy_device',
'terms' => array( 'new', 'colour' ),
'field' => 'slug',
'hide_empty' => false,
)
)
)
);
?>
<h3><?php echo $categorie->name; ?></h3>
<ul>
<?php while ($args->have_posts()) : $args->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
$args = null;
wp_reset_postdata();
endforeach;
?>
如果我执行以下操作:
array(
'taxonomy' => esc_html__('copy_device'),
'hide_empty' => true,
)
);?>
我得到:
Array
(
[0] => WP_Term Object
(
[term_id] => 12
[name] => BW
[slug] => bw
[term_group] => 0
[term_taxonomy_id] => 12
[taxonomy] => copy_device
[description] =>
[parent] => 9
[count] => 1
[filter] => raw
)
[1] => WP_Term Object
(
[term_id] => 11
[name] => Colour
[slug] => colour
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => copy_device
[description] =>
[parent] => 9
[count] => 1
[filter] => raw
)
[2] => WP_Term Object
(
[term_id] => 9
[name] => New
[slug] => new
[term_group] => 0
[term_taxonomy_id] => 9
[taxonomy] => copy_device
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
[3] => WP_Term Object
(
[term_id] => 10
[name] => Used
[slug] => used
[term_group] => 0
[term_taxonomy_id] => 10
[taxonomy] => copy_device
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
)
)
我花了这么多天的时间来完成这项工作。我正在阅读法典、论坛、网站,并尝试使用不同的方法,但我失败了……有没有铁杆成员可以帮助我完成这项工作?
使用此代码:
$args = array( 'post_type' => 'devices');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$terms = get_the_terms( get_the_id(), 'taxonomy');
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo $termID[0]; `
endwhile;
`
什么是$categories? foreach 的错误大概就是由此而来。我认为您不需要在循环内调用 WP_Query 。删除 ( $categories as $categorie ) 的 foreach 循环,WP_Query 将 return 具有新术语和颜色术语的设备。另外,tax查询中没有'hide_empty'参数,你也可以去掉。
很抱歉我得到这样的 $categories:
<?php
// get all devices
$categories = get_terms(
array(
'taxonomy' => esc_html__('copy_device'),
'hide_empty' => true,
)
);?>
我想在我的 WP 前端显示具有特定术语的自定义 posts,但我无法使用 foreach 循环获得特定分类法的 term_id。我m using fresh WP installation and I
正在使用二十二十主题。
因此,首先我创建自定义 post 在我的 function.php 中键入“设备”,然后我创建分类法“copy_device”,其中包含术语“新”、“二手”、“颜色” ”,“wb”。所以我想显示例如所有“新的彩色复印设备”而不是“用过的彩色复印设备”。
我正在尝试在前端显示带有特定条款的自定义 post 类型,因此我执行了以下代码。但是我什么也得不到...如果我在 page.php 中删除 if 语句“if ( $terms && ! is_wp_error( $terms ) ){” 我会收到错误消息“警告:为 foreach 提供的参数无效( ) 在第 43 行的 /(...)/public_html/wp-content/themes/twentytwenty/page.php 中
数组 ( )
第 43 行是“foreach ( $terms as $term ) {”
FUNCTION.php
function td_devices_posttype() {
$labels = array(
'name' => _x( 'Devices', 'Post Type General Name', 'textdomain' ),
'singular_name' => _x( 'Device', 'Post Type Singular Name', 'textdomain' ),
'menu_name' => esc_html__( 'Devices', 'textdomain' ),
'parent_item_colon' => esc_html__( 'Parent Device', 'textdomain' ),
'all_items' => esc_html__( 'All Devices', 'textdomain' ),
'view_item' => esc_html__( 'View Devices', 'textdomain' ),
'add_new_item' => esc_html__( 'Add New Devices', 'textdomain' ),
'add_new' => esc_html__( 'Add New', 'textdomain' ),
'edit_item' => esc_html__( 'Edit Devices', 'textdomain' ),
'update_item' => esc_html__( 'Update Devices', 'textdomain' ),
'search_items' => esc_html__( 'Search Devices', 'textdomain' ),
'not_found' => esc_html__( 'Not found', 'textdomain' ),
'not_found_in_trash' => esc_html__( 'Not found in Trash', 'textdomain' )
);
$args = array(
'label' => esc_html__( 'devices', 'textdomain' ),
'description' => esc_html__( 'All devices you have', 'textdomain' ),
'labels' => $labels,
'taxonomies' => array( 'copy_device'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 100,
'can_export' => true,
'has_archive' => esc_html__( 'devices' ),
'exclude_from_search' => false,
'publicly_queryable' => true,
'query_var' => true,
'show_admin_column' => true,
'capability_type' => 'post',
'rewrite' => array('slug' => 'devices'),
'supports' => array( 'title','editor','thumbnail', 'custom-fields')
);
register_post_type( 'devices', $args );
}
add_action( 'init', 'td_devices_posttype' );
// custom taxonomies Copy devices
function td_posttype_taxonomy() {
$labels = array(
'name' => 'Copy Devices',
'singular_name' => 'Copy Device',
'search_items' => 'Search items',
'all_items' => 'All items',
'parent_item' => 'Parent item',
'parent_item_colon' => 'Parent item colon:',
'edit_item' => 'Edit item',
'update_item' => 'Update item',
'add_new_item' => 'Add new item',
'new_item_name' => 'New item name',
'menu_name' => 'Copy Devices'
);
$args = array (
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'copy-device')
);
register_taxonomy( 'copy_device', array( 'devices' ), $args );
}
add_action('init', 'td_posttype_taxonomy');
在PAGE.php中我有以下代码:
// trying to hook terms ID`s (Not working?)
$termID = array();
$terms = get_the_terms($post->ID, 'copy_device');
if ( $terms && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo '<pre>'; print_r(array_values($termID)); echo '</pre>';
}
// Loop and return terms
foreach ( $categories as $categorie ):
// set up a new query
$args = new WP_Query(
array(
'post_type' => 'devices',
'tax_query' => array(
array(
'taxonomy' => 'copy_device',
'terms' => array( 'new', 'colour' ),
'field' => 'slug',
'hide_empty' => false,
)
)
)
);
?>
<h3><?php echo $categorie->name; ?></h3>
<ul>
<?php while ($args->have_posts()) : $args->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
$args = null;
wp_reset_postdata();
endforeach;
?>
如果我执行以下操作:
array(
'taxonomy' => esc_html__('copy_device'),
'hide_empty' => true,
)
);?>
我得到:
Array
(
[0] => WP_Term Object
(
[term_id] => 12
[name] => BW
[slug] => bw
[term_group] => 0
[term_taxonomy_id] => 12
[taxonomy] => copy_device
[description] =>
[parent] => 9
[count] => 1
[filter] => raw
)
[1] => WP_Term Object
(
[term_id] => 11
[name] => Colour
[slug] => colour
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => copy_device
[description] =>
[parent] => 9
[count] => 1
[filter] => raw
)
[2] => WP_Term Object
(
[term_id] => 9
[name] => New
[slug] => new
[term_group] => 0
[term_taxonomy_id] => 9
[taxonomy] => copy_device
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
[3] => WP_Term Object
(
[term_id] => 10
[name] => Used
[slug] => used
[term_group] => 0
[term_taxonomy_id] => 10
[taxonomy] => copy_device
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
)
)
我花了这么多天的时间来完成这项工作。我正在阅读法典、论坛、网站,并尝试使用不同的方法,但我失败了……有没有铁杆成员可以帮助我完成这项工作?
使用此代码:
$args = array( 'post_type' => 'devices');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$terms = get_the_terms( get_the_id(), 'taxonomy');
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo $termID[0]; `
endwhile;
`
什么是$categories? foreach 的错误大概就是由此而来。我认为您不需要在循环内调用 WP_Query 。删除 ( $categories as $categorie ) 的 foreach 循环,WP_Query 将 return 具有新术语和颜色术语的设备。另外,tax查询中没有'hide_empty'参数,你也可以去掉。
很抱歉我得到这样的 $categories:
<?php
// get all devices
$categories = get_terms(
array(
'taxonomy' => esc_html__('copy_device'),
'hide_empty' => true,
)
);?>