英文:
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( '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 } ?>
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
属性中设置的。检查 'private' === $products->post_status
将指示文章是否为私密的。建议使用 get_post_status()
辅助函数。
要仅显示已发布的产品,请尝试以下代码(未经测试):
if ( false !== $products && 'publish' === get_post_status( $products) ) {
如果您更喜欢这种方法,您也可以跳过私密文章:
if ( false === $products || 'private' === get_post_status( $products ) ) {
continue;
}
英文:
The post's visibility is set in it's post_status
property. Checking 'private' === $products->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 && 'publish' === get_post_status( $products) ) {
You could also skip private posts, if you prefer that approach:
if ( false === $products || 'private' === get_post_status( $products ) ) {
continue;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论