2012-11-23 32 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>', 
      )); 
     } 
    } 
} 
相关问题