2012-11-23 31 views
2

我試圖讓wordpress爲自定義帖子類型的每個帖子添加一個側欄。 因此,當我進入小工具區域時,我希望在那裏有儘可能多的邊欄,因爲有自定義帖子類型的帖子命名爲portfolio。每個自定義帖子的wordpress側欄

這是我正在使用的代碼,但我只得到1個沒有標題的邊欄。

更新:我在代碼中添加了全局$帖子,現在我獲得了側邊欄,然後我將the_title()更改爲get_the_title()。現在它正在工作。下面的完整代碼。如果有人有更好的解決方案,請讓我知道。

function the_slug() { 
    $post_data = get_post($post->ID, ARRAY_A); 
    $slug = $post_data['post_name']; 
    return $slug; 
} 

add_action('init', 'portfolios_sidebars'); 
/** 
* Create widgetized sidebars for each portfolio 
* 
* This function is attached to the 'init' action hook. 
*/ 

function portfolios_sidebars() { 
    $args = array('post_type' => 'portfolios', 'numberposts' => -1, 'post_status' => 'publish'); 
    $portfolios = get_posts($args); 
    global $post; 
    if ($portfolios) { 
     foreach ($portfolios as $post) { 
      setup_postdata($post); 
      $portfoliotitle = get_the_title(); 
      $portfolioslug = the_slug(); 
      register_sidebar(array(
       'name' => $portfoliotitle, 
       'id' => $portfolioslug . '-sidebar', 
       'description' => 'This is the ' . $portfoliotitle . ' widgetized area', 
       'before_widget' => '<aside id="%1$s" class="widget %2$s" role="complementary">', 
       'after_widget' => '</aside>', 
       'before_title' => '<h4 class="widget-title">', 
       'after_title' => '</h4>', 
      )); 
     } 
     wp_reset_postdata(); 
    } 
} 

回答

0

我只想刪除不必要的函數調用:

add_action('init', 'portfolios_sidebars'); 
/** 
* Create widgetized sidebars for each portfolio 
* 
* This function is attached to the 'init' action hook. 
*/ 

function portfolios_sidebars() { 
    $args = array('post_type' => 'portfolios', 'numberposts' => -1, 'post_status' => 'publish'); 
    $portfolios = get_posts($args); 
    if ($portfolios) { 
     foreach ($portfolios as $portfolio) { 
      $portfoliotitle = $portfolio->post_title; 
      $portfolioslug = $portfolio->post_name; 
      register_sidebar(array(
       'name' => $portfoliotitle, 
       'id' => $portfolioslug . '-sidebar', 
       'description' => 'This is the ' . $portfoliotitle . ' widgetized area', 
       'before_widget' => '<aside id="%1$s" class="widget %2$s" role="complementary">', 
       'after_widget' => '</aside>', 
       'before_title' => '<h4 class="widget-title">', 
       'after_title' => '</h4>', 
      )); 
     } 
    } 
} 
相關問題