将rss feed异步放入动态页面 - wordpress

问题描述 投票:0回答:2

在我的wordpress网站上,我有许多基于不同人的动态页面。我做一个Ajax调用来获取数据,用javascript函数中包含的数据生成所有html,然后将它全部插入到实际页面上的div中。有了这个,我想展示最近的三篇关于页面加载的特定人物的文章。我发现结果告诉我将它添加到functions.php:

//This file is needed to be able to use the wp_rss() function.
include_once(ABSPATH.WPINC.'/rss.php');
function readRss($atts) {
    extract(shortcode_atts(array(
    "feed" => 'http://',
      "num" => '1',
    ), $atts));

    return wp_rss($feed, $num);
}
add_shortcode('rss', 'readRss');

那么我试着把它放在我的html中:

var rsser = '<h2>In the News</h2>' +
            '[rss feed="http://website.com/tag/' + tagname + '/feed/" num="3"]';
$('#rssCon').html(rsser);

然而,这似乎并没有起作用,我担心这可能是因为它是异步发生的。在这种情况下,“tagname”将是我从Ajax调用中获得的一段数据。

所以我正在寻找的是一种异步动态生成rss feed的方法。如果可能的话,如果有人可以指出我的方向很好,或者,如果不是,请告诉我!

添加更多代码:

var getNewsPerson = function() {
    $.ajax({
        url:"http://website/api/v1/api?pid=" + personId,
        type:"get",
        success:function(res) {
            return_tagname(res);
            processPerson(res);
        }
    });
};

function processPerson(data) {
    var returnedFeedShortcode = return_tagname(data);
    var head = 
        '<div class="headForPerson-nt">' +
            '<div class="hfpm-NextPerson-nt">' +
                '<div class="hfpm-header-nt">' + data[0][0].FirstName + '</div>' +
                '<div class="hfpm-ng-one-nt">' + data[0][0].LastName + '</div>' +
            '</div>' +
            '<div class="hfpm-News-nt">' +
                '<div class="hfpm-header-nt">In the News</div>' + returnedFeedShortcode +
            '</div>' +
        '</div>';
    $('#personPageHead-nt').html(head);
}

$(document).ready(function() {
    if($('#personPageHead-nt').length) {
        getNewsPerson(location.search);
    }
});

function return_tagname(data) {
    var tagname = data[0][0].FirstName + '+' + data[0][0].LastName;
    return do_shortcode('[rss feed="http://website/tag/' + tagname + '/feed/" num="3"]');
};
jquery ajax wordpress rss
2个回答
0
投票

通过javascript添加短代码是行不通的。由于PHP已在页面上运行,因此不会对其进行处理。您将需要通过AJAX调用处理短代码并将该HTML返回到javascript函数而不是标记名。

以下是您的javascript通话。你不应该在这里有do_shortcode。这是一个PHP调用,需要在你的functions.php文件中,我将在之后显示。

var getNewsPerson = function() {
    $.ajax({
        url:"http://website/api/v1/api?pid=" + personId,
        type:"get",
        success:function(res) {
            processPerson(res);
        }
    });
};

function processPerson(data) {
    var returnedFeedShortcode = return_tagname(data);
    var head = 
        '<div class="headForPerson-nt">' +
            '<div class="hfpm-NextPerson-nt">' +
                '<div class="hfpm-header-nt">' + data[0][0].FirstName + '</div>' +
                '<div class="hfpm-ng-one-nt">' + data[0][0].LastName + '</div>' +
            '</div>' +
            '<div class="hfpm-News-nt">' +
                '<div class="hfpm-header-nt">In the News</div>' + returnedFeedShortcode +
            '</div>' +
        '</div>';
    $('#personPageHead-nt').html(head);
}

$(document).ready(function() {
    if($('#personPageHead-nt').length) {
        getNewsPerson(location.search);
    }
});

function return_tagname(data) {
    var tagname = data[0][0].FirstName + '+' + data[0][0].LastName;
    var requestData = {
        'action': 'return_tagname',
        'tagname': tagname
    };
    $.ajax({
        url: MyAjax.ajaxurl,
        data: requestData,
        success: function( tagdata ) {
            return tagdata;
    }
})
};

以下是您需要在functions.php文件中添加的内容:

//You need to do this when enqueuing your javascript so that you have access to the Wordpress AJAX URL. The contents of this function can be added to your current function that is enqueuing your styles and scripts.
function my_enqueue_script(){
    // declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
    wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
} 
add_action( 'wp_enqueue_scripts', 'my_enqueue_script' );

//The 'action data ('return_tagname') sent in the ajax call will tell Wordpress to use this function. You need to have it end in _callback.
function return_tagname_callback(){
    //$_POST['tagname'] is coming from the AJAX post made in the return_tagname function on the javascript side. do_shortcode will return the result of the function.
    return do_shortcode('[rss feed="http://website/tag/' + $_POST['tagname'] + '/feed/" num="3"]');
}
add_action( 'wp_ajax_my_action', 'return_tagname_callback' );
add_action( 'wp_ajax_nopriv_my_action', 'return_tagname_callback' );

0
投票

对于RSS Feed,您可以使用它。将其粘贴到您的模板页面上。

    <?php
    $curl = 
    curl_init('http://website.com/tag/');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $page = curl_exec($curl);
    if(curl_errno($curl)) // check for execution errors
    {
    echo 'Scraper error: ' . curl_error($curl);
    exit;
    }
    curl_close($curl);
    $regex = '/<table class="StoryengineTable">(.*?)<\/div>/s';
    if ( preg_match($regex, $page, $list) )
    echo $list[0];
    else 
    print "Not found"; 
    ?>
© www.soinside.com 2019 - 2024. All rights reserved.