[Resolved] Custom Fields

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Custom Fields

Tagged: 

Viewing 15 posts - 16 through 30 (of 37 total)
  • Author
    Posts
  • #13000
    Cris
    Participant

    Sorry, 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 plugin

    Thanks!

    • This reply was modified 6 months, 1 week ago by Cris. Reason: Expand the information
    #13016
    Tom
    Keymaster

    To 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>';
        }
    } );
    #13033
    Cris
    Participant

    Thanks 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?

    #13038
    Gwen
    Participant

    Just 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.)

    #13058
    Tom
    Keymaster

    You’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>';
            }
        }
    #13064
    Cris
    Participant

    Thanks 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>';
        }
    #13095
    Tom
    Keymaster

    Which specific data are you trying to output?

    #13106
    Cris
    Participant

    I 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:

    taxonomies
    shows all created data for dormitorios, when it should show only the dormitorios: 3

    You can see it live here

    • This reply was modified 6 months ago by Cris.
    • This reply was modified 6 months ago by Cris.
    #13138
    Tom
    Keymaster

    Did you get this sorted? It looks good to me 🙂

    #13166
    Cris
    Participant

    That 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>';
        }
    #13175
    Tom
    Keymaster

    Can 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.

    #13183
    Cris
    Participant

    Keep 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.

    • This reply was modified 6 months ago by Cris.
    • This reply was modified 6 months ago by Cris.
    • This reply was modified 6 months ago by Cris.
    #13219
    Cris
    Participant

    Hi 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;
    }
    #13231
    Tom
    Keymaster

    Try 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>';
            }
        }
    #13245
    Cris
    Participant

    Perfect!!! Thanks a lot Tom !!

Viewing 15 posts - 16 through 30 (of 37 total)
  • You must be logged in to reply to this topic.