Creating custom posts in WordPress (Custom post types)
Leave an answer
Recent Posts
- The 6 best wireless speakers to have anywhere in the house
- New launches in electronics and technology available on Amazon
- The best charging stations you should have at home for your devices
- The best smart light options to illuminate your home in a personalized way
- 6 great gift ideas for any tech lover
We are going to create a custom post type example for a real estate. We will install the latest version of wordpress and then we will see if the template we have supports thumbnail images, also known as thumbnails.
Many times as developers we need to create personalized content or some functionality in an organized way and register our own values and fields that WordPress does not have by default, it is also important that it be reusable, I look for the functions.php file in the template directory, here for example within the Twenty ten template.
Inside the file we look for if the function exists add_theme_support (‘post-thumbnails’) which is the one that allows the use of thumbnail images, if it is not, I will add it and I will also add a variable for a specific size.
In this case the template already has support for thumbnail images so I just add my custom size. This means that when I assign property-thumb to an image it will automatically be sized at 80 pixels wide by 80 pixels high. We started creating the functionality For this it is better to separate the code, you could put it at the end of the functions file, but to work more ordered we will put it in a directory Componentswhere we will create a file called real estate.php, so we have the separate component. // CUSTOM POST: Real Estate // I define the labels of the menu and forms // -------------------------------- -------------- $ real estate_labels = array ('name' => _x (‘Real estate’, ‘post type general name’), ‘singular_name’ => _x (‘Real estate’, ‘ post type singular name ‘),’ add_new ‘=> _x (‘ New property ‘,’ property ‘),’ add_new_item ‘=> __ (“New Property”),’ edit_item ‘=> __ (“Edit property”), ‘new_item’ => __ (“New property”), ‘view_item’ => __ (“View property”), ‘search_items’ => __ (“Search property”),’ not_found ‘=> __ (‘ No found properties’), ‘not_found_in_trash’ => __ (‘No properties’),’ parent_item_colon ‘=>’ ‘); // I create the arguments for the database $ inmobiliaria_args = array (‘labels’ => $ inmobiliaria_labels, ‘public’ => true, ‘publicly_queryable’ => true, ‘show_ui’ => true, ‘query_var’ => true , ‘rewrite’ => true, ‘hierarchical’ => false, ‘menu_position’ => null, ‘capability_type’ => ‘post’, ‘supports’ => array (‘title’, ‘excerpt’, ‘editor’, ‘thumbnail’), ‘menu_icon’ => get_bloginfo (‘template_directory’). ‘/images/photo-album.png’ // 16×16 png if you want an icon); // Register the post register_post_type (‘real estate’, $ real estate_args); ?> Then we include it in the functions.php file at the end or in a section for the components, in this case we put it together with the other configurations.
We are going to enter our wordpress administrator, in case this active we should update or close session and re-enter to update the changes made in the functions.php file, which is the one who invokes the component. As we can see, a new real estate me, to be able to manage our own data for this custom post component.
We will also create some categories or taxonomies so that our application is more complete. For example, the type of property, house, flat, etc. and another for provinces. For this, in the real estate file below we will add the following code, each block is a category and we could create the ones we want. php // Create Type of property add_action ('init', 'tx_tipo_inmueble', 0); function tx_tipo_inmueble () {register_taxonomy ('type', 'real estate', array ('hierarchical' => true, ‘label’ => ‘Types of property’, ‘singular_label’ => ‘Type’, ‘rewrite’ => true )); }?> php // Create Provinces add_action ('init', 'tx_province', 0); function tx_province () {register_taxonomy ('province', 'real estate', array ('hierarchical' => true, ‘label’ => ‘Provinces’, ‘singular_label’ => ‘province’, ‘rewrite’ => true)) ; }?> Then when updating our wordpress administrator we can see both categories in the real estate menu.
Listing our own dataThe wordpress entries and page in the administrator always have the same title, author and date data, in this case we need to list the data of the real estate component. For this we are going to customize the columns of the list Php // Customize columns // -------------------------------- -------------- // activate the action to customize columns // for the real estate component add_action ('manage_posts_custom_column', 'customize_columns'); add_filter ('manage_edit-real estate_columns', 'real estate_columns'); // I define which columns will be shown in the list function real estate_columns ($ columns) {$ columns = array ('cb' => ‘‘, ‘title’ => ‘Title’, ‘photo’ => ‘Photo’, ‘type’ => ‘Property type’, ‘province’ => ‘Province’, ‘date’ => ‘Date’,); return $ columns; }?> After indicating the titles and type of columns that will be displayed, we proceed to assign the data from queries that we will carry out in the database and assign the data to each column, in this case we search the categories with the function of wordpress get_the_term_list (). function customize_columns ($ column) {global $ post; switch ($ column) {case 'photo': echo the_post_thumbnail ('property-thumb'); break; case 'property': the_title (); break; case 'type': echo get_the_term_list ($ post-> ID, ‘type’, ”, ‘,’, ”); break; case ‘province’: echo get_the_term_list ($ post-> ID, ‘province’, ”, ‘,’, ”); break; }} // add thumbnail images to column add_filter (‘manage_posts_columns’, ‘showphoto’, 5); add_filter (‘manage_pages_columns’, ‘showphoto’, 5); add_filter (‘manage_custom_post_columns’, ‘showphoto’, 5); // Add the column function showpicture ($ cols) {$ cols[‘foto’] = __ (‘Thumbnail’); return $ cols; }?> In the case of the image we create a function to find the photo and add it to custom_post_columns if we want we can also add it so that it is supported for posts and pages, apart from our component, otherwise we remove those lines. // add thumbnail images to columnadd_filter ('manage_posts_columns', 'showphoto', 5); add_filter ('manage_pages_columns', 'showphoto', 5); add_filter ('manage_custom_post_columns', 'showphoto', 5); // Add the columnfunction showphoto ($ cols) {$ cols['foto'] = __ ('Thumbnail'); return $ cols;}?> We save the real estate.php file where we made these changes and updated the wordpress administrator page. We are going to test our application by registering a property, for this we will previously register the Types of properties categories: House, Apartment, Land, etc. Then we will register some provinces Barcelona, Madrid, etc. Then we go to the menu Properties> New property and we register the data as a normal wordpress entry, only that we will have the category and an image or photo to insert as a featured image.
Remember that the images will be sized at 80 x 80 pixels for the property list, we had defined this in our custom size real estate-thumb, So the best thing is that we use square images so that they are not cut, the ideal would be 500×500 pixels. Then when we save the changes we can go to view the list of properties and we will see our custom columns, with all the functionalities activated to search filter, sort alphabetically or see only some province, etc.
This will be very easy for us to administer and we can also add more functionalities if we need to modify the custom post type or custom post type. Regarding reuse, the component is optimal, since if we want to use it in another project we simply copy the custom post type, include it in our functions.php file and we will have it available to use without having to reprogram it, then with simple code We can show this on the home page or in a section of our website, carry out searches by province or by type of property. We can also extend the component by adding more functionalities or complementing it with other plugins, for example to implement it in several languages with Qtraslator or add multiple images Multiple Featured Images To have more prominent images and create a photo gallery for each property, changing some lines could also be used for a vehicle agency where the categories were vehicle brands and models or for a travel agency where we would have packages and destinations, the possibilities They are infinite, everything depends on the needs and our imagination.