Wordpress - 如何通过其分类过滤添加的自定义帖子?

     2023-02-24     121

关键词:

【中文标题】Wordpress - 如何通过其分类过滤添加的自定义帖子?【英文标题】:Wordpress - how to filter the added custom post through its taxonomy? 【发布时间】:2022-01-19 08:08:23 【问题描述】:

我在我的functions.php 中添加了一个新的自定义帖子,如何通过其分类对现有帖子进行排序?目标是在点击特定类别时过滤现有帖子。下面是示例代码:

在functions.php中添加了自定义帖子:

  $args = array(
   'label' => 'category',
   'public' => true,
   'show_ui' => true,
   'show_in_nav_menus' => true,
   'show_admin_column' => true,
   'hierarchical' => true,
   'query_var' => true
  );
   register_taxonomy('pdf_cat','pdf',$args);

在页面上 - 获取分类并将其作为我的类别选项:

<?php
  $args = array(
  'post_type' => 'pdf',
  'orderby' => 'slug',
  'order' => 'ASC',
  'parent' => 0,
  'hide_empty' => false
   );
  $categories = get_terms([
    'taxonomy' => 'pdf_cat',
    'hide_empty' => false,
     ]);
    foreach( $categories as $category )
      echo '<option><a class="ctg" href="'. get_category_link( $category->term_id ) .' ">' . $category->name . '</a></option>';
     
   ?>   

点击特定类别时通过其分类过滤现有帖子:

            <?php
              $cat = get_the_category();
              $cat = $cat[0]; 
              $catname = get_cat_name($cat->term_id); 
              $catid = get_cat_ID($catname);  
            ?>
              
            <?php
                  $paged = get_query_var('paged', 1);
                  $args = array(  
                      'post_type' => 'pdf',
                      'paged' => $paged,
                      'post_type' => 'post',
                      'cat' => $catid,
                  );
                  $query = new WP_Query($args);         
         
              global $query_string; 
              query_posts( $query_string . "&posts_per_page=15&paged=".$paged );
              while ( have_posts() ) : the_post()
            ?>

【问题讨论】:

【参考方案1】:

你需要使用tax_query

$pdf_args = array(
    'post_type'   => 'pdf',
    'paged'       => $paged,
    'post_status' => 'publish',
    'tax_query'   => array(
        array(
            'taxonomy' => 'pdf_cat',
            'field'    => 'term_id',
            'terms'    => $catid,
        ),
    ),
)

$pdf_data = new WP_Query( $pdf_args );  

了解更多详情here

【讨论】:

这段代码应该放在哪里? 我在如何将术语 id 变成每个类别的永久链接时遇到了麻烦

全代码实现wordpress分类目录和标签添加新的自定义字段

WordPress的分类目录默认只有名称、别名、父节点和描述这几个字段,有时候我们需要给分类目录拓展一些信息,如想添加一个分类封面图、给分类指定keywords和description等等,这个时候我们就得给分类目录添加自定义字段(或者... 查看详情

如何获取在 Wordpress 中按类别过滤的自定义帖子类型的永久链接?

】如何获取在Wordpress中按类别过滤的自定义帖子类型的永久链接?【英文标题】:HowtogetpermalinkofcustomposttypethatfilterbycategoryinWordpress?【发布时间】:2013-02-2308:06:50【问题描述】:我有一个自定义帖子类型,例如“文章”,并使用... 查看详情

php带有排序选项的wordpress的自定义分类法过帐列表过滤器(代码片段)

查看详情

在 wordpress 的自定义页面模板中显示帖子分类信息

】在wordpress的自定义页面模板中显示帖子分类信息【英文标题】:Showposttaxonomyinformationincustompagetemplateinwordpress【发布时间】:2017-11-2510:08:41【问题描述】:我有一个正在通过子主题编辑的主题。我已经能够创建自定义页面模板... 查看详情

Wordpress 过滤自定义帖子类型分类

】Wordpress过滤自定义帖子类型分类【英文标题】:Wordpressfilteringcustomposttypetaxonomy【发布时间】:2020-11-1719:01:03【问题描述】:我一直在关注本指南:https://rudrastyh.com/wordpress/ajax-post-filters.html#comment-908。我有一个名为“project”的... 查看详情

为wordpress的论文主题添加多个压缩页面,并使用论文1.8的自定义过滤器

Togetthesimplestsqueezepagepossible:noheader,sidebarsorfooter.PastethefollowingPHPincustom_functions.php.Thefirstfunctionisthefilterthatreturns“false”insteadofreturningthecontentthatwouldnormalbet 查看详情

嵌套的 wordpress 术语列表 - 过滤器

】嵌套的wordpress术语列表-过滤器【英文标题】:Nestedwordpresstermslist-filter【发布时间】:2019-03-2818:11:39【问题描述】:我使用插件CPTUI在Wordpress中创建了自定义术语分类。所以我可以将“产品类别”添加到我的自定义帖子类型“... 查看详情

如何通过分类术语从自定义 WordPress MySQL 查询中排除结果

】如何通过分类术语从自定义WordPressMySQL查询中排除结果【英文标题】:HowToExcludeResultsFromCustomWordPressMySQLQueryByTaxonomyTerm【发布时间】:2015-09-1107:34:37【问题描述】:我只想显示分类“product-brand”中没有术语“brand-slug”的帖子。... 查看详情

为wordpress的论文主题添加压缩页面,并使用论文1.8的自定义过滤器

Togetthesimplestsqueezepagepossible:noheader,sidebarsorfooter.PastethefollowingPHPincustom_functions.php.Thefirstfunctionisthefilterthatreturns“false”insteadofreturningthecontentthatwouldnormalbethere(intheheader,orfooter,orsidebar).Thesecondfunctionselectsthepageonwhichtoapplythefiltera... 查看详情

为wordpress的论文主题添加压缩页面,并使用论文1.8的自定义过滤器

Togetthesimplestsqueezepagepossible:noheader,sidebarsorfooter.PastethefollowingPHPincustom_functions.php.Thefirstfunctionisthefilterthatreturns“false”insteadofreturningthecontentthatwouldnormalbethere(intheheader,orfooter,orsidebar).Thesecondfunctionselectsthepageonwhichtoapplythefiltera... 查看详情

如何实现wordpress的友情链接两列显示,分类目录两列显示

1、链接——添加——输入名称和web地址。2、添加分类目录——输入“友情链接”——添加;打开方式选择第一个“_blank—新窗口或新标签”。3、在右侧,点击“添加链接”。已经添加上,首页是不显示的,可以通过以下方式来... 查看详情

wordpress上面如何在导航栏上面添加子分类

...个做了之后,在网站上面还是没有显示出来参考技术A在wordpress后台添加子分类,子分类的html结构是一定的,可以使用jquery控制参考该篇文章:http://www.tennfy.com/484.html 参考技术B直接拉到父菜单下面即可 查看详情

Wordpress - 按“post_type”检索所有帖子,然后按分类过滤

】Wordpress-按“post_type”检索所有帖子,然后按分类过滤【英文标题】:Wordpress-RetrieveallPostsby\'post_type\'thenfilterbytaxonomy【发布时间】:2020-06-0403:53:12【问题描述】:我有一个安装了ACF(高级自定义字段)的Wordpress安装。我创建了C... 查看详情

Wordpress - 如何使用 URL 中的搜索查询参数“标签”注册自定义分类并使其工作?

】Wordpress-如何使用URL中的搜索查询参数“标签”注册自定义分类并使其工作?【英文标题】:Wordpress-HowcanIregisteracustomtaxonomywiththesearchqueryparam\'tag\'inURLandmakeitwork?【发布时间】:2021-10-0114:49:27【问题描述】:我注册了一个自定义... 查看详情

Weka 添加多个 Meta 过滤分类器

...teredclassifier【发布时间】:2016-01-1522:41:53【问题描述】:如何添加超过1个元过滤分类器?例如,我想应用SMOTE过滤器两次,因为我的数据有3个类别,比例为6:3:1。因此,为了平衡数据,我必须运行SMOTE三次,通过首先给出200%-->... 查看详情

如何在 wordpress 中显示自定义帖子类型类别?

】如何在wordpress中显示自定义帖子类型类别?【英文标题】:Howtodisplaycustomposttypecategoryinwordpress?【发布时间】:2014-04-1416:13:21【问题描述】:最近我在wordpress中创建了一个自定义帖子类型,并添加了名为category和tags的分类法。... 查看详情

Wordpress:如何通过自定义分类法在作者页面中显示帖子计数

】Wordpress:如何通过自定义分类法在作者页面中显示帖子计数【英文标题】:Wordpress:Howtodisplaypostcountinauthorpagebycustomtaxonomy【发布时间】:2015-09-1420:37:51【问题描述】:我正在尝试使用计数器在作者页面中显示自定义分类,但似... 查看详情

WordPress:如何使用 $wp_query 按类别过滤帖子?

】WordPress:如何使用$wp_query按类别过滤帖子?【英文标题】:WordPress:Howtofilterpostsbycategoryusing$wp_query?【发布时间】:2017-06-1216:41:41【问题描述】:我在WordPress上构建了一个带有静态首页的自定义主题,并且在设置>阅读设置>... 查看详情