将ACF字段的位置设置为search.php中使用的模板部分

sq1bmfud  于 11个月前  发布在  PHP
关注(0)|答案(2)|浏览(61)

我是新的WordPress主题的发展。我在我的WordPress模板中使用了ACF字段。但是acf字段的值不会显示,如果它是不连接到页面在管理 Jmeter 板。像search.php文件,其中wordpress重定向搜索到这个页面。在search.php中,我使用了一个名为cta.php的模板部分。它可以很好地与各种页面模板说服务模板。
如果URL变为wordpress/post_type=“post”&s=“battery”,则结果将出现在search.php,其中还使用了cta.php模板部分。那么,如何设置cta字段的位置规则以显示在search.php上?

l5tcr1uw

l5tcr1uw1#

没有用于编辑搜索结果页面的界面,因此需要创建/使用ACF option page,并且还需要为字段组设置该位置。然后,cta.php模板部分将需要检查is_search()是否为true,并更改从何处检索ACF数据(未测试):
cta.php

$acf_post_id = get_queried_object_id();

if ( is_search() ) {
    $acf_post_id = 'option';
}

the_field( 'cta_title', $acf_post_id );

或者,如果你需要更明确:

$title = get_field( 'cta_title' );

if ( is_search() ) {
    $title = get_field( 'default_cta_title', 'option' );
}

echo $title;
2sbarzqh

2sbarzqh2#

过了一段时间,我找到了一个间接解决它的方法。由于我在主页上使用了模板部分,因此我可以通过使用主页ID检索这些字段的值。$home = get_page_by_path("Home"); $id = $home->ID;现在,the_field('cta_title', $id)给出了值。我可以在那里使用一个位置规则,它将在模板部分使用的任何地方进行更改。

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

相关问题