PHP if / else get_field()与WordPress [英] PHP if/else get_field() with WordPress

查看:83
本文介绍了PHP if / else get_field()与WordPress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在WP后端中为客户提供一些选项,以自定义显示在其网站首页上的轮播。我正在使用高级自定义字段来处理从客户端获取输入的问题。我想给他们两个选择:

I'm trying to give a client some options in the WP backend to customize a carousel that is displayed on their website's homepage. I am using Advanced Custom Fields to handle getting the input from the client. I want to give them two options:

选择#1)允许客户端插入要显示的文本字符串('carousel_title_text')

Option #1) Allows the client to insert a string of text to be displayed ('carousel_title_text')

选项#2)允许客户端上载要显示的徽标( carousel_logo)

Option #2) Allows the client to upload a logo to be displayed ('carousel_logo')

我要检查代码标题文字,如果没有,则显示徽标。我还没有决定如果两个字段都为空该怎么办。无论如何,这就是我想出的:

I want the code to check for title text, and if there is none, display the logo instead. I haven't yet decided what will happen if both fields are empty. Anyway, here is what I've come up with:

<?
if( get_field('carousel_title_text') ) { ?>
  <h2 class="promo"><?php the_field('carousel_title_text');?></h2>
<? } elseif( get_field('carousel_logo') ) {
  the_field('carousel_logo');
}
?>

只要有输入,就会显示 carousel_title_text,但是当它为空时显示为 carousel_logo不是,它不能正确输出徽标。谁能告诉我我在做错什么,还是有更好的方法?

The 'carousel_title_text' displays as long as there is an input, but when it's empty and 'carousel_logo' is not, it does not properly output the logo. Can anyone tell me what I'm doing wrong or if there's a better approach?

在此先感谢

推荐答案

这听起来与徽标的设置有关,我假设徽标是图像字段?在Wordpress管理员中,该字段下会有一个名为返回值的设置,可以将其设置为数组或 URL。

This sounds like it's to do with the settings for the logo, which I'm assuming is an image field? In the Wordpress admin there will be a setting under that field called 'Return value' which can be set to either 'Array' or 'URL'.

如果将其设置为数组,则需要将该数组存储到变量中,然后按如下所示在标记中访问其属性:

If this is set to 'Array', you'd need to store that array to a variable and then access its properties in your markup like so:

<?php

    if ( get_field('carousel_title_text') ) { ?>
        <h2 class="promo"><?php the_field('carousel_title_text'); ?></h2>
    <?php } else{
        $carousel_logo = get_field('carousel_logo');
        if ( $carousel_logo ) { ?>
            <img src="<?php echo $carousel_logo['url']; ?>" />
        <?php } else { ?>
            <h2 class="promo">Default slider title </h2>
        <?php }
    }

?>

或者,您可以将字段设置为仅返回图像URL,然后像这样访问它:

Alternatively, you could set your field to only return the image URL and then access it like so:

<?php

    if ( get_field('carousel_title_text' ) { ?>
        <h2 class="promo"><?php the_field('carousel_title_text'); ?></h2>
    <?php } elseif ( $carousel_logo ) { ?>
        <img src="<?php the_field('carousel_logo'); ?>" />
    <?php } else { ?>
        <h2 class="promo">Default slider title </h2>
    <?php }

?>

ACF在图像字段上的文档将提供更多信息。

这篇关于PHP if / else get_field()与WordPress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆