最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom post categories are not displaying on the page

programmeradmin0浏览0评论

I created a custom post type which is displaying on the menu. I created two posts and added them to the category. Everything working on the admin side.

Now I am trying to display on the website but it's not displaying. var_dump($plans); not displaying the output.

would you help me out what is the issue in this code?

Below code is for adding the in the menu

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');

Below code is for adding the short code in my wpbakery plugin. So that I will get the dropdown in the wpbakery plugin and select the category

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' );

I am using below code to display the output.

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');

I created a custom post type which is displaying on the menu. I created two posts and added them to the category. Everything working on the admin side.

Now I am trying to display on the website but it's not displaying. var_dump($plans); not displaying the output.

would you help me out what is the issue in this code?

Below code is for adding the in the menu

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');

Below code is for adding the short code in my wpbakery plugin. So that I will get the dropdown in the wpbakery plugin and select the category

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' );

I am using below code to display the output.

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');

Share Improve this question edited Aug 22, 2020 at 13:01 user9437856 asked Aug 21, 2020 at 18:17 user9437856user9437856 1117 bronze badges 2
  • instade of 'showposts' use 'numberposts'. – Behemoth Commented Aug 21, 2020 at 19:31
  • @Behemoth, I added numberposts. Do you know that where is the issue in the code? – user9437856 Commented Aug 22, 2020 at 4:10
Add a comment  | 

1 Answer 1

Reset to default 0
  $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>';

Change your register_taxonomy like this

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');
发布评论

评论列表(0)

  1. 暂无评论