Registering your Custom Post Type using category

 /*

* Creating a function to create our CPT

*/

 

function custom_post_type() {

 

// Set UI labels for Custom Post Type

    $labels = array(

        'name'                => _x( 'Page Top Post', 'Post Type General Name', 'twentytwenty' ),

        'singular_name'       => _x( 'Page Post', 'Post Type Singular Name', 'twentytwenty' ),

        'menu_name'           => __( 'All Page Top Post', 'twentytwenty' ),

        'parent_item_colon'   => __( 'Parent Page Post', 'twentytwenty' ),

        'all_items'           => __( 'Page Top Post', 'twentytwenty' ),

        'view_item'           => __( 'View Page Post', 'twentytwenty' ),

        'add_new_item'        => __( 'Add New Page Post', 'twentytwenty' ),

        'add_new'             => __( 'Add New Post', 'twentytwenty' ),

        'edit_item'           => __( 'Edit Post', 'twentytwenty' ),

        'update_item'         => __( 'Update Post', 'twentytwenty' ),

        'search_items'        => __( 'Search Post', 'twentytwenty' ),

        'not_found'           => __( 'Not Found', 'twentytwenty' ),

        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwenty' ),

    );

     

// Set other options for Custom Post Type

     

    $args = array(

        'label'               => __( 'pagetoppost', 'twentytwenty' ),

        'description'         => __( 'Page Post news and reviews', 'twentytwenty' ),

        'labels'              => $labels,

        // Features this CPT supports in Post Editor

        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),

        // You can associate this CPT with a taxonomy or custom taxonomy. 

        'taxonomies'          => array( 'genres' ),

        /* A hierarchical CPT is like Pages and can have

        * Parent and child items. A non-hierarchical CPT

        * is like Posts.

        */ 

        'hierarchical'        => false,

        'public'              => true,

        'show_ui'             => true,

        'show_in_menu'        => true,

        'show_in_nav_menus'   => true,

        'show_in_admin_bar'   => true,

        'menu_position'       => 5,

        'can_export'          => true,

        'has_archive'         => true,

        'exclude_from_search' => false,

        'publicly_queryable'  => true,

        'capability_type'     => 'post',

        'show_in_rest' => true,

    );

    //  Registering your Custom Post Type using category   

    register_post_type( 'pagetoppost', $args );

}


/* Hook into the 'init' action so that the function

* Containing our post type registration is not 

* unnecessarily executed. 

*/

 

add_action( 'init', 'custom_post_type', 0 );


function my_taxonomies_product() {

  $labels = array(

    'name'              => _x( 'Page Categories', 'taxonomy general name' ),

    'singular_name'     => _x( 'Page Category', 'taxonomy singular name' ),

    'search_items'      => __( 'Search Page Categories' ),

    'all_items'         => __( 'All Page Categories' ),

    'parent_item'       => __( 'Parent Page Category' ),

    'parent_item_colon' => __( 'Parent Page Category:' ),

    'edit_item'         => __( 'Edit Page Category' ), 

    'update_item'       => __( 'Update Page Category' ),

    'add_new_item'      => __( 'Add New Page Category' ),

    'new_item_name'     => __( 'New Page Category' ),

    'menu_name'         => __( 'Page Categories' ),

  );

  $args = array(

    'labels' => $labels,

    'hierarchical' => true,

  );

  register_taxonomy( 'product_category', 'pagetoppost', $args );

}

add_action( 'init', 'my_taxonomies_product', 0 );


GET POSTS IN TEMPLATEPAGE.PHP

<?php

    $args = array(

      'post_type' => 'product',

      'tax_query' => array(

        array(

          'taxonomy' => 'product_category',

          'field' => 'slug',

          'terms' => 'boardgames'

        )

      )

    );

    $products = new WP_Query( $args );

    if( $products->have_posts() ) {

      while( $products->have_posts() ) {

        $products->the_post();

        ?>

          <h1><?php the_title() ?></h1>

          <div class='content'>

            <?php the_content() ?>

          </div>

        <?php

      }

    }

    else {

      echo 'Oh ohm no products!';

    }

  ?>

Comments

Popular posts from this blog

Get Post Data From Simple Post custom shortcode

Top 20 Beautiful Pen For Writing

Fatal error: Maximum execution time of 300 seconds exceeded in C:\xampp\phpMyAdmin\libraries\classes\Dbi\DbiMysqli.php on line 199