2016-09-07 53 views
2

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

//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"]'); 
}; 

回答

2

通过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

我不确定您的意思是“通过AJAX调用处理短代码”。我创建了return_tagname()并把它返回到AJAX调用生成的html中,但是我在函数的return语句中得到了“wp_rss未定义”。我已经尝试使用“do_shortcode”和“add_shortcode”,但我添加到functions.php的代码似乎说使用“wp_rss”,所以我去了。有任何想法吗? –

+0

由于您使用的是AJAX,因此您必须在functions.php中运行do_shortcode并返回值。您使用的文档可能不是使用AJAX。发布AJAX调用的整个JavaScript部分到您的问题,我会确保这是正确的。 – Fencer04

+0

已添加。没有尝试添加rss feed所有其他部分的工作,所以我不认为我在其他地方有一些错误,它搞砸了,但它总是可能的。 –

1

对于RSS订阅您可以使用此。将其粘贴到您的模板页面上。

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