N
N
Nikita2015-11-08 14:03:20
WordPress
Nikita, 2015-11-08 14:03:20

How to properly display a page in Wordpress?

I'm trying to learn how to work with wp. Experience in programming is small, tell me how to properly implement my idea. I have registered a new post type - my_trans - register_post_type('my_trans, $args); (do I need to create a rule 'rewrite'=> array('slug'=>'my_trans', 'with_front'=>true, 'hierarchical'=>true)?. And registered a taxonomy for it - register_taxonomy('my_category', ' my_trans', $args); - my_category, with the name of the parent taxonomy My transport and added children for it - cars, trucks, motorcycles, planes, steamboats, etc. and added pages to each category - for example, for cars - this is mercedes, bmw, audi, i.e. indicated in the taxonomy - my transport and its descendant cars.After that, I added a template

add_filter( 'single_template', 'my_auto_template_function', 20);

functionmy_auto_template_function'() {
  if ( get_post_type() == 'my_trans' ) {
    if ( is_single() ) {
      // checks if the file exists in the theme first,
      // otherwise serve the file from the plugin
      if ( $theme_file = locate_template( array ( 'single-my_trans.php' ) ) ) {
        $template_path = $theme_file;
        } 
      }
    }
  return $template_path;
}

Where he described the desired page structure. And here a snag arose (my misunderstanding) with the naming of pages and how to resolve it correctly.
I made a menu - My transport and, accordingly, a submenu from its descendants
. My transport - Cars; - page address - www.example.com/my_category/legkovie
Trucks; - page address - www.example.com/my_category/gruzovie
Motorcycles; - page address - www.example.com/my_category/motocikli
Aircraft; - page address - www.example.com/my_category/samolety
Steamboats; - page address - www.example.com/my_category/parohodi
those. here goes the page is formed by its taxonomy. And when I go, for example, to the page www.example.com/my_category/legkovie, then I accordingly open a page on which a list of all cars (bmw, mercedes, audi).
And so the essence of the problem, if I go to the page www.example.com/my_category/my_trans/ - I have everything, both cars and trucks, and planes, and motorcycles, and steamboats are displayed together. Is it possible somehow on this page to display the names of child taxonomies - i.e. cars, trucks, motorcycles, ships, airplanes. And where is it correct to set it up - in the template to do the check or somewhere else?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2015-11-09
@Kozack

Read this - 3 Ways to Create a Page Template
There is also a diagram of how the VI looks for the template file .
For example, if Легковые автоyou want to make a custom template page for a term, you can create a taxonomy-my_category-legkovie.php.
In your case, if I understood everything correctly, you need to create a file taxonomy-my_category-my_trans.phpin which to check for the presence of child terms, and if there are any, output them.

N
Nikita, 2015-11-11
@Nivalis

Not exactly, the fact is that child taxonomies need the same template, but it’s the parent taxonomy that needs a different template . On the 36cff884f1bc454ea48db9822507c26d.png
left, the picture is how I want the page with the parent taxonomy to look, and on the middle picture, how it looks now. But your method involves creating a template for each child taxonomy, and I would like to have a template for the parent category. Or is it already inside the template that needs to be sorted out, such as if parent from wp_get_post_term = 0 (that is, this is my transport), then one markup, otherwise another.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question