Come creare feed RSS aggiuntivi con testo completo

13 lug 2011, 16:19:13
Visualizzazioni: 5.26K
Voti: 4

A rischio di sembrare completamente ridicolo, sto pubblicando questa domanda qui poiché la natura della mia richiesta non sembra aiutarmi molto con Google.

I feed RSS predefiniti di WordPress utilizzano l'estratto del post. Questo feed è accessibile tramite www.miodominio.com/feed

Ho la necessità di creare un altro feed RSS che contenga il testo completo. Come posso creare un feed RSS aggiuntivo (che sia privato/fornito solo a parti fidate)? Preferibilmente utilizzando una funzione, ma un plugin come ultima risorsa.

0
Tutte le risposte alla domanda 2
3

Ecco come creare un feed personalizzato:

Per prima cosa crea un nuovo file nella directory del tuo tema, chiamalo your-custom-feed.php e inserisci questo codice all'interno

<?php

     /**
      * Feed RSS personalizzato.
      *
      * @package WordPress
      */

     header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);
     $more = 1;

    echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>

    <rss version="2.0"
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        xmlns:wfw="http://wellformedweb.org/CommentAPI/"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:atom="http://www.w3.org/2005/Atom"
        xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
        xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
        <?php do_action('rss2_ns'); ?>
    >

        <channel>
            <title><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
            <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
            <link><?php bloginfo_rss('url') ?></link>
            <description><?php bloginfo_rss("description") ?></description>
            <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
            <language><?php echo get_option('rss_language'); ?></language>
            <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
            <sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
            <?php do_action('rss2_head'); ?>
            <?php while( have_posts()) : the_post(); ?>
            <item>
                <title><?php the_title_rss() ?></title>
                <link><?php the_permalink_rss() ?></link>
                <comments><?php comments_link_feed(); ?></comments>
                <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
                <dc:creator><?php the_author() ?></dc:creator>
                <?php the_category_rss('rss2') ?>

                <guid isPermaLink="false"><?php the_guid(); ?></guid>
                <description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
            <?php if ( strlen( $post->post_content ) > 0 ) : ?>
                <content:encoded><![CDATA[<?php the_content_feed('rss2') ?>]]></content:encoded>
            <?php else : ?>
                <content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
            <?php endif; ?>

                <wfw:commentRss><?php echo esc_url( get_post_comments_feed_link(null, 'rss2') ); ?></wfw:commentRss>
                <slash:comments><?php echo get_comments_number(); ?></slash:comments>
        <?php rss_enclosure(); ?>
            <?php do_action('rss2_item'); ?>
            </item>
            <?php endwhile; ?>
        </channel>
    </rss>

poi aggiungi una semplice funzione per richiamare quel file template usando do_feed_$hook

//carica il template del feed
function create_my_customfeed() {
    load_template( TEMPLATEPATH . 'your-custom-feed.php'); 
}
add_action('do_feed_mycustomfeed', 'create_my_customfeed', 10, 1);

Ora quando accedi a http://yoursite.com/?feed=mycustomfeed otterrai un feed completo, indipendentemente da ciò che hai definito nell'admin di WordPress.

Bonus

se vuoi creare una regola di riscrittura per il tuo feed personalizzato in modo che l'URL possa essere:

http://yoursite.com/mycustomfeed.xml oppure http://yoursite.com/feed/mycustomfeed/

aggiungi questo codice al tuo file functions.php:

//crea regola di riscrittura per il feed

function custom_feed_rewrite($wp_rewrite) {
    $feed_rules = array('feed/(.+)' => 'index.php?feed=' . $wp_rewrite->preg_index(1),'(.+).xml' => 'index.php?feed='. $wp_rewrite->preg_index(1));
    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
13 lug 2011 18:34:15
Commenti

Grazie per questo brillante tutorial. Quando creo il file (full-feed.php) e lo chiamo mysite.com/?feed=full-feed ottengo il seguente errore

ERROR: full-feed is not a valid feed template.

swordfish81 swordfish81
14 lug 2011 06:42:08

devi cambiare l'hook e il nome del file nel codice

Bainternet Bainternet
14 lug 2011 06:55:42

ahh colpa mia. Sistemato. Ma controllo il feed e mostra ancora solo un paio di righe da ogni post??

swordfish81 swordfish81
14 lug 2011 07:21:57
2

Prova qualcosa del genere (non testato, semplificato da codice più complesso):

add_feed('full', 'full_feed');

function full_feed() {

    // Disabilita gli estratti e mostra il contenuto completo
    add_filter('pre_option_rss_use_excerpt', '__return_zero');
    // Carica il template del feed RSS2
    load_template( ABSPATH . WPINC . '/feed-rss2.php' );
}
13 lug 2011 18:35:54
Commenti

Certamente, presumo che questo codice vada in functions.php ma quale file devo chiamare? C'è già un /feed che va di default con l'excerpt regolare. Quindi c'è bisogno di un altro file o di un nome che deve essere chiamato??

swordfish81 swordfish81
14 lug 2011 07:28:50

@Sledge81 il primo argomento di add_feed() è lo slug del feed che viene creato. Quindi questo feed aggiuntivo sarà disponibile all'indirizzo /feed/full o ?feed=full (il primo dipende se hai i pretty permalink abilitati).

Rarst Rarst
14 lug 2011 08:49:01