如何解决按条款显示自定义帖子
想法是我想在WP的前端显示具有特定术语的自定义帖子,但是我很难使用foreach循环来获取特定分类法的term_id。我m using fresh WP installation and I
正在研究二十二十个主题。
因此,首先我在我的function.php中创建自定义帖子类型“设备”,然后我创建了带有术语“新”,“二手”,“颜色”,“ wb”的分类法“ copy_device”。因此,我想显示所有“新彩色复印设备”,而不是“二手彩色复印设备”。
我想在前端显示带有特定术语的自定义帖子类型,所以我做了下面的代码。但是我什么也没得到。。。如果我在page.php中删除if语句“ if($ terms &&!is_wp_error($ terms)){”,则会收到错误消息“警告:/(。中为foreach()提供了无效参数。 。)/ public_html / wp-content / themes / twentytwenty / page.php,第43行“
数组 ( )
第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','menu_name' => esc_html__( 'Devices','parent_item_colon' => esc_html__( 'Parent Device','all_items' => esc_html__( 'All Devices','view_item' => esc_html__( 'View Devices','add_new_item' => esc_html__( 'Add New Devices','add_new' => esc_html__( 'Add New','edit_item' => esc_html__( 'Edit Devices','update_item' => esc_html__( 'Update Devices','search_items' => esc_html__( 'Search Devices','not_found' => esc_html__( 'Not found','not_found_in_trash' => esc_html__( 'Not found in Trash','textdomain' )
);
$args = array(
'label' => esc_html__( 'devices','description' => esc_html__( 'All devices you have','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将返回带有新术语和颜色术语的设备。另外,税务查询中没有'hide_empty'参数,您也可以将其删除。
,对不起,我得到这样的$个类别:
<?php
// get all devices
$categories = get_terms(
array(
'taxonomy' => esc_html__('copy_device'),'hide_empty' => true,)
);?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。