2017-02-23 66 views
0

我定義在WordPress一個自定義分類如下:調用WordPress的職位由自定義分類

function content_selector() { 
    register_taxonomy(
     'contentselector', 
     'post', 
     array(
       'label' => __('Content Selector'), 
       'rewrite' => array('slug' => 'cs'), 
       'hierarchical' => true, 
    ) 
); 
} 
add_action('init' , 'content_selector'); 

它顯示了在新的崗位和值可以分配,實際上它似乎工作。但是,當我使用下面的函數來調用這個分類的帖子時,沒有成功。

add_shortcode('rps', 'rpsf'); 
function rpsf() { 
    $args =[ 'posts_per_page' => 1, array(

      'tax_query' => array(
          array(
           'taxonomy' => 'contentselector', 
           'field' => 'slug', 
           'terms' => 'one-of-the-assigned-terms') 
     ))]; 

    $query = new WP_Query($args); 

    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); 
      ob_start(); ?> 

       <div class="rpst"> 
       <a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span</a> 
       </div> 

     <?php endwhile; endif; wp_reset_postdata(); 
    return ob_get_clean(); 
} 

我在定義分類法或調用帖子時犯了錯誤嗎?

回答

2

如果格式正確,調試代碼更容易,並且對於使用的符號保持一致(例如,[] vs array())。

「清潔」你$args定義之後,很明顯地看到,這是不正確的結構:

$args = array(
    'posts_per_page' => 1, 
    array(
     'tax_query' => array(
      array(
       'taxonomy' => 'contentselector', 
       'field' => 'slug', 
       'terms' => 'one-of-the-assigned-terms' 
      ) 
     ) 
    ) 
); 

應該看起來更像是這樣的:

$args = array(
    'posts_per_page' => 1, 
    'tax_query' => array(
     array(
      'taxonomy' => 'contentselector', 
      'field' => 'slug', 
      'terms' => 'one-of-the-assigned-terms' 
     ) 
    ) 
); 
+0

感謝您的幫助。 – ata

相關問題