Php 如果选中了ACF子字段,则隐藏行
我有一个ACF收割机领域与一对夫妇的行,我试图显示。但是,如果选中了复选框(复选框是中继器中的一个子字段),我只希望显示行。我正试图通过使用if in_数组来实现这一点,如“条件逻辑”中所述:Php 如果选中了ACF子字段,则隐藏行,php,wordpress,if-statement,checkbox,advanced-custom-fields,Php,Wordpress,If Statement,Checkbox,Advanced Custom Fields,我有一个ACF收割机领域与一对夫妇的行,我试图显示。但是,如果选中了复选框(复选框是中继器中的一个子字段),我只希望显示行。我正试图通过使用if in_数组来实现这一点,如“条件逻辑”中所述: if(在数组(“bestyrelsevalg”中,获取子字段('bestyrelse')) 我将结果输出到WordPress短代码中。现在,我的代码可以工作了,除了它在repeater字段中显示所有结果(也包括未选中的结果)。我错过了什么 我的代码: function investor_bestyrels
if(在数组(“bestyrelsevalg”中,获取子字段('bestyrelse'))
我将结果输出到WordPress短代码中。现在,我的代码可以工作了,除了它在repeater字段中显示所有结果(也包括未选中的结果)。我错过了什么
我的代码:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo get_field( 'upload_dokument' );
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
function investor\u bestyrelse\u shortcode(){
$rows=get_字段('budgetter_og_nyhedsbreve');
如果($行){
echo'
”;
}
}
添加_短代码('investor_bestyrelse'、'investor_bestyrelse_短代码');
您不能在foreach循环中使用get\u sub\u field(),您需要使用have\u rows-while-loop或从关联数组访问它:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo $row['upload_dokument'];
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
function investor\u bestyrelse\u shortcode(){
$rows=get_字段('budgetter_og_nyhedsbreve');
如果($行){
echo'
”;
}
}
添加_短代码('investor_bestyrelse'、'investor_bestyrelse_短代码');
在@Maggiator的回答的帮助下,成功地解决了这个问题。由于某种原因,echo引起了问题。我必须使用return insted:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
$content = '<ul class="dokumenter">';
foreach( $rows as $row ) {
if( !in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$pdf = $row['upload_dokument'];
$content = $content . '<li>' . $pdf . '</li>';
}
}
}
$content = $content . '</ul>';
return $content;
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
function investor\u bestyrelse\u shortcode(){
$rows=get_字段('budgetter_og_nyhedsbreve');
如果($行){
$content='';
foreach($行作为$行){
if(!in_数组(“bestyrelsevalg”,$row['bestyrelse'])){
$pdf=$row['upload_dokument'];
$content=$content.“- ”.$pdf.
”;
}
}
}
$content=$content.“
”;
返回$content;
}
添加_短代码('investor_bestyrelse'、'investor_bestyrelse_短代码');
谢谢!这是有道理的。但是,仍然相同:显示repeater字段中的所有结果(以及未选中的结果)。顺便说一句,代码中有语法错误。add_shortcode()结尾缺少分号;好的,您可以运行var_dump($row);在循环开始时,要查看数组的外观,可能是输入错误或类似的错误。var_dump运气不佳-不确定是否正确,因为它在循环中。我还阅读了ACF中继器文档,看到他们没有使用get_字段作为中继器字段,而是使用have_row()也许我应该修改一下代码?文档:foreach很好,可以工作-另一个问题可能是短代码不能在您想要的帖子上运行。您可以选中echo以获取您的id();如果是正确的帖子。如果没有,则需要将postId传递到get_字段('key',$postId);功能。谢谢你的帮助,@maggiathor。我确实找到了解决方案,并在这个帖子中发布了答案:)