skip to Main Content

I am new to wordpress theme developments. I used ACF fields in my wordpress template. But acf fields value aren’t showed if it is not connected to page in admin dashboard.Like search.php file where wordpress redirect search to this page. At search.php, I used a template part called cta.php. It works fine with various page template say service template.
If the url becomes wordpress/post_type="post"&s="battery" then the results appear at search.php where cta.php template part also used. So, how can I set location rule for fields of cta to appear at search.php?

2

Answers


  1. Chosen as BEST ANSWER

    After a while I found a workaround to solve it indirectly. As, I used the template part at Home page, I can then retrieve the value of those field by using Home page id. $home = get_page_by_path("Home"); $id = $home->ID; Now, the_field('cta_title', $id) gives me the value. And I can use a single location rule over there and it will be changed everywhere the template part used.

    <div class="cta-area">
        <div class="container">
            <div class="row cta-content">
                <div class="col-lg-7 col-md-7 col-12 wow fadeInUp" data-wow-delay=".2s">
                <?php $home = get_page_by_path('Home');
                    if($home): ?>
                    <h2><?php the_field('cta_title', $home->ID); ?></h2>
                    <p><?php the_field('cta_intro', $home->ID); ?></p>
                    <?php endif; ?>
                </div>
                <div class="col-lg-5 col-md-5 col-12 text-center wow fadeInUp" data-wow-delay=".3s">
                    <a href="<?php $call = get_page_by_path('Call'); echo $call ? get_permalink($call->ID): '#';?>" class="main-btn">Get a Call</a>
                </div>
            </div>
        </div>
    </div>
    

  2. There is no interface to edit the search results page, so an ACF option page will need to be created/used and that location also set for the field group. The cta.php template part will then need to check if is_search() is true and change where it retrieves ACF data from (untested):

    cta.php:

    $acf_post_id = get_queried_object_id();
    
    if ( is_search() ) {
        $acf_post_id = 'option';
    }
    
    the_field( 'cta_title', $acf_post_id );
    

    Or, if you need to be more explicit:

    $title = get_field( 'cta_title' );
    
    if ( is_search() ) {
        $title = get_field( 'default_cta_title', 'option' );
    }
    
    echo $title;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search