Support » Developing with WordPress » Custom Post Type

  • Resolved Xaibi Aslam

    (@lahorimela)


    Hi,
    I’m interested in a custom post-type system. Plugins and codes are available, but I don’t want to use them. Instead, I would like a section called CITIES. There, I would enter the City Name, City Slug, and City Description. However, I want it to display all posts from the tagged posts in the POSTS section when I display it on the front end. And to display there would be a custom template (cities.php) as, like tags (tags.php or Single.php), I will display the posts by the given name of the city.

    That’s what I’m looking for. I hope you understand.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Xaibi Aslam

    (@lahorimela)

    Okay, I got this and all things are good but it will not display the posts because we have to add them in every post, is it possible that the same name or slug will take the tags posts.

    /** CITIES **/
    /*
     * Add Custom Taxonomies
     */
    function cities_custom_taxonomies() {
    
      $taxonomies = array(
    
        // Director
        array(
          'name'   => 'city', // here the name of the taxonomy
          'single' => 'City', // here the single taxonomy name to show in the backend
          'plural' => 'Cities', // here the plural taxonomy name to show in the backend
          'cpt'    => 'post' ), // which Custom Post Type to associate the taxonomy
      );
    
      foreach ($taxonomies as $taxonomy) {
    
        $name = $taxonomy['name'];
        $single = $taxonomy['single'];
        $plural = $taxonomy['plural'];
        $cpt = $taxonomy['cpt'];
    
        $labels = array(
          'name'              => _x( $plural, 'taxonomy general name' ),
          'singular_name'     => _x( $single, 'taxonomy singular name' ),
          'search_items'      => __( 'Search '. $plural ),
          'all_items'         => __( 'All '. $plural ),
          'parent_item'       => __( 'Parent '. $single ),
          'parent_item_colon' => __( 'Parent '. $single.':' ),
          'edit_item'         => __( 'Edit '. $single ),
          'update_item'       => __( 'Update '. $single ),
          'add_new_item'      => __( 'Add New '. $single ),
          'new_item_name'     => __( 'New Name '. $single ),
          'menu_name'         => __(  $plural )
        );
    
        $args = array(
          'labels'            => $labels,
          'hierarchical'      => true,  // false: as Tags, true: as Category
          'show_ui'           => true,
          'show_admin_column' => true
        );
    
        register_taxonomy( $name, $cpt, $args );
    
      } //foreach
    }
    add_action( 'init', 'cities_custom_taxonomies', 0 );
    Moderator bcworkz

    (@bcworkz)

    You can get a custom template to be used for certain situations by properly naming the template file. See
    https://developer.wordpress.org/themes/basics/template-hierarchy/#custom-taxonomies

    In some cases the default taxonomy term query doesn’t pick up the post types that you want it to. You can modify the query through the “pre_get_posts” action and set the “post_type” query var to the desired post type or array of types.
    https://developer.wordpress.org/reference/hooks/pre_get_posts/

    Thread Starter Xaibi Aslam

    (@lahorimela)

    Thanks, I’ll do more research on that. Regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.