add_feed()

最后更新于:2021-11-25 19:11:20

add_feed( string$feedname, callable$function)

Add a new feed type like /atom1/.

参数

$feedname

(string) (Required) Feed name.

$function

(callable) (Required) Callback to run on feed display.

响应

(string) Feed action name.

源文件

文件: gc-includes/rewrite.php

function add_feed( $feedname, $function ) {
	global $gc_rewrite;

	if ( ! in_array( $feedname, $gc_rewrite->feeds, true ) ) {
		$gc_rewrite->feeds[] = $feedname;
	}

	$hook = 'do_feed_' . $feedname;

	// Remove default function hook.
	remove_action( $hook, $hook );

	add_action( $hook, $function, 10, 2 );

	return $hook;
}
function add_custom_feed() {
	add_feed( 'custom', 'render_custom_feed' );
}
add_action( 'init', 'add_custom_feed' );


function custom_feed_content_type( $content_type, $type ) {
	if( 'custom' == $type ) {
		$content_type = 'application/rss+xml';
	}
	return $content_type;
}
add_filter( 'feed_content_type', 'custom_feed_content_type', 10, 2 );

function render_custom_feed() {
	echo 'aye!';
}
add_action( 'init', 'gcdocs_custom_feed_rss2' );
function gcdocs_custom_feed_rss2() {

    // Create your feed name like this : https://yoursite.com/gcdocs_custom?tag=test
    add_feed( 'gcdocs_custom', 'gcdocs_change_main_query' );
    function gcdocs_change_main_query() {

        // Set right headers for RSS Feed
        header( 'Content-Type: application/rss+xml' );

        // Get main GC Query
        global $gc_query;

        // Get parameters in url
        if ( ! empty( $_GET['tag'] ) ) :
            $tag = $_GET['tag'];
        endif;

        // Overwrite main GC Query with yours
        $gc_query = new GC_Query(
            array(
                'post_type' => 'any',
                'fields'    => 'ids',
                'tag'       => $tag,
            )
        );

        // Use the basic template to load your custom RSS Feed
        get_template_part( 'feed', 'rss2' );
    }
}