Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Hur man visar custom taxonomi termer i WordPress sidebar widgets

Vill du visa customize taxonomi termer på din WordPress sidebar?

Custom taxonomies allow you to group your content beyond categories and tags, till exempel genom att organisera book reviews efter fiction och nonfiction. Att displayed de taxonomi termer du använder kan hjälpa dina besökare att hitta det content de letar efter.

I den här artikeln visar vi hur du visar custom taxonomi termer i WordPress sidebar widgets.

How to Display Custom Taxonomy Terms in WordPress Sidebar Widgets

Varför visa customize taxonomi termer i WordPress?

När du först konfigurerar din WordPress-webbplats kan du använda kategorier och tags för att organisera dina posts. Dessa två taxonomier är all många websites behöver.

Vissa webbplatser kan dock dra nytta av anpassade taxonomier som ett extra sätt att organisera grupper av inlägg och custom post types.

Om du till exempel skapar en custom post type som heter ”Books”, kan du sortera den med hjälp av en custom taxonomi som heter ”Topics” som har termer som Adventure, Romance och Horror.

Sedan kan du hjälpa dina läsare att enkelt sortera böcker efter ämnen genom att visa dessa termer i sidebaren i WordPress.

Med detta sagt, låt oss ta en titt på hur man visar custom taxonomi termer i WordPress sidebar widgets. Vi kommer att täcka två metoder, och den första rekommenderas för de flesta användare.

Visning av custom taxonomi termer i en widget med hjälp av ett plugin

Det första du behöver göra är att installera och aktivera TaxoPress är WordPress Tag, Category, and Taxonomy Manager plugin. För mer detaljer, se vår steg-för-steg guide om hur du installerar ett WordPress plugin.

Skapa en lista med customize taxonomi-termer

När du har aktiverat måste du navigera till TaxoPress ” Terms Display page i din WordPress admin area. Här kan du konfigurera vilka custom taxonomies och termer som du vill visa på din website.

Configure How Terms Are Displayed

Notice that one ”Terms Display” object has already been created for you, and you can edit it to display the taxonomi you wish to display on your site. Men om du vill visa mer än en lista med termer måste du uppgradera till Pro-versionen.

Du kan klicka på ”Terms Display” för att konfigurera objektet. Då kommer du till vyn ”Edit Terms Display” där du kan välja vilka objekt i taxonomin du vill visa.

Configure How Terms Will Be Displayed

Du måste ge listan med termer en rubrik. I den här tutorialen anropar vi den ”Book Topics”.

Därefter måste du visa den post type och taxonomi som du vill visa i din sidebar. På vår demo website har vi en custom post type som heter ”Books” och en custom taxonomi som heter ”Topics”, så vi väljer dessa.

Om du behöver hjälp med att skapa en custom taxonomi på din website kan du läsa vår guide om hur du skapar custom taxonomier i WordPress.

Slutligen måste du välja vilket format du vill använda för displayen. Du kan välja mellan ”Moln” och ”Lista”. I den här tutorialen väljer vi alternativet ”Lista”.

För många websites är detta de enda alternativen du behöver konfigurera, så du kan gratis klicka på knappen ”Save Terms Display” för att store dina inställningar.

Det finns dock ytterligare alternativ tillgängliga på de andra tabbarna på sidan Edit Terms Display, och vi ska titta på dem kortfattat.

Som standard visas alla termer i den valda taxonomin, men på tabben ”Choose Terms” kan du begränsa termerna till dem som har använts senaste tiden, t.ex. under den senaste månaden eller det senaste året. Du kan också välja sorteringsorder för termerna och andra relaterade settings.

Choose the Terms That Will Be Displayed

På tabben Design finns inställningar för hur din lista med taxonomi-termer ska se ut.

Du kan till exempel välja hur många termer som ska visas, välja storlek och färg på fonten med mera.

Choose How the Terms Will Be Displayed

Tabben Display Format är mer teknisk och låter dig ändra formatet på dina termer.

You will need to follow the link to the provided documentation to learn what to enter in each field.

Terms Display Format

Om du har ändrat någon av dessa ytterligare Settings, se till att du klickar på knappen ”Save Terms Display” längst ner på sidan för att lagra dem.

Displaying Your List of Custom Taxonomi Terms

Nu måste du add to listan med taxonomi termer till WordPress sidebar. Du kan göra detta med hjälp av en shortcode eller en widget. I denna tutorial kommer vi att använda en widget.

Först måste du navigera till sidan Appearance ” Widgets och sedan klicka på knappen ’+ Toggle block inserter’ högst upp på sidan. Därefter måste du lokalisera widgeten ”Terms Display (TaxoPress Shortcode)” och sedan dra den till din sidebar.

Add the Terms Display (TaxoPress Shortcode) Widget

Nu måste du välja den lista med termer som du vill visa från rullgardinsmenyn. I den här tutorialen väljer vi listan ”Book Topics” som vi just skapade.

You can now click the ”Update” button at the top of the page to save the new widget, and then visit your website to see it in action. Så här ser det ut på vår demo website.

Custom Taxonomy Terms Preview

Visa customize taxonomi termer i en widget med hjälp av kod

Den här metoden är mer avancerad och rekommenderas för dem som är vana vid att arbeta med code snippets och gillar att veta hur saker och ting fungerar. Denna metod ger dig också mer control över hur custom taxonomi termer visas i dina widgets.

Först måste du add to den här koden i functions.php-filen i ditt theme eller i ett site-specifikt 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');

Koden skapar en shortcode ct_terms som är obligatorisk med en parameter custom_taxonomy.

För att använda denna shortcode måste du besöka appearance ” Widgets och drag and drop en widget för genväg till din sidebar. Därefter ska du lägga till följande shortcode till widgeten och sedan klicka på knappen ”Update” högst upp på Widgets page.

[ct_terms custom_taxonomy=customtaxonomyname]

Se till att du ersätter customtaxonomyname med namnet på den taxonomi som du vill lista. I den här tutorialen använder vi taxonomin ”ämnen”.

Adding the Shortcode Widget to the Sidebar

När du har updateat dina widgetar kommer din lista med custom taxonomi termer att visas på WordPress sidebar.

Så här ser det ut på vår demo website. Du kanske också gillar att add to en widget titel för att beskriva listan över taxonomi termer.

Preview of Custom Taxonomy Terms

Vi hoppas att denna tutorial hjälpte dig att lära dig hur du visar custom taxonomi termer i WordPress sidebar widgets. Du kanske också vill lära dig hur du trackar besökare på din WordPress site, eller kontrollera vår lista över de bästa tillägg för kontaktformulär för WordPress.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Avslöjande: Vårt innehåll stöds av våra läsare. Det innebär att om du klickar på några av våra länkar, kan vi tjäna en provision. Se hur WPBeginner finansieras, varför det är viktigt, och hur du kan stödja oss. Här är vår editoriala process.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Den ultimata WordPress-verktygslådan

Få GRATIS tillgång till vår verktygslåda - en samling WordPress-relaterade produkter och resurser som varje professionell användare bör ha!

Reader Interactions

21 kommentarerLämna ett svar

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Amina says

    I have been trying to produce a simple list of custom tags on my website for hours! When I tried this it really was with little hope, so I couldn’t believe it when it worked! Thank you for all the work you are doing at WPBeginner; it’s incredibly useful.

  3. Jared says

    ”This plugin has been closed as of November 25, 2018 and is not available for download. Reason: Guideline Violation.”

    • WPBeginner Support says

      Thank you for letting us know, we will certainly take a look and update this article when we are able.

      Administratör

  4. Mario says

    I am getting this warning in my site:

    Warning: Use of undefined constant taxonomy – assumed ’taxonomy’ (this will throw an Error in a future version of PHP) in…

    • WPBeginner Support says

      It would depend on where that error message is being generated from. If the error says it is from wp-content/plugins/plugin name then you would want to reach out to that plugin’s support and they should be able to assist.

      Administratör

  5. Antonio says

    Hi, the code give me this warning:

    Warning: Use of undefined constant taxonomy – assumed ’taxonomy’ (this will throw an Error in a future version of PHP) in /home/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(361) : eval()’d code on line 12

    How can I fix it?
    Thanks

    • WPBeginner Support says

      on line 12 of the code, you would surround the word taxonomy with single quotes (’ ’) to remove that error :)

      Administratör

  6. Nicole says

    Hello, trying to apply this from within a post. I want it to list the custom taxonomies I have associated the post to and this is not working. How could I do this? Thanks!

    • Dennis says

      Yea I’m currently scouring the web trying to figure this out myself. Did you ever get a solid answer to this? It seems like it should be such a basic need

  7. Hernan says

    Hi.
    Do you know how to put it so that it counts the posts of each ”slug” of the custom taxonomy in the shortcode?

  8. Muhammad Mobeen says

    Hi WPbeginner,

    Thanks for the code. It works. BUT I’m using it to output the taxonomies into the posts and list structure doesn’t display well (goes to the top of the post).

    Please guide me how to output the taxonomies in a span instead of a list.

    • Deb S says

      I had the same problem and had to modify the code somewhat.

      add ’echo’=>false to your args

      $args = array(
      taxonomy => $custom_taxonomy,
      title_li => ”,
      ’echo’ => false,
      );

      and then ”return” the value instead of echoing it.

      $widgettext = ” . wp_list_categories($args) . ”;
      return $widgettext;

      and then the list displays inside the widget box.

  9. deuts says

    What if I want to display a ”linked” top 10 used terms of my custom taxonomy in a period of time (e.g. the last 30 or 90 days)?

  10. Julie says

    Finally! An article that works. I have tried all different approaches, but yours is simple and functions. I styled the list so it mimicked the look of the theme sidebar links. Thanks for the article!!

  11. Nitin says

    Thanks for tutorial. My custom taxonomies are displaying. But the problom is my list is not in proper hierarchy. Child list is not displaying as a child. All data is displaying in a single unordered list. Second thing i want to give class to my parent list like and child also. How it can be possible.

  12. Aniket says

    Hi,
    i would like to add selected taxonomies on my sidebar. I have installed the plugin and it works fine. But i would like to know/learn, is there a way to add selected taxonomies using the code? Like there could be a exclude or include feature where in i could manually add the id’s of the taxonomy that i would want to display or not. Thanks.

  13. jas says

    very helpful post but i am facing one problem with this,
    i am using Custom Taxonomies Menu Widget and able to show a custom taxonomy in sidebar but when i click on the category link of custom taxonomy than page shows that No posts found.

    is there any problem in archive page????

    please help.

Lämna ett svar

Tack för att du väljer att lämna en kommentar. Tänk på att alla kommentarer modereras enligt våra policy för kommentarer, och din e-postadress kommer INTE att publiceras. Vänligen använd INTE nyckelord i namnfältet. Låt oss ha en personlig och meningsfull konversation.