wordpress侧边栏_如何在WordPress侧边栏小部件中显示自定义分类术语

wordpress侧边栏

WordPress has come a long way from being a simple blogging platform. With additions of custom post types and custom taxonomies, it is being used as a true content management system (CMS). In response from our article on how to create custom taxonomies in WordPress, some of our readers asked us how to display custom taxonomy terms in sidebar widgets. In this article we will show you how to display custom taxonomy terms in WordPress sidebar widgets.

WordPress从成为一个简单的博客平台已经走了很长一段路。 加上自定义帖子类型自定义分类法 ,它已被用作真正的内容管理系统(CMS)。 在我们有关如何在WordPress中创建自定义分类法的文章的回应 ,一些读者问我们如何在侧边栏小部件中显示自定义分类法术语。 在本文中,我们将向您展示如何在WordPress侧边栏小部件中显示自定义分类术语。

We will show you how to display custom taxonomy terms in sidebar widgets using two different methods. The first method is by installing a plugin which is simpler and easier. We recommend using this method for majority of our audience. The second method involves creating your own shortcode. This method is for those who like to know how things work and have more control over the output.

我们将向您展示如何使用两种不同的方法在侧边栏小部件中显示自定义分类术语。 第一种方法是安装一个更简单易用的插件。 我们建议大多数观众使用此方法。 第二种方法涉及创建自己的简码。 该方法适用于那些想知道事物如何工作并且对输出有更多控制权的人。

在窗口小部件(插件)中添加自定义分类术语 (Adding Custom Taxonomy Terms in a Widget (Plugin))

To display custom taxonomy terms in sidebar or other widget areas using a plugin, the first thing you need to do is install and activate Custom Taxonomies Menu Widget plugin. Upon activation, it adds a custom taxonomies menu widget under Appearance » Widgets. Drag and drop the widget to your sidebar. The widget configuration options allow you to choose the taxonomies you want to display or exclude. It also allows you to exclude terms inside a taxonomy.

要使用插件在侧边栏或其他窗口小部件区域中显示自定义分类术语,您需要做的第一件事是安装并激活“ 自定义分类菜单”窗口小部件插件。 激活后,它将在“ 外观»小部件”下添加一个自定义分类菜单小部件 。 将小部件拖放到侧边栏。 窗口小部件配置选项使您可以选择要显示或排除的分类法。 它还允许您排除分类法中的术语。

Showing Custom Taxonomies in Sidebar using Widget
使用简码添加自定义分类术语 (Adding Custom Taxonomy Terms with Shortcode)

Custom Taxonomies Menu Widget plugin allows you to easily display terms from any custom taxonomy, without worrying about the code. However some users would want to learn how to do it manually, so that they could have more control on how terms for their custom taxonomy appears in their widgets. Furthermore, using the shortcode method allows you to display taxonomy terms within your post content should you need to.

自定义分类法菜单小部件插件使您可以轻松显示任何自定义分类法中的术语,而无需担心代码。 但是,有些用户希望学习手动操作方法,以便可以更好地控制其自定义分类法的术语在其小部件中的显示方式。 此外,使用简码方法可让您在需要的帖子内容中显示分类术语。

First we need to create a shortcode that displays a list of terms and accepts parameters. The only parameter we need is the name of the taxonomy. Add this code in a site-specific plugin:

首先,我们需要创建一个短代码,以显示术语列表并接受参数。 我们唯一需要的参数是分类法的名称。 将此代码添加到特定站点的插件中



// First we create a function
function list_terms_custom_taxonomy( $atts ) {

// Inside the function we extract custom taxonomy parameter of our shortcode

	extract( shortcode_atts( array(
		'custom_taxonomy' => '',
	), $atts ) );

// arguments for function wp_list_categories
$args = array( 
taxonomy => $custom_taxonomy,
title_li => ''
);

// We wrap it in unordered list 
echo '<ul>'; 
echo wp_list_categories($args);
echo '</ul>';
}

// Add a shortcode that executes our function
add_shortcode( 'ct_terms', 'list_terms_custom_taxonomy' );

//Allow Text widgets to execute shortcodes

add_filter('widget_text', 'do_shortcode');

The code above creates a shortcode ct_terms that requires one parameter custom_taxonomy. To use this shortcode drag and drop a Text widget into your sidebar. Add this shortcode in your Widget and save.

上面的代码创建了一个ct_terms ,它需要一个参数custom_taxonomy。 要使用此短代码,请将“文本”小部件拖放到侧边栏中。 将此简码添加到您的小部件中并保存。

[ct_terms custom_taxonomy=customtaxonomyname]

[ct_terms custom_taxonomy = customtaxonomyname]

Replace customtaxonomyname with the name of the taxonomy you want to list.

用要列出的分类名称替换customtaxonomyname。

We hope that you find this article useful in displaying custom taxonomy terms in your sidebar or other widget area. Let us know how you prefer to list terms of a custom taxonomy in the comments below.

我们希望您发现本文对在侧边栏或其他窗口小部件区域显示自定义分类术语很有用。 让我们知道您更喜欢在以下评论中列出自定义分类法的术语。

翻译自: https://www.wpbeginner.com/plugins/how-to-display-custom-taxonomy-terms-in-wordpress-sidebar-widgets/

wordpress侧边栏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值