[英]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,它將顯示問候,但不幸的是到目前為止還沒有運氣。
有人對此有其他想法嗎?
謝謝!
從您的問題來看,這似乎可以解決問題:
將此代碼放在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.