WordPress ACF从关联循环中排除私人帖子

huangapple go评论52阅读模式
英文:

WordPress ACF exclude private post from relationship loop

问题

I have a post relationship option in ACF in WooCommerce products. Some products are marked as private. I am able to select published and private products and display it as below.

如何在下面的循环中排除私人帖子?

while ( have_rows( 'similar_product' ) ) {the_row();
	$products = get_sub_field( 'select_product' ); ?>
	<?php if ( $products !== false ) { ?>
        <a href="<?php echo get_the_permalink( $products->ID ) ?>">
			<?php echo get_the_post_thumbnail( $products->ID, 'medium', ); ?>
        </a>
	<?php }
}
echo "</div>";
?>
</div>
<?php } ?>

我尝试在ACF中的选择选项中排除私人帖子,但我需要这个可能性,以防相关产品再次可用。

英文:

I have a post relationship option in ACF in woocommerce products. Some products are marked as private. I am able to select published and private products and display it as below.

How to exclude private posts from loop below?

while ( have_rows( &#39;similar_product&#39; ) ) {the_row();
			$products = get_sub_field( &#39;select_product&#39; ); ?&gt;
			&lt;?php if ( $products !== false ) { ?&gt;
                &lt;a href=&quot;&lt;?php echo get_the_permalink( $products-&gt;ID ) ?&gt;&quot;&gt;
					&lt;?php echo get_the_post_thumbnail( $products-&gt;ID, &#39;medium&#39;, ); ?&gt;
                &lt;/a&gt;
			&lt;?php }
		}
		echo &quot;&lt;/div&gt;&quot;;
		?&gt;
    &lt;/div&gt;
&lt;?php } ?&gt;

I tried to exclude private post from select option in ACF but I need this possibility in case when related product will be available again.

答案1

得分: 0

以下是翻译好的部分:

这篇文章的可见性是在它的 post_status 属性中设置的。检查 &#39;private&#39; === $products-&gt;post_status 将指示文章是否为私密的。建议使用 get_post_status() 辅助函数。

要仅显示已发布的产品,请尝试以下代码(未经测试):

if ( false !== $products &amp;&amp; &#39;publish&#39; === get_post_status( $products) ) {

如果您更喜欢这种方法,您也可以跳过私密文章:

if ( false === $products || &#39;private&#39; === get_post_status( $products ) ) {
    continue;
}
英文:

The post's visibility is set in it's post_status property. Checking &#39;private&#39; === $products-&gt;post_status will indicate if a post is private. Recommend to use get_post_status() helper function.

To only display published products, try this (untested):

if ( false !== $products &amp;&amp; &#39;publish&#39; === get_post_status( $products) ) {

You could also skip private posts, if you prefer that approach:

if ( false === $products || &#39;private&#39; === get_post_status( $products ) ) {
    continue;
}

huangapple
  • 本文由 发表于 2023年6月6日 01:32:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定