Filtering fields based on CPT

This topic contains 3 replies, has 2 voices, and was last updated by  Paolo 9 years, 7 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #28801

    halalcompass
    Expired Member
    Post count: 32

    Hey everyone,

    I’ve been trying to modify the layout of the detail-page fields to achieve what is in the screenshot attached. I’ve realized that I have to extract each field manually and place them in my own html. I’ve done this using the below code (Ignore the HTML display part it’s not done):

    
    
    // ### BEGIN CUSTOM DETAILS ### //
    
    add_action( 'geodir_details_main_content', 'custom_details', 23 );
    function custom_details() {
    global $post; 
    global $wp_query;
    
    $postid = $wp_query->post->ID;
    
    $current_posttype = geodir_get_current_posttype();
    
    	$myauthor = get_the_author();
    	$myauthorlink = get_author_posts_url( get_the_author_meta( 'ID' ) ).'?post_type='.$current_posttype;
    //	echo '<p class="clearfix">Listed by <a href="' .$myauthorlink . '">' . $myauthor . '</a></p>';
    	
    	
    	$hc_title= geodir_get_post_meta($postid,'post_title',true); //title
    	
    	$hc_address=geodir_get_post_meta($postid,'post_address',true); //address
    	$hc_phone=geodir_get_post_meta($postid,'geodir_contact',true); //phone
    	$hc_email=geodir_get_post_meta($postid,'geodir_email',true); //email
    	$hc_website=geodir_get_post_meta($postid,'geodir_website',true); //website
    	$hc_facebook=geodir_get_post_meta($postid,'geodir_facebook',true); //facebook
    	$hc_twitter=geodir_get_post_meta($postid,'geodir_twitter',true); //facebook
    	$hc_restaurant_hours=geodir_get_post_meta($postid,'geodir_restaurant_hours',true); //hours
    	$hc_restaurant_ppp=geodir_get_post_meta($postid,'geodir_restaurant_ppp',true); //price
    	$hc_restaurant_wheelchair=geodir_get_post_meta($postid,'geodir_restaurant_wheelchair',true); //wheelchair
    	
    //		echo '<h3>'.$hc_title.'</h3>';
    	
    	echo '<p class="clearfix">Address: '.$hc_address.'</p>';
    	echo '<p class="clearfix">Phone: '.$hc_phone.' | Email: '.$customemail.'</p>';
    	echo '<p class="clearfix"><a href="'.$hc_website.'" target="_blank">Visit Website</a></p>';
    	echo '<p class="clearfix"><a href="'.$hc_facebook.'" target="_blank">Facebook</a></p>';
    	echo '<p class="clearfix"><a href="'.$hc_twitter.'" target="_blank">Twitter</a></p>';
    	echo '<p class="clearfix">Timing: '.$hc_restaurant_hours.'</p>';
    	echo '<p class="clearfix">Price Per Plate: '.$hc_restaurant_ppp.'</p>';
    	echo '<p class="clearfix">Wheelchair Accessible?: '.$hc_restaurant_wheelchair.'</p>';
    	
    		
    }
    
    // ### END CUSTOM DETAILS ### //

    The problem is that ALL these fields are being displayed on all CPTs, but I just want to show the fields for the relevant CPT. For example, the “Price Per Plate” field should show for Restaurants CPT but not for Schools.

    How would I go about doing this OR if there is a better way to achieve my screenshot layout, please advise.

    Thanks very much,
    Shiraz

    #28873

    Paolo
    Site Admin
    Post count: 31206

    You can use conditional tags to filter custom post type and present only the corresponding fields.

    Example:

    
    
    
    if ( 'gd_school' == get_post_type() ) { // school fields here }
    elseif ( 'gd_restaurant' == get_post_type() ) { // restaurant fields here }
    else { // nothing here in case it's neither school or restaurant }
    

    Let us know how you went.

    Thanks

    #30044

    halalcompass
    Expired Member
    Post count: 32

    This worked wonderfully, thank you.

    #30098

    Paolo
    Site Admin
    Post count: 31206

    Glad I could help! 🙂

Viewing 4 posts - 1 through 4 (of 4 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount