Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每个自定义帖子的wordpress侧边栏_Wordpress_Custom Post Type_Sidebar - Fatal编程技术网

每个自定义帖子的wordpress侧边栏

每个自定义帖子的wordpress侧边栏,wordpress,custom-post-type,sidebar,Wordpress,Custom Post Type,Sidebar,我试图让wordpress为每个自定义帖子类型的帖子添加一个侧栏。 因此,当我进入widgets区域时,我希望有尽可能多的侧栏,就像有我命名为portfolio的自定义帖子类型的帖子一样 这是我正在使用的代码,但我只得到一个没有标题的边栏 更新:我在代码中添加了global$posts,现在我得到了侧栏,然后我更改了_title()以获得_title()。现在它开始工作了。完整代码如下。如果有人有更好的解决方案,请告诉我 function the_slug() { $post_data

我试图让wordpress为每个自定义帖子类型的帖子添加一个侧栏。 因此,当我进入widgets区域时,我希望有尽可能多的侧栏,就像有我命名为portfolio的自定义帖子类型的帖子一样

这是我正在使用的代码,但我只得到一个没有标题的边栏

更新:我在代码中添加了global$posts,现在我得到了侧栏,然后我更改了_title()以获得_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();
    }
}
函数_slug(){
$post\u data=get\u post($post->ID,数组A);
$slug=$post_data['post_name'];
返回$slug;
}
添加_操作('init','portfolions_边栏');
/**
*为每个投资组合创建widgetized边栏
*
*此函数附加到“init”操作挂钩。
*/
函数(U侧边栏)(){
$args=array('post_type'=>'公文包','numberposts'=>-1,'post_status'=>'发布');
$portfolions=get_posts($args);
全球$员额;
if(投资组合){
foreach($post形式的投资组合){
设置_postdata($post);
$portfoliotitle=获取标题();
$portfolioslug=_slug();
寄存器侧栏(数组)(
'name'=>$portfoliotitle,
'id'=>$portfolioslug.'-sidebar',
'description'=>'这是'$portfoliotitle.'widgetized area',
'在小部件'=>''之前,
'在小部件'=>''之后,
“在标题“=>”之前,
“在标题“=>”之后,
) );
}
wp_reset_postdata();
}
}

我只想删除不必要的函数调用:

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>',
            ) );
        }
    }
}
add_action('init','portfolions_边栏');
/**
*为每个投资组合创建widgetized边栏
*
*此函数附加到“init”操作挂钩。
*/
函数(U侧边栏)(){
$args=array('post_type'=>'公文包','numberposts'=>-1,'post_status'=>'发布');
$portfolions=get_posts($args);
if(投资组合){
foreach($组合作为$组合){
$portfoliotitle=$portfolio->post_title;
$portfolioslug=$portfolio->post_name;
寄存器侧栏(数组)(
'name'=>$portfoliotitle,
'id'=>$portfolioslug.'-sidebar',
'description'=>'这是'$portfoliotitle.'widgetized area',
'在小部件'=>''之前,
'在小部件'=>''之后,
“在标题“=>”之前,
“在标题“=>”之后,
) );
}
}
}