single.php on wordpress

0

If I have a post my single.php will display this my full post on the site. Correct?

But if I also have a page of view of developments, real estate, cars, etc ... Where each of these views have different styles, how can I distinguish this in wordpress? For example: The single would treat only the posts. For projects, for example, it would be another view with the same behavior as single.php, however it is not single.php!

Have you understood?

    
asked by anonymous 11.01.2018 / 12:19

1 answer

4

When you work with various types of posts and want to have a style for each, you can choose to use Custom Post Type . With this you can create posts of various types, with several other elements through metaboxes . This will give you greater control over the post itself.

Although you can customize these posts , they will still behave like any other post of WordPress.

The advantage is that you can better manage those custom posts on the front end. It will also allow you to create "special" pages with unique characteristics for certain types of post .

Creating Post Type

To create a custom post type , you can use the following code:

function create_post_type_car() {
  register_post_type( 'post_car', [
      'labels' => [
        'name' => "Cars",
        'singular_name' => "Car"
      ],
      'public' => true,
      'has_archive' => true,
    ]
  );
}
add_action( 'init', 'create_post_type_car' );

More information about the register_post_type function can be found at documentation .

Displaying Post Type

Creating a post_type is quite simple, but displayed is simpler still. To do this simply create a file, at the root of your theme, called single-post-car.php . WordPress will automatically identify and "link" the post to this page.

This is made possible by Template Hierarchy of WordPress .

File Hierarchy

In the database, more specifically in the wp_posts table, the CMS will add all submitted attachments and posts information.

When WP captures this information it follows a series of actions before displaying them, one of these actions involves fetching some files at the root of your theme, and according to mimetype ( files) or post type (posts), search for a particular file to serve as view .

In this search (in the case of posts) WordPress will return the > slug (or simply Url Amigavel) along with other post information.

With this information, WP will look for the file single-$posttype-$slug , if it does not find it, it will continue searching for the necessary files until it finds or reaches the "parent" file, index.php p>

  

The order for Custom Post Types is: single- $ posttype- $ slug.php - > single- $ posttype.php - > single.php - > singular.php - > index.php

Filtering Post Type

You can also filter certain types of posts (To make a side menu, for example). For this you only need to use the class WP_Query . Ex:

$posts = new WP_Query([
    "post_type" => "post_car"
]);

if ($posts->have_posts()) {
    while($posts->have_posts()) {
        $posts->the_post();

        the_title();

        /* Code Here */
    }
}

Working with static pages in WordPress

If you only have static pages (Yes, they also "descend" from single.php ), you can not use the above information as a basis. For this type of page, WordPress has another way of working.

These pages are typically used for forms of contact , about us , event calendar etc.

To create them is very simple, just create a php file, example aboutus.php and use the code below to serve as view .

Code:

<?php

/* Template Name: AboutUs */

/* Todo o restante do código aqui. Html, css, js; PHP etc */

?>

This code will cause an option to appear when adding a new static page.

Optionaddedwhencreatingan.phpfilewiththecomment"Template Name"

In this way you can work with several templates pages, with different files, different information, different styles, different business logics etc.

    
11.01.2018 / 12:49