按条款显示自定义帖子

如何解决按条款显示自定义帖子

想法是我想在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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-