2012-11-23 3 views
2

나는 사용자 정의 게시 유형의 각 게시물에 대해 사이드 바를 추가하도록 wordpress를 얻으려고합니다. 그래서 내가 위젯 영역으로 갈 때 나는 거기에 많은 사이드 바를 가지고 싶습니다.각 사용자 지정 게시에 대한 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>', 
      )); 
     } 
    } 
} 
관련 문제