Home › Forums › Pro Support › Custom Fields
Tagged: acf
- This topic has 43 replies, 11 voices, and was last updated 2 years, 10 months ago by
elvin.
-
AuthorPosts
-
January 29, 2020 at 4:43 am #13000
Cris
ParticipantSorry, another thing.
How can I customize the “custom fields” using css? I don’t know how to add classes or a icon image.I explain, because maybe I can’t do what I want:
I am doing a real estate website and I want to use WPSP to display them on the home page. I would like to put below the photo, the characteristics (bedrooms, bathrooms, price, etc …).
With the code you give, the custom fields appear pasted behind each other and I would like to add an icon or text in front of them. I also have some taxonomies that I want to place next to the custom fields.
You can see what I am doing here.
In addition to custom fields I am using taxonomies to be able to search with the Search & Filter pluginThanks!
January 29, 2020 at 5:45 pm #13016Tom
KeymasterTo display terms from a taxonomy, you would need to use this function: https://developer.wordpress.org/reference/functions/get_terms/
To add a class to your custom fields, you can do this:
add_action( 'wpsp_before_content', function() { $meta = get_post_meta( get_the_ID(), 'precio', true ); if ( isset( $meta ) && '' !== $meta ) { echo '<div class="your-class-name-here">'; echo $meta; echo "€"; echo '</div>'; } } );
January 30, 2020 at 3:44 am #13033Cris
ParticipantThanks Tom, your code to add class works fine.
I hope you don’t laugh at me but I’m not a developer:) I did this and only the word “Array” returns to me.
$terms = get_terms( ( array('taxonomy' => 'post_tag', 'hide_empty' => false,))); if ( isset( $terms ) && '' !== $terms ) { echo '<div class="tax referencia">'; echo $terms; echo '</div>'; }
Can you help me with taxonomies, please?
January 30, 2020 at 10:30 am #13038Gwen
ParticipantJust want to catch up here with a THANK YOU for that code, Tom! (I’ve been away sick and don’t want you to think I’m unappreciative.)
January 30, 2020 at 8:28 pm #13058Tom
KeymasterYou’re welcome, Gwen! Hope you’re feeling better 🙂
Cris – that’s close, try this:
$terms = get_terms( ( array('taxonomy' => 'post_tag', 'hide_empty' => false,))); if ( isset( $terms ) && '' !== $terms ) { foreach( $terms as $term ) { echo '<div class="tax referencia">'; echo $term->name; echo '</div>'; } }
January 31, 2020 at 12:40 am #13064Cris
ParticipantThanks Tom!
I tried it and it shows all the data in the taxonomy, instead of showing the specific data that corresponds to each entry.$terms = get_terms( ( array('taxonomy' => 'dormitorio', 'hide_empty' => false,))); if ( isset( $terms ) && '' !== $terms ) { foreach( $terms as $term ) { echo '<div class="tax dormitorio">'; echo $term->name; echo '</div>'; }
February 1, 2020 at 5:48 pm #13095Tom
KeymasterWhich specific data are you trying to output?
February 2, 2020 at 2:34 am #13106Cris
ParticipantI have created a type of personalized entrance with several taxonomies as categories (dormitorios, tipo, zona…). When using WPSP, it shows each personalized entry with its categories. The code you provided to show the categories shows all the fields created in that category, instead of showing the specific data of that entry. Look at the attached images:
You can see it live here
February 3, 2020 at 6:13 pm #13138Tom
KeymasterDid you get this sorted? It looks good to me 🙂
February 4, 2020 at 11:24 am #13166Cris
ParticipantThat code doesn’t work properly, Tom. I explain:
Within the “dormitorio” taxonomy I have the values 0, 1, 2, 3 and 4 (these are the room numbers). If an entry has only the value 3 marked, only that value should be shown but it does not do well because it shows all the values 0, 1, 2, 3 and 4. How can it be solved and how can include several taxonomies with diferents classes in this code?
$terms = get_terms( ( array('taxonomy' => 'dormitorio', 'hide_empty' => false,))); if ( isset( $terms ) && '' !== $terms ) { foreach( $terms as $term ) { echo '<div class="tax dormitorio">'; echo $term->name; echo '</div>'; }
February 4, 2020 at 5:13 pm #13175Tom
KeymasterCan you show me exactly what this code outputs?:
$terms = get_terms( ( array('taxonomy' => 'dormitorio', 'hide_empty' => false,))); if ( isset( $terms ) && '' !== $terms ) { foreach( $terms as $term ) { echo '<div class="tax dormitorio ' . $term->slug . '">'; echo $term->name; echo '</div>'; } }
It should output every single term by name attached to the current post.
If it does, we should be able to add a class name to the surrounding div with a class specific to the name.
February 5, 2020 at 1:27 am #13183Cris
ParticipantKeep showing all the taxonomy data, instead of showing the one that corresponds to each entry.
You can see it here
the first one on the left should show only the value 0, and the others the value 3 and 4 respectively. But all the taxonomy values (0, 1, 2, 3, 4) appear in each of the entries.February 6, 2020 at 10:59 am #13219Cris
ParticipantHi Tom, somebody told me that get_the_terms() retrieves the terms of the taxonomy that are attached to the post, but I can’t make it work. To make it works with WPSP, what should I change and where do I add the names of the taxonomies?
function get_the_terms( $post, $taxonomy ) { $post = get_post( $post ); if ( ! $post ) { return false; } $terms = get_object_term_cache( $post->ID, $taxonomy ); if ( false === $terms ) { $terms = wp_get_object_terms( $post->ID, $taxonomy ); if ( ! is_wp_error( $terms ) ) { $term_ids = wp_list_pluck( $terms, 'term_id' ); wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' ); } } /** * Filters the list of terms attached to the given post. * * @since 3.1.0 * * @param WP_Term[]|WP_Error $terms Array of attached terms, or WP_Error on failure. * @param int $post_id Post ID. * @param string $taxonomy Name of the taxonomy. */ $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy ); if ( empty( $terms ) ) { return false; } return $terms; }
February 6, 2020 at 6:07 pm #13231Tom
KeymasterTry this:
$terms = get_the_terms( get_the_ID(), 'dormitorio' ); if ( isset( $terms ) && '' !== $terms ) { foreach( $terms as $term ) { echo '<div class="tax dormitorio ' . $term->slug . '">'; echo $term->name; echo '</div>'; } }
February 7, 2020 at 1:25 am #13245Cris
ParticipantPerfect!!! Thanks a lot Tom !!
-
AuthorPosts
- You must be logged in to reply to this topic.