页面上未显示自定义帖子类别

时间:2020-08-21 作者:user9437856

我创建了一个自定义的帖子类型,显示在菜单上。我创建了两个帖子并将它们添加到类别中。管理方面的一切工作。

现在我试图在网站上显示,但它没有显示。var_dump($plans); 不显示输出。

你能帮我找出代码中的问题吗?

以下代码用于在菜单中添加

function create_travelplan_cpt() {

    $labels = array(
        \'name\' => _x( \'travelplans\', \'Post Type General Name\', \'travelplanslist\' ),
        \'singular_name\' => _x( \'travelplan\', \'Post Type Singular Name\', \'travelplanslist\' ),
        \'menu_name\' => _x( \'travelplans\', \'Admin Menu text\', \'travelplanslist\' ),
        \'name_admin_bar\' => _x( \'travelplan\', \'Add New on Toolbar\', \'travelplanslist\' ),
        \'archives\' => __( \'travelplan Archives\', \'travelplanslist\' ),
        \'attributes\' => __( \'travelplan Attributes\', \'travelplanslist\' ),
        \'parent_item_colon\' => __( \'Parent travelplan:\', \'travelplanslist\' ),
        \'all_items\' => __( \'All travelplans\', \'travelplanslist\' ),
        \'add_new_item\' => __( \'Add New travelplan\', \'travelplanslist\' ),
        \'add_new\' => __( \'Add New\', \'travelplanslist\' ),
        \'new_item\' => __( \'New travelplan\', \'travelplanslist\' ),
        \'edit_item\' => __( \'Edit travelplan\', \'travelplanslist\' ),
        \'update_item\' => __( \'Update travelplan\', \'travelplanslist\' ),
        \'view_item\' => __( \'View travelplan\', \'travelplanslist\' ),
        \'view_items\' => __( \'View travelplans\', \'travelplanslist\' ),
        \'search_items\' => __( \'Search travelplan\', \'travelplanslist\' ),
        \'not_found\' => __( \'Not found\', \'travelplanslist\' ),
        \'not_found_in_trash\' => __( \'Not found in Trash\', \'travelplanslist\' ),
        \'featured_image\' => __( \'Featured Image\', \'travelplanslist\' ),
        \'set_featured_image\' => __( \'Set featured image\', \'travelplanslist\' ),
        \'remove_featured_image\' => __( \'Remove featured image\', \'travelplanslist\' ),
        \'use_featured_image\' => __( \'Use as featured image\', \'travelplanslist\' ),
        \'insert_into_item\' => __( \'Insert into travelplan\', \'travelplanslist\' ),
        \'uploaded_to_this_item\' => __( \'Uploaded to this travelplan\', \'travelplanslist\' ),
        \'items_list\' => __( \'travelplans list\', \'travelplanslist\' ),
        \'items_list_navigation\' => __( \'travelplans list navigation\', \'travelplanslist\' ),
        \'filter_items_list\' => __( \'Filter travelplans list\', \'travelplanslist\' ),
    );
    $args = array(
        \'label\' => __( \'travelplan\', \'travelplanslist\' ),
        \'description\' => __( \'\', \'travelplanslist\' ),
        \'labels\' => $labels,
        \'menu_icon\' => \'dashicons-book-alt\',
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\'),
        \'taxonomies\' => array(\'travelPlans_cat\'),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'menu_position\' => 80,
        \'show_in_admin_bar\' => true,
        \'show_in_nav_menus\' => true,
        \'can_export\' => true,
        \'has_archive\' => true,
        \'hierarchical\' => false,
        \'exclude_from_search\' => false,
        \'show_in_rest\' => true,
        \'publicly_queryable\' => true,
        \'capability_type\' => \'post\',
    );
    register_post_type( \'travelplan\', $args );

}
add_action( \'init\', \'create_travelplan_cpt\', 0 );

function create_travelPlan_taxonomy() {
    register_taxonomy(
        \'travelPlans_cat\',
        \'travelplan\',
        array(
            \'label\' => __( \'Category\' ),
            \'rewrite\' => array( \'slug\' => \'testtravelplan\' ),
            \'hierarchical\' => true,
        )
    );
}
add_action( \'init\', \'create_travelPlan_taxonomy\');
下面的代码用于在我的wpbakery插件中添加短代码。因此,我将在wpbakery插件中获得下拉列表并选择类别

function travelPlans_cat(){
    $args = array(
       \'child_of\'                 => 0,
       \'parent\'                   => \'\',
       \'orderby\'                  => \'name\',
       \'order\'                    => \'ASC\',
       \'hide_empty\'               => 1,
       \'hierarchical\'             => 1,
       \'taxonomy\'                 => \'travelPlans_cat\',
       \'pad_counts\'               => false );
    $cats = get_categories($args);
    return $cats;
}

function shortcode_travelPlansDropdown(){
$testcats = travelPlans_cat();
$travelPlans_category[\'All\']="All";
foreach($testcats as $testcat){
    $travelPlans_category[$testcat->slug] = $testcat->cat_name;
}
vc_map( 
    array(
        \'name\'     => __(\'Travel Plans\'),
        \'base\'     => \'travelplans\',
        \'category\' => __(\'Test shortcodes\'),
        "icon" => get_template_directory_uri() . "/images/shortcode_blog.png",
        \'params\'   => array(
            array(
                \'type\'        => \'dropdown\',
                \'heading\'     => __(\'Category\'),
                \'param_name\'  => \'cat\',
                \'admin_label\' => true,
                \'value\'       => $travelPlans_category,
        \'std\'         => \'0\', // Your default value
        \'description\' => __(\'Select Category\')
        ))
    )
);
}
add_action( \'init\', \'shortcode_travelPlansDropdown\' );
我使用下面的代码来显示输出。

function viewTravelPlan( $atts ){
    if($atts[\'cat\']==\'All\'){            
        $plans = get_posts(array(
          \'numberposts\' => 80, //add -1 if you want to show all posts
          \'post_type\' => \'travelplan\'
          ));
    }else{  
        $plans = get_posts(array(
          \'numberposts\' => 10, //add -1 if you want to show all posts
          \'post_type\' => \'travelplan\',
          \'tax_query\' => array(
              array(
            \'taxonomy\' => \'travelPlans_cat\',
            \'field\' => \'slug\',
            \'terms\' => $atts[\'cat\'] //pass your term name here
              )
            ))
           );
    }
    var_dump($plans);   //it\'s notdisplaying anything
    
       $data = \'<div class="main-carousel mt-5">\';
        foreach($plans as $blogslider){
            
        $tid = $blogslider->ID;
        $data.= \' <div class="carousel-cell">
        <a href="\'.get_permalink($tid).\'">
                    <div class="blogBoxwrapper">
                    <p>Testing</p>
                    </div>
                    </a>
                    </div>\';   
        }
        $data.=\'</div>\';
        
        return $data; 
    
}
add_shortcode( \'travelPlan\', \'viewTravelPlan\');

enter image description here

1 个回复
SO网友:Sobir

  $args = array(
    \'post_type\'   => \'travelplan\',
    \'numberposts\' => -1,
    \'tax_query\'   => array(
       array(
          \'taxonomy\' => \'travelplan_cat\',
          \'field\' => \'slug\',
          \'terms\' => \'testtravel\'
       )
     )
   );
  $plans = get_posts( $args );

  echo \'<pre>\';
  print_r($plans);
  echo \'</pre>\';
更改您的register_taxonomy 像这样

function create_travelPlan_taxonomy() {
    register_taxonomy(
        \'travelplan_cat\',
        \'travelplan\',
        array(
            \'label\' => __( \'Category\' ),
            \'rewrite\' => array( \'slug\' => \'travelplan_cat\' ),
            \'hierarchical\' => true,
        )
    );
}
add_action( \'init\', \'create_travelPlan_taxonomy\');

相关推荐