Vill du skapa customize taxonomies i WordPress?
Som standard tillåter WordPress dig att organisera ditt content med kategorier och taggar. Men med customize taxonomies kan du ytterligare anpassa hur du sorterar ditt content.
I den här artikeln visar vi dig hur du enkelt skapar customize taxonomier i WordPress med eller utan att använda ett plugin.
Vad är en taxonomi i WordPress?
En taxonomi i WordPress är ett sätt att organisera grupper av inlägg och custom post types.
Som standard levereras WordPress med två taxonomier som anropas kategorier och taggar. Du kan använda dem för att organisera dina blogginlägg.
Men om du använder en custom post type kanske kategorier och taggar inte ser lämpliga ut för allt content.
You kan till exempel skapa en custom post type som heter ”Books” och sortera den med hjälp av en custom taxonomi som heter ”topics”.
Du kan add to ämnestermer som äventyr, romantik, skräck och andra ämnen du vill ha. Detta skulle allow you, och your läsare att enkelt sortera och filtrera böcker efter varje ämne.
Taxonomier kan också vara hierarkiska, vilket innebär att du kan ha huvudämnen, eller parent, ämnen som Fiction och Nonfiction. Sedan har du underämnen, eller children, under varje kategori.
Till exempel kan parent category Fiction ha Adventure, Romance och Horror som children.
Nu när du vet vad en custom taxonomi är, ska vi lära oss hur man skapar custom taxonomies i WordPress.
Att skapa customize taxonomies är kraftfullt, men det finns mycket att covera. För att hjälpa you att sätta upp detta på rätt sätt har vi skapat en enkel tabell med content under:
Skapa Custom Taxonomies i WordPress (Video Tutorial)
Om du föredrar skriftliga instruktioner, fortsätt då att Läsa.
Skapa customize taxonomier med ett plugin (det enkla sättet)
Det första du behöver göra är att installera och aktivera UI-pluginet Custom Post Type. För detaljer, se vår guide om hur du installerar ett plugin för WordPress.
I den här tutorialen har vi redan skapat en custom post type och antagit namnet ”Books”. Så se till att du har skapat en custom post type innan du börjar skapa dina taxonomier.
Gå sedan till CPT UI ” Add/Edit Taxonomies menu item i WordPress admin area för att skapa din första taxonomi.
På den här vyn måste du göra följande:
- Skapa din taxonomi slug (detta kommer att gå i din URL)
- Skapa pluraletiketten
- Skapa den singulära etiketten
- Automatisk ifyllning av etiketter
Your första steg är att skapa en slug för taxonomin. Denna slug används i URL:en och i WordPress sökningar.
Detta kan endast innehålla bokstäver och siffror och kommer automatiskt att konverteras till gemener.
Därefter fyller du i plural- och singularnamnen för din customize taxonomi.
Därifrån har du alternativet att klicka på länken ”Populate additional labels based on chosen labels. Om du gör detta kommer pluginet automatiskt att fylla i resten av etikettfälten åt dig.
Nu rullar du ner till section ”Additional Labels”. I detta area kan du ge en description av din post type.
Dessa etiketter används i din WordPress dashboard när du redigerar och hanterar content för viss custom taxonomi.
Härnäst har vi alternativet Settings. I det här area kan du ställa in olika attribut för varje taxonomi som du skapar. Varje alternativ har en description som ger detaljer om vad det gör.
I screenshot ovan ser du att vi valde att göra denna taxonomi hierarkisk. Detta innebär att våra taxonomi ”Ämnen” kan ha underämnen. I instance, a subject called Fiction can have sub-topics like Fantasy, Thriller, Mystery, and more.
Det finns många andra Settings längre ner på vyn i din WordPress dashboard, men du kan låta dem vara som de är i den här tutorialen.
You can now click on the ”Add Taxonomy” button at the bottom to save your custom taxonomi.
Efter det kan du gå vidare och edit den post type som är associerad med denna taxonomi i WordPress content type för att börja använda den.
Skapa Custom Taxonomies manuellt (med kod)
Den här metoden är obligatorisk för att du ska kunna add to kod på din website i WordPress. Om du ej har gjort det tidigare rekommenderar vi att du läser vår guide om hur du enkelt lägger till code snippets i WordPress.
Vi rekommenderar inte att du direkt editerar dina WordPress-filer eftersom minsta lilla misstag kan förstöra hela din site. Det är därför vi rekommenderar att alla använder WPCode, det enklaste och säkraste code snippet plugin available.
För att börja måste du installera och aktivera det gratis pluginet WPCode. För detaljerade instruktioner, se vår Step-by-Step guide om hur du installerar ett WordPress plugin.
1. Skapa en hierarkisk taxonomi
Låt oss börja med en hierarkisk taxonomi som gillar kategorier och kan ha parent- och child-termer.
När du har installerat och aktiverat WPCode kan du navigera till Code Snippets ” Add Snippet i din WordPress dashpanel.
Håll muspekaren över ”Add Your Custom Code (New Snippet)” och click ”Use Snippet”.
Därefter kommer du att tas till sidan ”Create Custom Snippet”. Namnge helt enkelt ditt nya code snippet och klistra in följande kod i textområdet.
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_subjects_hierarchical_taxonomy', 0 );
//create a custom taxonomy name it subjects for your posts
function create_subjects_hierarchical_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
$labels = array(
'name' => _x( 'Subjects', 'taxonomy general name' ),
'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
'search_items' => __( 'Search Subjects' ),
'all_items' => __( 'All Subjects' ),
'parent_item' => __( 'Parent Subject' ),
'parent_item_colon' => __( 'Parent Subject:' ),
'edit_item' => __( 'Edit Subject' ),
'update_item' => __( 'Update Subject' ),
'add_new_item' => __( 'Add New Subject' ),
'new_item_name' => __( 'New Subject Name' ),
'menu_name' => __( 'Subjects' ),
);
// Now register the taxonomy
register_taxonomy('subjects',array('books'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'subject' ),
));
}
Var noga med att ändra Code Type till ”PHP Snippet” och toggle omkopplaren till ”Active”.
Glöm inte att ersätta taxonominamnet och etiketterna i snippet med dina egna taxonomietiketter. You will also notice that this taxonomi is associated with the Books post type, you’ll need to change that to whatever post type you want to use it with.
Därefter rullar du ner och ser till att ”Auto Insert” och ”Run Everywhere” är valda i boxen Insertion.
När det är gjort kan du rulla tillbaka högst upp och klicka på knappen ”Update” för att få dina ändringar att gälla.
2. Skapa en icke-hierarkisk taxonomi
För att skapa en icke-hierarkisk customizer-taxonomi som gillar Tags, använder du WPCode och följer exakt samma steg som ovan, men du använder den här koden istället:
//hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires
add_action( 'init', 'create_topics_nonhierarchical_taxonomy', 0 );
function create_topics_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' => _x( 'Topics', 'taxonomy general name' ),
'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
'search_items' => __( 'Search Topics' ),
'popular_items' => __( 'Popular Topics' ),
'all_items' => __( 'All Topics' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Topic' ),
'update_item' => __( 'Update Topic' ),
'add_new_item' => __( 'Add New Topic' ),
'new_item_name' => __( 'New Topic Name' ),
'separate_items_with_commas' => __( 'Separate topics with commas' ),
'add_or_remove_items' => __( 'Add or remove topics' ),
'choose_from_most_used' => __( 'Choose from the most used topics' ),
'menu_name' => __( 'Topics' ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('topics','books',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'topic' ),
));
}
Notice the difference between the 2 code snippets. Under register_taxonomy()
-funktionen sätts värdet för det hierarkiska
argumentet till true
för kategoriliknande taxonomier och false
för tagg/tagged-liknande taxonomier.
I arrayen labels för icke-hierarkiska taxonomier har vi också add to null
för argumenten parent_item
och parent_item_colon
, vilket innebär att inget kommer att visas i användargränssnittet när du skapar ett parent objekt eller en taxonomi som kan ha underämnen.
Återigen, var noga med att edit koden för att inkludera dina egna custom taxonomi etiketter.
Displaying Custom Taxonomies (visa custom taxonomier)
Nu när vi har skapat custom taxonomies och har add to några termer kommer your WordPress theme fortfarande inte att visa dem.
För att kunna displayed dem måste du add to lite kod i your WordPress theme eller barntema.
Denna kod måste addas till templates-filer där du vill visa termerna.
Du kan manuellt add detta snippet till dina theme-filer, till exempel single.php, content.php, archive.php eller index.php. För att ta reda på vilken fil du behöver editera, se vår guide till WordPress mall-hierarki för detaljer.
Det kan dock förstöra din site om det inte görs korrekt, så vi rekommenderar återigen att du använder det gratis tillägget WPCode.
Du måste add to följande kod där du vill visa termerna.
<?php the_terms( $post->ID, 'topics', 'Topics: ', ', ', ' ' ); ?>
Följ stegen ovan för att klistra in snippet i WPCode.
Men under Insertion vill du klicka på dropdown bredvid ”Location” och välja var du vill visa taxonomin, till exempel före posten, efter den eller till och med mellan stycken.
För denna tutorial kommer vi att select ”Insert After Post”.
Du kan se på bilden under hur det kommer att se ut på din site.
Lägga till taxonomier för Custom Posts
Nu när du vet hur man skapar customize taxonomies, låt oss använda dem med ett exempel.
Vi ska skapa en taxonomi och anropa den ”Facklitteratur”.
Eftersom vi har en custom post type som heter ’Books’, liknar det hur du skulle skapa ett vanligt blogginlägg.
I din WordPress dashboard, gå till Books ” Subjects för att add to en term eller ett ämne.
På den här vyn ser du 4 area:
- From Name
- Slug
- Förälder
- Beskrivning
I namnet skriver du ut den term som du vill add to. Du kan hoppa över slug-delen och ge en description för den här vissa termen (valfritt).
Slutligen klickar du på knappen ”Add New Subject” för att skapa din nya taxonomi.
Din nya term som har lagts till kommer nu att visas i den högra columnen.
Nu har du en new term som du kan använda i dina blogginlägg.
Du kan också lägga till termer direkt när du redigerar eller skriver content under viss post type.
Gå bara till Books ” Add New page för att skapa ett post. I postens editor hittar du alternativet att välja eller skapa nya termer från den högra colonnen.
När du har lagt till termer kan du gå vidare och publicera det innehållet.
Alla dina posts som arkiveras under den termen kommer att vara tillgängliga på din website på sin egen URL. For instance, posts filed under the Fiction subject would appear at the following URL:
https://example.com/subject/fiction/
Lägga till Custom Taxonomies i navigationsmenyn
Nu när du har skapat customize taxonomies kanske du vill visa dem i navigation menu på din website.
Gå till Customize ” Menus och välj de termer du vill add to under din custom taxonomi tabs som visas på vänster sida av vyn.
Glöm inte att klicka på knappen ”Save Menu” för att spara dina Settings.
You kan nu besöka din website för att se din menu in action.
Mer detaljer finns i vår Step-by-Step guide om hur du skapar en dropdown-meny i WordPress.
Ta taxonomier i WordPress vidare
Det finns massor av saker du kan göra med customize taxonomies. You kan till exempel visa dem i en widget i sidebar eller add to images icons för varje term.
Du kan också add to aktivera RSS-flöde för customize taxonomier i WordPress och aktivera användare att prenumerera på enskilda termer. På så sätt får dina läsare bara updates om det specifika content som är viktigt för dem.
Om du vill customize layouten på dina customiserade taxonomies pages, kan du kontrollera SeedProd. Det är en drag and drop WordPress page bu ilder och theme builder som allow you att skapa custom layouts utan någon kodning.
Vi hoppas att den här artikeln hjälpte dig att lära dig hur du skapar customize taxonomies i WordPress. Du kanske också vill se vår guide om hur man trackar website visitors, och hur man skapar ett custom WordPress theme utan att skriva någon kod.
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.
Syed Balkhi
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!
Jiří Vaněk
Thank you for the clear instructions. I would like to make my own taxonomy for tutorials on the site. This tutorial is great and clear, thanks.
WPBeginner Support
Glad it was helpful
Administratör
Kira
Hello.
I don’t understand this part:
ID, ’topics’, ’Topics: ’, ’, ’, ’ ’ ); ?>
I mean, should I paste exactly that part into my loop-single.php?
I’m using a taxonomy called ”writer”
WPBeginner Support
It would depend on where you want to display the terms, normally it would be added to the single.php.
Administratör
Wolfgang
Hi,
I used your article to create a CPT with a hierarchical and a non-hierarchical taxonomy attached to it. I created a few entries and it all seems to be working fine. Both taxonomies display fine in their respective archive pages. One thing I noticed though when I tried to add the taxonomies to the menu was that the non-hierarchical taxonomy was available in the ”Add menu item” column but the hierarchical taxonomy was NOT.
Is that an expected behavior? If not what could be going wrong?
Thanks
WPBeginner Support
The most common issues you could check would be that you have published content in the taxonomy and you may want to test adding a different content type to test if it is an issue with the taxonomy or something else.
Administratör
mb
what does this line do
’menu_name’ => __( ’Subjects’ ),
is this in order to add taxonomies to the menu
aditya
hii i try everything but i taxonomy not showing in product. please help me.
WPBeginner Support
You would want to check under your preferences to ensure it is not hidden for your user.
Administratör
Gina Wilson
This tutorial and specifically the part of how to display the custom taxonomy was a lifesaver! I’m very much a beginner and this was very helpful in understanding where I went wrong in my coding.
Thank you!!!
WPBeginner Support
Glad our guide was helpful
Administratör
fengquanli
this is very confident with the custom post ui, thanks very much ,it’s very useful for get them.
WPBeginner Support
Glad our guide was helpful
Administratör
Bruno Oliveira
Awesome tutorial! i have one question. how do i use my new taxonomy slug as permalink like category (/%category%/%year%/%monthnum%/%day%/%postname%/)
I want something like /%custom_taxonomy%/%year%/%monthnum%/%day%/%postname%/
i keep getting 404 error
WPBeginner Support
That is not something WordPress would allow by default and would require some custom coding to set up.
Administratör
vikas
i tried the plugin method , it sucessfully created a new category in custom post type but it is not showing on my posts like tags and other categoties. can you help me with that?
WPBeginner Support
You would want to reach out to the support for your specific theme for customizing the display to include your taxonomy.
Administratör
Richard
I am creating a podcast network where I have multiple podcasts on a single site with episodes under each individual podcast. Would something like this work for that? I really don’t want to go the multi site route.
WPBeginner Support
You can certainly use this should you want or another option would be to create a custom post type depending on your preference.
Administratör
Maria
Hello is possible add the custom taxonomies to a custom product type?
I create a custom product call drinks and i have several taxonomies like country, material and etc
I want when the user click in drinks then only apperas those taxonomies, is this posiible?
WPBeginner Support
You should be able to using the plugin method.
Administratör
Parveen Kaushik
Hi,
Thanks for this article, I am getting 404 page after using this code, can you help me
WPBeginner Support
If you haven’t done so yet, resave your permalinks for the most common solution
Administratör
Mike Smith
this code works great on my site for work. Can you tell me how to add the custom taxonomy into the site’s rss feed?
WPBeginner Support
It would depend on what you are looking for, for a starting point you would want to take a look at our article below:
https://www.wpbeginner.com/wp-tutorials/how-to-make-a-separate-rss-feed-for-each-custom-post-type-in-wordpress/
Administratör
angela
thank you for taking the time to post this, this was the first site that actually explained this and it made sense, haha. im a happy camper now
WPBeginner Support
You’re welcome, glad our guide could help simplify the process
Administratör
Jem
How to show custom taxonomy (checkbox list) in Post editor page like category and tag?
WPBeginner Support
It would depend on which method you used to create the custom taxonomy, if you’re using the plugin you would want to reach out to the plugin’s support to make sure they have show_in_rest set to true for seeing it in the block editor.
Administratör
Jem
No, I am not using any plugin. I have just copy paste your snippet. Its create new taxonomy. But it is not display in post editor page like category, tags display on rightside panel.
Can you please guide me how can I show custom taxonomy in post editor page?
WPBeginner Support
In the register_taxonomy array, you would want to first try adding a new line with the code below:
’show_in_rest’ => true,
Jim Gersetich
The first half of this post is completely useless. the Simple Taxonomy plugin doesn’t work with the current WordPress version, and it hasn’t been updated in four years.
Please try to find another plugin and change that section to go with the new one.
WPBeginner Support
Thank you for letting us know, we’ll certainly take a look at updating this article.
Administratör