【问题标题】:Get xml attribute获取xml属性
【发布时间】: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 项目。

标签: php jquery xml wordpress


【解决方案1】:

在您构建的循环之外,您可以简单地迭代 category XML 标记,然后为其创建隐藏输入。

foreach ( $channel->channel->category as $idx => $category);
    echo '<input type="hidden" name="category" class="category" data-categoryIndex="'.$idx.'"  value="'.$category.'"/>';
endforeach;

那么你就可以这样访问了:

$('.category[data-categoryIndex="'+1/2/3/etc+'"]').val();

这将为您提供基于 data-categoryIndex="1/2/3/etc" 的类别,该类别已由我们的 $idx 变量在我们的 foreach 循环中分配。

如果你只有一个类别,那很简单:

$('.category').val();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 2016-11-05
    • 2021-11-17
    相关资源
    最近更新 更多