[英]ACF - Using relationship field to select page
I am currently trying to use the ACF relationship field to select which pages I would like a certain piece of code to run on, for example, if I select Page A, Page B and Page C, I would like the work "hello" to be added. 我当前正在尝试使用ACF关系字段来选择要在其上运行某些代码的页面,例如,如果我选择页面A,页面B和页面C,我希望工作“您好”被添加。
So far, I have the below Relationship field. 到目前为止,我具有以下“关系”字段。
<?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; ?>
My idea was to try and matchthe URL with the url selected, meaning if my URL was http://www.example.com/pagea and I had selected Page A, it would display hello, but unfortunately no luck so far. 我的想法是尝试将URL与选择的URL进行匹配,这意味着如果我的URL是http://www.example.com/pagea并且我选择了Page A,它将显示问候,但不幸的是到目前为止还没有运气。
Does anyone have an idea of another way around this? 有人对此有其他想法吗?
Thanks! 谢谢!
From your question, it looks like this would do the trick: 从您的问题来看,这似乎可以解决问题:
Place this code in your page.php file, or wherever you want your code to run: 将此代码放在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.