Home › Forums › General Issues › Control Display of Custom Field
I’ve created a custom field with the rule Post Type = Widget Block.
In my Widget Block, I created the custom field and it displays perfectly using this template code:
`<?php
$retheading = get_field(‘ret_heading’,163);
$retcont = get_field(‘ret_content’,163);
$leftcont = get_field(‘left_column_content’,163);
$centercont = get_field(‘center_column_content’,163);
$rightcont = get_field(‘right_column_content’,163);
$botcont = get_field(‘bottom_content’,163);
if( !empty($retcont) ): ?>
<section id=”retinal”>
<div class=”container”>
<h3><?php echo $retheading; ?></h3>
<?php echo $retcont; ?>
<div style=”overflow: hidden”>
<div class=”col-sm-4″>
<?php echo $leftcont; ?>
</div>
<div class=”col-sm-4″>
<?php if( !empty($centercont) ): ?>
<img src=”<?php echo $centercont[‘url’]; ?>” />
<?php endif; ?>
</div>
<div class=”col-sm-4″>
<?php echo $rightcont; ?>
</div>
</div>
<?php echo $botcont; ?>
</div>
</section>
<?php endif; ?>`
EXCEPT, I don’t want it to load/display on specific pages. I’ve tried using “Display Widget” to control this, but the custom fields still post. Is there a way to get around this? Or do I have to create a custom template for specific pages to not display this field?
Hi @toad78 ,
Thanks a lot for reaching out to us.
I believe you should be able to add to create an extra location rule such that fields show where post type == widget block and page ==’you pages’.
Let me know if it helps.
Hi @toad78 ,
Thanks a lot for reaching out to us.
I believe you should be able to add to create an extra location rule such that fields show where post type == widget block and page ==’you pages’.
Let me know if it helps.
Well I did try this and the widget block still appears on those pages that share the same template.
Hi @toad78
I think I misunderstood your question. I’m sorry about that.
I believe you can add a post object field and then add the pages you want to exclude. After that, you can check if the current page is excluded or not like this:
// Get the excluded IDs
$excluded = get_field('excluded_pages_field', 163, false);
// Get the current post
global $post;
//Check if the current page is excluded or not
if( $excluded && !in_array($post->ID, $excluded) ){
// Your code here
echo "not excluded";
}
I hope this helps 🙂
Okay, I’ve tried the above using this method but the widget block just disappeared and doesn’t appear on ANY pages.
<?php
$excluded = get_field('excluded_pages_field', 1255, 1509, 1213, 1293, false);
global $post;
if( $excluded && !in_array($post->ID, $excluded) ){
$eventcontent = get_field('event_content', 1368);
$moredetails = get_field('more_details', 1368);
$eventimage = get_field('event_image', 1368);
if(!empty($eventcontent) ): ?>
<section class="boldSection gutter btBottomVertical btParallax">
<div class="specialevent" style="background: linear-gradient(to right, rgba(255,255,255,1) 0%,rgba(255,255,255,1) 16%,rgba(255,255,255,1) 50%,rgba(255,255,255,0.01) 52%,rgba(255,255,255,0) 55%), url(<?php echo $eventimage; ?>); background-size: cover!important; background-repeat: no-repeat!important; background-position: right center!important;">
<div class="col-sm-6">
<header class="header btDash bottomDash">
<div class="dash">
<h2>
<span class="headline">Special Event</span>
</h2>
</div>
</header>
<?php echo $eventcontent; ?>
<a href="<?php echo $moredetails; ?>" class="btBtn btBtn btnOutlineStyle btnAccentColor btnBig btnHalfWidth btnRightPosition btnNoIcon"><span class="btnInnerText">More Details</span></a><a href="about/season-tickets/" class="btBtn btBtn btnOutlineStyle btnAccentColor btnBig btnHalfWidth btnRightPosition btnNoIcon"><span class="btnInnerText">Get Tickets</span></a>
<div class="btClear btSeparator topSemiSpaced noBorder"><hr></div>
</div>
</div>
</section>
<?php endif; ?>
<?php } ?>
Hi @toad78
Please keep in mind that you are not supposed to list the post IDs you want to exclude in the get_field()
function. You should use the post ID where the post object field is assigned (1368 in your last code). Also, please don’t forget to create the excluded_pages_field post object field in your widget field group too.
So, after you set the pages you want to exclude, you should be able to do it like this:
<?php
$excluded = get_field('excluded_pages_field', 1368, false);
global $post;
if( $excluded && !in_array($post->ID, $excluded) ){
$eventcontent = get_field('event_content', 1368);
$moredetails = get_field('more_details', 1368);
$eventimage = get_field('event_image', 1368);
if(!empty($eventcontent) ): ?>
I hope this makes sense 🙂
Thanks for the help, James.
I’ve managed to get it working correctly just as you’ve recommended.
The topic ‘Control Display of Custom Field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.