简体   繁体   English

如果第一个ACF Repeater字段为空,则打印内容

[英]Print content if first ACF Repeater field is empty

I'm trying to make a simple document management system with ACF's repeater field. 我正在尝试使用ACF的Repeater字段创建一个简单的文档管理系统。 I need to print a button to download a file attached to the the top repeater field (with the size and filetype of the download). 我需要打印一个按钮来下载附加到顶部转发器字段的文件(以及下载的大小和文件类型)。 But if the top repeater field is empty, it should print “file not available” content. 但是,如果顶部转发器字段为空,则应打印“文件不可用”内容。

I'm pretty new to PHP but this mostly works so far: 我对PHP还是很陌生,但是到目前为止,大多数方法都可以使用:

$repeater = get_field( 'document' )[0];

    if( $repeater ) {

        $fileurl = $repeater[ 'document' ][ 'url' ];
        $filesize = filesize( get_attached_file ($repeater[ 'file' ][ 'id' ]) );
        $filesize = size_format($filesize);
        $filetype = wp_check_filetype( get_attached_file ($repeater[ 'file' ][ 'id' ]));

        $download = '<div><a href="' . $repeater[ 'file' ][ 'url' ] . '">Download</a><div>' . $filesize . ' <span>' . $filetype[ 'ext' ] .'</span></div></div>' ;

                echo $download;
        }

This prints a button to the attached file in the top repeater, when there is an attached file in the top repeater. 当顶部转发器中有附件时,这会将按钮打印到顶部转发器中的附件。 Only it prints out a dead link if there is nothing in the top repeater. 如果顶部转发器中没有任何内容,则仅打印出无效链接。 This won't do. 这不会。 I need to add an else condition or something so that it prints "file not available" content if there is nothing in the first repeater. 我需要添加一个else条件或其他条件,以便在第一个转发器中没有内容的情况下打印“文件不可用”内容。

    if(empty( $repeater )) {

        $unavailable = '<div>Unavailable<div>This document isn\'t ready yet. Please check back later.</div></div>' ;

                echo $unavailable;
        }

I've tried a lot of different ways to do this, such as above, and I don't know what I'm doing wrong. 我已经尝试了许多不同的方法来执行此操作,例如以上所述,但我不知道我做错了什么。 Can you help? 你能帮我吗?

You have to check for a value before displaying field like that : 在显示类似这样的字段之前,您必须检查一个值:

if( get_field('document'){
    ... // there is an attached file
}
else {
    $unavailable = '<div>Unavailable<div>This document isn\'t ready yet. Please check back later.</div></div>' ;
    echo $unavailable;
}

I finally got it! 我终于明白了! I think I wasn't declaring my variables clearly enough. 我认为我没有足够清楚地声明变量。

$row = get_field( 'document' );
$first_row = $row[0];
$first_row_file = $first_row[ 'file' ];

if( $first_row_file ) :

            $download = '<div>Available!<div>This document is ready for download.</div></div>' ;

                    echo $download;

else :

            $unavailable = '<div>Unavailable!<div>This document isn\'t ready yet. Please check back later.</div></div>' ;

                    echo $unavailable;

endif;

Now I can add more sophisticated content (like a download button) to display when there is a file to download, and a helpful message when there isn't. 现在,我可以添加更复杂的内容(例如下载按钮),以在有文件下载时显示,而在没有文件时显示有用的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM