0

I'm developing a real estate site for a client and I'm struggling a bit with it. I created a custom post type called "Property". A property can be on sale and/or on rent. Two different menu items in the site, "Properties on Sale" and "Properties on Rent", show a list of properties either on sale or on rent.

When a user clicks on a property on the "Properties on Sale" list, I need to show some data, and when the user clicks on a property on the "Properties on Rent" list, I need to show slightly different data. Basically, there would be two slightly different single property templates. My question is, how can I do this? Do I need to add some parameter to the url of a property (something like www.domain.com/property/ID/?onsale and www.domain.com/property/ID/?onrent).

Any help would be appreciated.

1

Add categories to your custom posts. Ex:

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

function create_gallery_taxonomies() {

    $labels = array(
        'name' => _x( 'B&A Categories', 'taxonomy general name' ),
        'singular_name' => _x( 'B&A', 'taxonomy singular name' ),
        'search_items' => __( 'Search B&A Categories' ),
        'all_items' => __( 'All B&A Categories' ),
        'parent_item' => __( 'Parent B&A Category' ),
        'parent_item_colon' => __( 'Parent B&A Category:' ),
        'edit_item' => __( 'Edit B&A Category' ),
        'update_item' => __( 'Update B&A Category' ),
        'add_new_item' => __( 'Add New B&A Category' ),
        'new_item_name' => __( 'New B&A Category Name' ),
        'menu_name' => __( 'B&A Categories' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'gallery-categories' ),
    );

    register_taxonomy( 'gallery-categories', array( 'gallery_post' ), $args );

}

Check out that last line where it sais "gallery_post", that would'be your custom post type.

Then you can add the parameter to the url, and display only the ones from your category

$gallery_slug = $_GET['parameter'];

$gallery_menu =  get_terms('gallery-categories', 'hide_empty=1');

foreach($gallery_menu as $item) {
    if( $item->slug == $gallery_slug ) {
        $current_cat = $item;
    }
}
if( !$current_cat )
    $current_cat = get_term(81, 'gallery-categories'); // DEFAULT CATEGORY TO SHOW


$args = array(
    'post_type' => 'gallery_post',
    'taxonomy' => $current_cat->taxonomy,
    'term' => $current_cat->slug,
    'posts_per_page' => -1,
    'nopaging' => true,
);
$query = new WP_Query( $args );

On the single template, you have to check which category the post has to show whatever info is needed.

| improve this answer | |
  • 1
    Just a note here, never use hyphens in taxonomy or post type types. They are really problematic later on, believe me. Only use underscores to separate words – Pieter Goosen Oct 16 '14 at 4:33
1

I think you could do this with one template such as single-property.php.

As Sanchy recommended you would want categories for "sale" and "rental."

You would also want custom fields for the data. The Advanced Custom Fields Plug-in makes it easy to add those. https://wordpress.org/plugins/advanced-custom-fields/ Add all the ones you would want for both rental and sale. Then when you or your client are adding property info to the site, they will choose the appropriate category then only fill in the fields they need for that category. (You can include instructions about this.)

Then in your template, use something like this to call the content for the fields. This could call in the field named "price."

<?php if(get_field('price'))
{
echo '<p>' . get_field('price') . '</p>';
}
?>

This way only the fields that have been filled in should display. Advanced Custom Fields offers additional documentation at http://www.advancedcustomfields.com

| improve this answer | |
0

WordPress will autoload different templates for custom post types:

Template file used to render a single post page.

  1. single-{post_type}.php - If the post type were product, WordPress would look for single-product.php.

http://codex.wordpress.org/Template_Hierarchy#Single_Post_display

That should be all you need to load your "slightly different single property templates"

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.