【问题标题】:Putting a rss feed into a dynamic page asynchronously - wordpress将 rss 提要异步放入动态页面 - wordpress
【发布时间】:2016-09-07 13:16:32
【问题描述】:

在我的 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);

但是,这似乎不起作用,我担心这可能是因为它是异步发生的。在这种情况下,“标记名”将是我从 Ajax 调用中获得的一条数据。

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

添加更多代码:

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


    【解决方案1】:

    通过 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' );
    

    【讨论】:

    • 我不确定您所说的“通过 AJAX 调用处理短代码”是什么意思。我创建了 return_tagname() 并将它返回的内容放入由 AJAX 调用生成的 html 中,但我在函数的返回语句中得到“未定义 wp_rss”。我也尝试过使用“do_shortcode”和“add_shortcode”,但我添加到functions.php的代码似乎说要使用“wp_rss”,所以我就这么做了。有什么想法吗?
    • 由于您使用的是 AJAX,您必须在 functions.php 中运行 do_shortcode 并返回值。您使用的文档可能没有使用 AJAX。将 AJAX 调用的整个 javascript 部分发布到您的问题中,我会确保这是正确的。
    • 已添加。在不尝试添加 rss 提要的情况下,所有其他部分都可以正常工作,所以我不认为我在其他地方有一些错误会弄乱它,但它总是有可能的。
    • 这很有帮助。您将需要另一个 AJAX 调用来完成这项工作。我现在正在旅行,但今晚/晚上会发布一个新的答案,这会有所帮助。
    • 我更新了答案。由于 do_shortcode 是 PHP 而不是 javascript,因此您需要进行第二次 AJAX 调用才能运行该函数。
    【解决方案2】:

    对于 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"; 
        ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多