Support

Account

Home Forums General Issues the_field is not displaying

Solving

the_field is not displaying

  • example of code

                <?php
    
    $term = $_GET['s'];
    $expTerm = explode(" ",$term);
    
    $search = "(";
    foreach($expTerm as $ek=>$ev)
    {
    if($ek == 0)
    {
    $search .= "post_title LIKE '%".$ev."%'";
    }
    else
    {
    $search .= " OR post_title LIKE '%".$ev."%'";
    }
    }
    $search .= ")";
    
    $query = $wpdb->get_results(" SELECT * FROM ".$wpdb->prefix."posts WHERE post_status='publish' and $search ");
    ?>
                <?php
    
    foreach($query as $qk=>$qv)
    {
    $link = get_permalink($qv->ID);
    
    ?>
                <div class="col-12">
                    <div class="card stacked">
                        <div class="card-body">
                            <div class="row align-items-center">
                                <div class="col-md-2 text-center">
                                    <i
                                        class="icon-<?php the_field('category_icon', $qv->ID);?> icon-boxed icon-boxed-lg bg-blue text-white"></i>
                                </div>
                                <div class="col">
                                    <h3 class="card-title mt-2 mt-md-0 text-center text-md-left mb-2">
                                        <?php print $qv->post_title; ?>
                                    </h3>
                                    <div class="row">
                                        <div class="col-12">
                                            <p> <?php print wp_trim_words($qv->post_content,50,'...'); ?>
                                            </p>
                                        </div>
                                        <div class="mt-2 col-12">
                                            <a class="text-blue eyebrow action" href="<?php print $link;?>">Continue
                                                Reading</a>
                                        </div>
    
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
    
                <?php
    }
    ?>
  • I am unclear about what you are getting here

    
    the_field('category_icon', $qv->ID);
    

    I ask because the field name is category_icon. Is this a field that is added to a post or a category?

  • The field is added to a custom taxonomy. Every post will have only one custom taxonomy term applied to it but there are a few options of custom taxonomy that would be applied based on the post type.

  • You need to get the terms of the post which will return an array (even if there is only one). The you need to get the field from the term.

    
    $terms = wp_get_object_terms($qv->ID, 'TAX NAME');
    the_field('category_icon', $terms[0];
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.