【发布时间】:2015-01-25 07:38:08
【问题描述】:
我正在开发一个包含多个小部件的插件,每个小部件都填充不同的 XML 文件。我的 XML 文件代表了我的所有类别(我有 7 个类别)并且其中有多个项目。
XML 示例:
<channel>
<category> This XML category </category>
<item>
<title> Item title 1 </title>
<description> This is the first item description. </description>
</item>
<item>
<title> Item title 2 </title>
<description> This is the second item description. </description>
</item>
</channel>
我的插件所做的是获取一个 XML 文件并使用它来填充一个带有其项目的小部件。每个项目都有一个指向它的按钮链接,当我单击它时,它会调用一个 jQuery 函数。
PHP:
foreach ( $channel->channel->item as $item ) {
echo "<div class='xppArticle'>";
echo "<h2>" . $item->title . "</h2>";
if ( preg_match( '/p$/', $this->set_paragraph_length_display( $item->description, 400 ) ) || preg_match( '/\\/$/', $this->set_paragraph_length_display( $item->description, 400 ) ) ) {
echo "<p>". html_entity_decode( $this->set_paragraph_length_display( $item->description, 375 ) ) . "(...)" ."</p>";
}
else {
echo "<p>". html_entity_decode( $this->set_paragraph_length_display( $item->description, 400 ) ) . "(...)" . "</p>";
}
global $wpdb;
$query = $wpdb->prepare ( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_title = %s', $item->title );
$cID = $wpdb->get_var( $query ) ;
if ( ! empty( $cID ) ) {
echo "<input type='button' class='putPending button-primary' value='Already publish' disabled='disabled' />";
}
else {
echo "<input type='button' class='putPending button-primary' value='Edit' />";
}
echo "<hr />";
echo "</div>";
}
jQuery :
jQuery( '.putPending' ).live( 'click', function( ) {
var title = jQuery( this ).siblings( "h2" ).text( );
jQuery.ajax( {
url: ajaxurl,
datatype: 'json',
method: 'POST',
data:
{
action: 'ajax_fill_post',
title: title,
},
success: function( data )
{
var result = JSON.parse( data );
window.location.href = result.url;
}
});
});
我的 jQuery 将 title 返回到我的 post_item_function (我不会显示它,因为它是 100+ 行)。 foreach 获取 $current_category 并搜索与 $title 对应的项目的标题。如果没有 $current_category,我无法在单个 foreach 中执行此操作,它会为我的插件添加 250-300 多行。
【问题讨论】:
-
你能澄清一下吗 ->
from my jQuery without having to show it like my title. -
我目前可以获得我的类别的唯一方法是在我的小部件中显示它,比如我的标题 (
title
)。但我不想这样做,因为它会随每个项目一起显示,而且我有 15 个 xml 项目。