繁体   English   中英

ACF-使用关系字段来选择页面

[英]ACF - Using relationship field to select page

我当前正在尝试使用ACF关系字段来选择要在其上运行某些代码的页面,例如,如果我选择页面A,页面B和页面C,我希望工作“您好”被添加。

到目前为止,我具有以下“关系”字段。

        <?php
$posts = get_field('which_venue', options);

if( $posts ): ?>
    <ul>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
          <?php
          $thisurl = the_permalink(); 
          // echo $thisurl;             

            $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
            if($url == $thisurl) {
    echo "match";
}
?>
        </li>
    <?php endforeach; ?>
    </ul>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?> 

我的想法是尝试将URL与选择的URL进行匹配,这意味着如果我的URL是http://www.example.com/pagea并且我选择了Page A,它将显示问候,但不幸的是到目前为止还没有运气。

有人对此有其他想法吗?

谢谢!

从您的问题来看,这似乎可以解决问题:

  1. 更改您的关系字段以保存帖子ID而不是帖子对象。
  2. 更改后,转到选项页面,然后重新保存字段数据。
  3. 将此代码放在page.php文件中,或您希望运行代码的任何地方:

     <?php //Check if this page is selected in which_venue //Get the selected fields $selectedPages = get_field('which_venue', options); //Get the current page's ID $myID = get_the_ID(); //Check if the current page's ID exists inside the selected fields array. if(in_array($myID, $selectedPages)){ //Run your code echo 'This Page was selected in "which_venue".'; }else{ //Run other code echo 'This Page was NOT selected in "which_venue".'; } ?> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM