使用if else在DOMDocument中创建createElement

时间:2014-05-22 08:41:37

标签: xml domdocument createelement

我有这段代码:

$args = array( 'post_type' => 'product', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); 
 global $product;
    $product = get_product( get_the_ID() );

  //create the title element
  $tutTag->appendChild(
    $xmlDoc->createElement("id", $loop->post->ID ));

  //create the date element
  $tutTag->appendChild(
    $xmlDoc->createElement("name", $loop->post->post_title ));

//create the date element
  $tutTag->appendChild(
    $xmlDoc->createElement("description", $loop->post->post_excerpt ));

//create the date element
  $tutTag->appendChild(
    $xmlDoc->createElement("category", $kategoria ));

endwhile; 

我需要$kategoria来执行此操作:

global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
if (is_array($terms)) {
foreach ( $terms as $term ){
    $category_id = $term->term_id;
    break; 
}
}
    $wccolor    = get_woocommerce_term_meta( $term->term_id, 'podujatie_pricemania_kategorie_pricemania_pridat', true );
if ( $wccolor ){
        echo $wccolor;
    } else {
        echo get_option('product_type');
    }

我尝试为$kategoria创建函数,但没有运气。

$kategoria的主要思想是自动从插件管理仪表板中选择优先选择。用户可以从更多类别中进行选择。但是如何实现这段代码?

我创建了xml feed,但我的一个客户需要将xml保存为xml文件,因此我正在实现xml文件的feed。但是根本没有运气。

谢谢。

1 个答案:

答案 0 :(得分:0)

这是我的问题的解决方案

$terms = get_the_terms( $post->ID, 'product_cat' );
$typ = get_option('product_type');
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $wccolor ){
        $category = $dom->createElement("category", $wccolor);
    } else {
        $category = $dom->createElement("category", $typ);
    }
    $product->appendChild($category);
}