內容營銷

WordPress:如何在部落格上發布每個類別的提要

預設情況下,WordPress 部落格有一個包含其所有貼文(無論類別為何)的提要。 改善網站訪客的個人化和細分的一種方法是啟用 RSS 特定於他們感興趣的類別的飼料。 您也可以使用特定於類別的 用於發布電子郵件通訊的 feed。 但是,如果您願意,您可以為 WordPress 部落格或自訂貼文類型建立自訂類別來源。

WordPress 類別提要

您可以將以下程式碼新增至您的 兒童主題的 functions.php 在 WordPress 中產生特定於類別的 RSS 來源的文件,其中包含類別 ID 的包含清單和排除清單:

function custom_category_feeds() {
    $categories = get_categories();

    // Define an array of category IDs to include and exclude
    $included_category_ids = array(3, 4); // Add IDs of categories to include
    $excluded_category_ids = array(1, 2); // Add IDs of categories to exclude

    foreach ($categories as $category) {
        $category_id = $category->term_id;

        // Check if the category should be excluded
        if (in_array($category_id, $excluded_category_ids)) {
            continue; // Skip excluded categories
        }

        // Check if the category should be included
        if (!empty($included_category_ids) && !in_array($category_id, $included_category_ids)) {
            continue; // Skip categories not in the inclusion list
        }

        $category_slug = $category->slug;
        $category_name = $category->name;

        // Start building the RSS feed content
        $rss_feed = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
        $rss_feed .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">' . "\n";
        $rss_feed .= '<channel>' . "\n";
        $rss_feed .= '<title>' . $category_name . ' RSS Feed</title>' . "\n";
        $rss_feed .= '<link>' . get_bloginfo('url') . '</link>' . "\n";
        $rss_feed .= '<description>' . $category_name . ' RSS Feed</description>' . "\n";
        $rss_feed .= '<atom:link href="' . esc_url(site_url("/category/$category_slug/feed/")) . '" rel="self" type="application/rss+xml" />' . "\n";

        // Query posts in the current category
        $args = array(
            'cat' => $category_id,
            'posts_per_page' => 10, // Adjust as needed
        );
        $category_posts = new WP_Query($args);

        while ($category_posts->have_posts()) {
            $category_posts->the_post();
            $rss_feed .= '<item>' . "\n";
            $rss_feed .= '<title>' . get_the_title() . '</title>' . "\n";
            $rss_feed .= '<link>' . get_permalink() . '</link>' . "\n";
            $rss_feed .= '<pubDate>' . get_the_time('D, d M Y H:i:s O') . '</pubDate>' . "\n";
            $rss_feed .= '</item>' . "\n";
        }

        wp_reset_postdata();

        $rss_feed .= '</channel>' . "\n";
        $rss_feed .= '</rss>';

        // Output the feed
        header('Content-Type: application/rss+xml; charset=UTF-8');
        echo $rss_feed;
    }
}

add_action('do_feed_category', 'custom_category_feeds', 10, 1);
add_action('do_feed_category_rss2', 'custom_category_feeds', 10, 1);

這是程式碼的解釋:

  • 函數聲明: 該程式碼定義了一個名為 custom_category_feeds.
  • 類別包含和排除清單:
    • 定義了兩個陣列:
      • $included_category_ids:此陣列會儲存您想要包含在 feed 中的類別 ID。
      • $excluded_category_ids:此陣列會儲存您想要從 feed 中排除的類別 ID。
  • 循環遍歷類別: 代碼使用 get_categories() 檢索所有類別的清單。
  • 排除清單檢查: 對於每個類別,它會檢查類別 ID 是否在 $excluded_category_ids 大批。 如果是,程式碼將繼續到下一個類別(排除它)。
  • 包含清單檢查: 然後它檢查是否應包含該類別。 如果 $included_category_ids 數組不為空,且類別 ID 不在該數組中,代碼繼續執行下一個類別(將其排除在包含範圍之外)。
  • 產生 RSS 提要內容: 該程式碼繼續為透過包含和排除檢查的類別產生 RSS 提要內容。 用於產生 RSS 提要內容的程式碼未顯示,但應該與前面的範例類似。
  • 輸出 Feed: 最後,它為 RSS 提要設定適當的內容類型並回顯 RSS 提要內容。

    此程式碼的主要功能是能夠指定類別 ID 的包含列表和排除列表,從而使您能夠精細控制在生成的特定於類別的 RSS 提要中包含或排除哪些類別。

    您的 WordPress 類別供稿

    使用者可以使用 網址 先前提供的結構用於存取您在 WordPress 中建立的自訂特定類別提要。 存取特定類別提要的 URL 格式如下:

    http://yourwebsite.com/category/{category-name}/feed/

    以下是如何調用 feed 的詳細資訊:

    1. 更換 yourwebsite.com 與您的實際網站網域名稱或 URL。
    2. 代替 {category-name} 替換為您要存取其 Feed 的類別的 slug。 slug 是類別名稱的小寫、連字號分隔版本。 例如,如果您的類別名稱是 營銷技巧,蛞蝓可能是 行銷技巧.
    3. 加入 /feed/ 到 URL 的末尾。 這表示您想要存取特定類別的 RSS 或 Atom 提要。

    例如,如果您的網站是“example.com”,並且您想要存取“行銷技巧”類別的 Feed,則 URL 將為:

    http://example.com/category/marketing-tips/feed/

    使用者可以在 Web 瀏覽器中輸入此 URL 或使用提要閱讀器應用程式來訂閱特定於類別的提要。 此 URL 將為他們提供所選類別的 RSS 或 Atom 提要,使他們能夠輕鬆了解該類別中內容的最新動態。

    Douglas Karr

    Douglas Karr 是 CMO 的 開放洞察 和創始人 Martech Zone。 道格拉斯幫助了數十家成功的 MarTech 新創公司,協助進行了超過 5 億美元的 MarTech 收購和投資盡職調查,並繼續協助公司實施和自動化其銷售和行銷策略。 道格拉斯是國際公認的數位轉型和 MarTech 專家和演講者。 道格拉斯也是一本傻瓜指南和一本商業領導書的出版作者。

    相關文章

    返回頂部按鈕
    關閉

    檢測到Adblock

    Martech Zone 我們能夠免費為您提供這些內容,因為我們通過廣告收入、聯屬鏈接和讚助從我們的網站中獲利。 如果您在瀏覽我們的網站時刪除廣告攔截器,我們將不勝感激。