Custom Fields
Re-Envisioned
Support General How to use conditional logic and then display custom meta on a page.

This topic contains 5 replies, has 2 voices, and was last updated by  @mindspark 1 year, 1 month ago.

  • Creator
    Topic
  • #10393
    Resolved
    @mindspark
    Participant

    I have created a CPT – ‘People’. Each person works in one of two offices – Tampa or St Pete. The office locations are Taxonomy assigned to CPT People. I am using MetaBox AIO and I am not a programmer.

    I am having trouble with:

    1. How / where do I provide the actual address for each of the 2 office locations?
    2. I need help writing the snippet that will display the office address based on which office each People is assigned to.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #10396

    Anh Tran
    Keymaster

    Hi,

    Are “Tampa” and “St Pete” terms of the “office location” taxonomy?

    If so, you have some ways to store the actual address of each location (each term):

    • Use the default WordPress term’s description for actual address (https://i.imgur.com/GIU2Bbb.png). If you don’t use the description for any purpose, it’s good to use it for address, cause it doesn’t require any coding.
    • Or you can use the MB Term Meta extension to add a text field “Address” for “office location” taxonomy. You can use the Meta Box Builder to create that field for you if you’re not familiar with coding. Just remember to go to Settings tab and select “Terms” & “Taxonomy” for the setting “Show meta box” for.

    After that, you’ll be able to enter address for each location (“term”) when you edit them.

    To show the office address, you can use this snippet:

    $locations = wp_get_object_terms( get_the_ID(),  'office_location_taxonomy' );
    if ( ! empty( $locations ) && ! is_wp_error( $locations ) ) {
        $location = reset( $locations );
        echo 'Address: ', $location->description; // If you use term description.
        echo 'Address: ', rwmb_meta( 'address_field_id', array( 'object_type' => 'term' ), $location->term_id ); // If you use MB Term Meta
    }
    #10413

    @mindspark
    Participant

    Yes Tampa and St Pete are taxonomies assigned to CPT People. thanks for help with the script….

    quick follow up –

    I’m also using Beaver Themer addon and Astra theme. I know that the Themer addon makes this a bit easier, but I need to add leading text before custom meta is displayed in same row – example:

    Direct Line: [custom meta field PHONE NUMBER].

    So am I able to add the snippet you helped with into a text box or HTML module? or do I need something more advanced to run PHP inside a page or widget?

    thanks!

    #10414

    @mindspark
    Participant

    It just occured to me that I may need to share more info about my project. See below:

    My People CPT are attorneys. there are about 18 meta fields in use to collect info on each Person that will be used to display when users visit the Profile page.

    most info is simple data, but, the People have a few custom items. this work is to make it easy for client to add their own new employees and then to ensure the right info is being displayed on the front end –

    if People work in Tampa, I need to display the Tampa address
    if People work in St. Pete, I need to display the St. Pete address

    also, each of the People CPT are authors for various blog articles, so we are using the Co-Authors plugin to handle that. this is important to mention because in addition to the profile info displayed for each attorney, we also need to display all articles attributed to them.

    here is a URL to the live site (uses WCK and a ton of custom code) which we are replicating using MEtaBox – https://www.trenam.com/people-list/paul-d-bain/

    #10426

    Anh Tran
    Keymaster

    Thanks for your details!

    Because the Tampa and St. Pete are terms that are connected to People CPT, to get the address, we actually go from post (People) -> taxonomy term (Tampa/St. Pete) -> term field.

    So I think the Beaver addon doesn’t work. It can only get post field for the current post.

    I’d suggest you write a custom shortcode to get the term address. Then you can put it in the Text/Heading field in the Beaver Themer. Adding prefix text is also easy then.

    #10431

    @mindspark
    Participant

    Thanks Anh. I’ll let you know how it turns out.

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.