text Wordpress - 创建自定义RSS源 - 显示RSS源 - 设置RSS缓存到期/缓存生命周期

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text Wordpress - 创建自定义RSS源 - 显示RSS源 - 设置RSS缓存到期/缓存生命周期相关的知识,希望对你有一定的参考价值。

/*----------------------------------------------*/
/*   Feed Cache Refresh Rate
/*----------------------------------------------*/
// http://www.village-idiot.org/archives/2009/08/04/fix-the-rss-update-time-on-the-wordpress-rss-widget/

// By default Wordpress checks for updates in RSS feeds only once every 12 hours.
// This code will cause the feeds to be checked every 30 minutes. Add to functions.php
// 1800 seconds = 30 min.

add_filter( 'wp_feed_cache_transient_lifetime', create_function('$a', 'return 1800;') );




/*----------------------------------------------*/
/*   Create A Custom Feed
/*----------------------------------------------*/
// http://www.wpbeginner.com/wp-tutorials/how-to-create-custom-rss-feeds-in-wordpress/

// 1. Declare the new feed in functions.php + indicate which template file to use

add_action('init', 'customRSS');
function customRSS(){
        add_feed('feedname', 'customRSSFunc');
}

function customRSSFunc(){
        get_template_part('rss', 'feedname');
}


// 2. Create template file 'rss-feedname.php'

<?php
/**
 * Template Name: Custom RSS Template - Feedname
 */
$postCount = 5; // The number of posts to show in the feed
// $posts = query_posts('showposts=' . $postCount);
$my_query = new WP_query( 
                    array(
                      'posts_per_page'   =>    -1,
                      'meta_query'       => array(
                          array(
                              'key' => 'featured_on_bunt',
                              'value' => '"in_cat"', 
                              'compare' => 'LIKE'
                            )
                        )
                      )
                    );


header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
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'); ?> - Feed</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 if ($my_query->have_posts()): ?>
          <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <item>
              <title><?php the_title_rss(); ?></title>
              <link><?php the_permalink_rss(); ?></link>
              <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>
              <guid isPermaLink="false"><?php the_guid(); ?></guid>
              <description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
              <content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
              <?php rss_enclosure(); ?>
              <?php do_action('rss2_item'); ?>
            </item>
          <?php endwhile; ?>
        <?php endif; ?>
</channel>
</rss>




/*----------------------------------------------*/
/*   Display a Feed in Wordpress
/*----------------------------------------------*/
// http://www.wpbeginner.com/wp-tutorials/how-to-display-any-rss-feed-on-your-wordpress-blog/

<h2><?php _e( 'Recent news from Some-Other Blog:', 'my-text-domain' ); ?></h2>

<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'http://www.wpbeginner.com/feed/' );

if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly

    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity( 5 ); 

    // Build an array of all the items, starting with element 0 (first element).
    $rss_items = $rss->get_items( 0, $maxitems );

endif;
?>

<ul>
    <?php if ( $maxitems == 0 ) : ?>
        <li><?php _e( 'No items', 'my-text-domain' ); ?></li>
    <?php else : ?>
        <?php // Loop through each feed item and display each item as a hyperlink. ?>
        <?php foreach ( $rss_items as $item ) : ?>
            <li>
                <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                    title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
                    <?php echo esc_html( $item->get_title() ); ?>
                </a>
            </li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>

以上是关于text Wordpress - 创建自定义RSS源 - 显示RSS源 - 设置RSS缓存到期/缓存生命周期的主要内容,如果未能解决你的问题,请参考以下文章

text “智能自定义字段(SCF)”wordpress插件

text 在WordPress中创建自定义搜索表单

Wordpress自定义管理页脚

text [自定义字段分类法类别] campos personalizados para categorias #wordpress #custom

WordPress 3自定义帖子类型:如何获得评论

text WordPress函数将TinyMCE WYSIWYG编辑器添加到“Textarea”类型的任何自定义字段