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 skapar ett separat RSS-flöde för varje custom post type i WordPress

Utöver sidor och inlägg kan du med WordPress skapa anpassade inläggstyper efter de unika behoven hos ditt innehåll. Du kanske t.ex. vill skapa en anpassad inläggstyp för filmrecensioner, produkter eller vittnesmål. Detta kan hjälpa dig att organisera din webbplats på ett mer effektivt sätt och förbättra användarupplevelsen.

Om du vill ta det ett steg längre kan du också skapa ett RSS-flöde för var och en av dina anpassade inläggstyper. Genom att erbjuda specialiserade flöden till dina webbplatsbesökare kan de engagera sig i ditt material på ett mer anpassat sätt.

I den här guiden går vi igenom processen för att konfigurera separata RSS-flöden för anpassade inläggstyper i WordPress. Låt oss komma igång!

Making RSS feed for custom post types in WordPress

Skapa Separata RSS-flöden för Custom Post Types i WordPress

Som standard genererar WordPress flera RSS-flöden för din website.

Till exempel visas alla dina senaste blogginlägg i din sites huvudsakliga RSS-flöde. Detta feed kan nås genom att lägga till /feed/ till ditt domain name gillar detta:

https://example.com/feed/

Vad de flesta nybörjare inte vet är att WordPress genererar separata RSS-flöden för varje arkivsida.

Till exempel har den separata RSS-flöden för kategorier, taggar, författare och custom post types.

Låt oss säga att du har en custom post type som heter movies på din website. Du kan view all content som skapats i den posttypen genom att besöka posttypens archive page:

https://example.com/movies

Example of a custom post type archive page

För att visa RSS-flödet behöver du bara add to /feed/ nästa inlägg i custom post type archive URL.

https://example.com/movies/feed/

Feed for custom post type

Alternativt kan du viewa flödet genom att lägga till parametern post type i ditt huvudsakliga RSS-flöde i WordPress. Till exempel

https://example.com/feed/?post_type=movies

Denna URL kommer då endast att hämta den custom post type som heter movies.

Alternate custom post type RSS feed URL

Vi använder anpassade inläggstyper för våra sektioner Ordlista och WordPress-hosting. De har båda RSS-flöden aktiverade och kan användas av användare som vill prenumerera på dessa specifika avsnitt.

Add a Link to The Custom Post Type RSS-flöde

Nu när du vet hur du kommer åt RSS-flödena för alla custom post types på din WordPress-webbplats kan du använda den URL:en för att skapa länkar till dina custom post type feeds.

Till exempel kanske du vill visa en icon eller en länk med plain text på custom post type archive page så att dina customers enkelt kan prenumerera på dessa posts.

1. Lägg till en länk till RSS-flödet för Custom Post Type i Block Themes

Om du använder ett blocktema med fullt stöd för webbplatsredigering gör du så här för att lägga till länken.

Först måste du add a custom code snippet till din WordPress website med hjälp av WPCode plugin.

Obs: Det finns också en gratisversion av WPCode, som du kan använda för att lägga till detta kodavsnitt.

Först måste du installera och aktivera WPCode plugin och sedan gå till Code Snippets ” + Add New Sn ippet page.

Add new snippet

Härifrån klickar du på knappen ”Use Snippet” under alternativet ”Add Your Custom Code (New Snippet)”.

På nästa vy ska du ange ett namn för code snippet. Detta kan vara vad som helst som hjälper dig att identifiera snippet. Efter det väljer du ”PHP Snippet” under Code Type.

Följ upp med att add to följande kod i Code Preview boxen:

    if (is_post_type_archive('movies')) {
            $post_type = get_queried_object();
            // Get RSS Feed URL
            $rss_feed_url = get_post_type_archive_feed_link($post_type->name);
            // Output the shortcode content
            return '<p>Subscribe to <a href="' . esc_url($rss_feed_url) . '">' . $post_type->label . '</a></p>';
        }
    }
// Register the shortcode
add_shortcode('custom_post_type_rss_link', 'custom_post_type_rss_link_shortcode');

Glöm inte att ersätta ”movies” med namnet på din anpassade inläggstyp.

Så här skulle det se ut som ett snippet i WPCode plugin:

Adding custom code for RSS feed link

Aktivera nu ”Active”-knappen och ”Save/Update” ditt snippet.

Den här koden upptäcker automatiskt om en användare visar den specifika custom post type archive page och visar sedan en länk för att prenumerera.

Det skapar sedan en shortcode som du kan använda i ditt block theme eller widgets för att visa länken.

Därefter måste du besöka sidan Appearance ” Editor för att starta Site Editor. Därefter väljer du alternativet Templates ” Archive från den vänstra menyn.

Edit Archive template in Site Editor

Lägg sedan till Shortcode-blocket där du vill visa länken till RSS-flödet.

Helst vill du visa det högst upp, precis under arkivnamnet.

Add shortcode block

I blocket Shortcode måste du add to följande shortcode:

[custom_post_type_rss_link]

Glöm inte att save dina ändringar efteråt.

Du kan nu besöka din custom post type archive page för att se RSS-flöde länken i action.

RSS feed link preview

2. Lägg till en länk till RSS-flödet för Custom Post Type i Classic Themes

Det enklaste sättet att göra detta är att skapa en separat template för din custom post type i ditt WordPress theme.

Anslut till din website i WordPress med en FTP-klient och navigera till mappen /wp-content/themes/your-current-theme/.

Nu, om din custom post type är anropad movies, då kan du skapa en archive-{post_type}.php-fil i din WordPress theme folder.

Efter det kan du helt enkelt kopiera innehållet från temats archive.php template och börja customize din new template.

Du kan helt enkelt add to en vanlig HTML-länk till din post type archive feed med hjälp av följande kod:

<p><strong>Subscribe to: <a href="https://example.com/movies/feed/">Movies</a></strong></p>

Glöm inte att ändra URL:en till URL:en för din post type feed.

Nu är problemet med den här koden att du måste skapa en new template-fil bara för den viss post typen.

Med nästa metod kan du dynamiskt generera länken till RSS-flödet för post-typen för alla dina archives pages.

Lägg bara till följande kod i temats archive.php template-fil.

<?php if ( is_post_type_archive() ) { 
$post_type = get_post_type( get_queried_object_id() );?> 
				<p><strong>Subscribe to: <a href="<?php echo get_post_type_archive_link( $post_type  ); ?>feed/"><?php post_type_archive_title(); ?></a></strong></p>
<?php } ?>		

Denna kod kommer helt enkelt att add to en länk under post typens archive page titel, uppmuntra användare att prenumerera på denna viss content typ.

Bonus Tips: Add Custom Post Type till ditt huvudsakliga RSS-flöde

Custom post type RSS-flöden är inte lätt att upptäcka av flödesläsare, och de flesta av dina kunder kan hitta din webbplats RSS-flöde lättare.

Detta innebär att användare som prenumererar på ditt huvudsakliga RSS-flöde kommer att missa innehållet du publicerar i din custom post type.

Du kan enkelt fixa detta genom att lägga till content från din custom post type så att det visas i webbplatsens huvudsakliga RSS-flöde.

För att göra det måste du lägga till ett custom code snippet till din WordPress blogg. Vi rekommenderar att du använder WPCode för att add to custom code snippets i WordPress.

Först måste du installera och aktivera det gratis pluginet WPCode. För mer detaljer, se vår artikel om hur du installerar ett plugin för WordPress.

När pluginet har aktiverats besöker du Code Snippets ” + Add Snippet page från WordPress admin sidebar.

Härifrån måste du klicka på knappen ”Use Snippet” under alternativet ”Add Your Custom Code (New Snippet)”.

Add new snippet

Du kommer nu till sidan ”Create Custom Snippet”, där du kan börja med att skriva in ett namn på ditt code snippet.

Detta namn kommer inte att displayed någonstans och används bara för identifieringsändamål.

Välj sedan alternativet ”PHP Snippet” från dropdown-menyn ”Code Type” till höger.

Snippet title and code type

Efter det är du redo att lägga till ditt custom code snippet.

Det är bara att copy and paste in följande kod i boxen Code Preview:

function myfeed_request($qv) {
    if (isset($qv['feed']) && !isset($qv['post_type']))
        $qv['post_type'] = array('post', 'movies', 'books');
    return $qv;
}
add_filter('request', 'myfeed_request');

När du har lagt till koden skriver du namnet på den custom post type nästa inlägg där ’[’post_type’]’ skrivs i koden. I vårt exempel har vi ”post”, ”movies” och ”books”.

Denna custom post type kommer att addas till ditt huvudsakliga WordPress RSS-flöde.

Add code snippet

Därefter måste du rulla tillbaka högst upp på sidan och toggle omkopplaren ”Inaktiverad” till ”Aktiv”.

Glöm inte att klicka på knappen ”Save Snippet” för att spara och köra koden på din WordPress website.

Save snippet

Det är all, din custom post type content kommer nu att läggas till i webbplatsens huvudsakliga RSS-flöde.

Vi hoppas att den här artikeln hjälpte dig att lära dig hur du skapar ett separat RSS-flöde för custom post types i WordPress. You may also want to see our tutorial on optimizing your WordPress RSS feeds and our expert picks for the best WordPress RSS plugins.

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

30 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. Buddika Wijerathena says

    Is there way to create RSS feed for single page ?
    Ex – example.com/single-post-type/single-post-name

    How to create RSS for this ?

    • WPBeginner Support says

      RSS feeds are for listing multiple posts, the RSS would include a link to the post itself. We would not recommend creating a specific RSS feed for one post or page.

      Administratör

  3. Milan says

    Hey, and do you know any rss wordpress plugin for visitors ? As big new companies have own RSS creator to put rss on some freelancer sites, I want to it same, but not with post but with custom post types….do you know some RSS plugin ?

  4. Tori says

    It still doesn’t work for me – I’m trying to display an RSS Feed for the content on this page –

    and displays the RSS feed for all of my posts. Any thoughts?

      • Tori says

        Thank you for the response. I’m sorry for the confusion as it looks like my links didn’t appear. The posts that appear on that page are custom posts (it’s like the blog roll but for custom posts), but the method to do the RSS feed didn’t work for me. Any thoughts or suggestions?

        • WPBeginner Support says

          Hey Tori,

          If you are certain that the posts belong to a custom post type, then you need to find out the name used by the custom post type. You can do that by clicking on the custom post type menu item in your WordPress admin sidebar. Clicking on it will take you to an admin page listing all the posts in that post type. Now if you look into the browser address bar you will see something like this:

          http://www.example.com/wp-admin/edit.php?post_type=your_post_type

          The part that appears after the post_type= is your custom post type name. Now you need to use this to reach the RSS feed URL for that custom post type.

          http://www.example.com/feed/?post_type=your_post_type

          Hope this helps.

    • Tori says

      Thank you for the response. When I do that, I see this error message — any thoughts?

      ______

      This page contains the following errors:

      error on line 2 at column 1: Extra content at the end of the document
      Below is a rendering of the page up to the first error.

  5. Cédric Charles says

    Hi and thanks for this !

    I would like to add custom fields for my custom post type feed (not for the regular posts, only for my custom post type).

    How could I do that ?

    Thanks a lot !

  6. mike says

    I’ve gotten this to work but it limits the posts in the rss feed to 10 when I feedburn it. Is there any way to create a full rss feed for custom post types without limiting the quantity of posts it pulls in?

  7. Andrew says

    Hi there. Could you tell me how to create a feed for all post types, so someone can signup to a single RSS feed for all posts on the site, regardless of which post type they are in?

  8. Rems says

    THANK YOU VERY MUCH, i was looking for this info for 2 hours. Glad i found your info. Fu…. taxonomy, where were you?!! aahhh!
    Work so fine now

  9. AlexAmelines says

    it works for me and I love you for it. I created a link to each pos type I’ve got, but in the RSS reader they are all called the same, any way I can affect the title of the feed to reflect the post type?

  10. Alan says

    If I am using a slug re-write for the taxonomy that wouldn’t make a difference and be causing the problem would it? I’ve tried both ways and it doesn’t work either way, with the actual registered taxonomy title or the slug.

  11. Aldi says

    I can’t seem to make that work. It only takes me back to the custom post type archive page again.

    Btw, I use Custom Post Permalinks plugin to allow custom post type permalinks and archive pages. Could that have created the issue??

    Cheers!

    • Aldi says

      Oops, sorry.. it was my theme’s problem. I had an action call to redirect anything that is related to custom post types, thus the redirection of the feeds. But got it fixed now.

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.