PHP 中的 Wordpress PHP [英] Wordpress PHP within PHP

查看:17
本文介绍了PHP 中的 Wordpress PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现在 WordPress 中结合两个插件的结果.

I am trying to achieve an outcome that combines two plugins in WordPress.

基本上,我使用的是 Easing Slider Pro 和高级自定义字段.当网站所有者编辑页面时,我希望他们能够通过将幻灯片 ID 输入到名为slider"的高级自定义字段中来添加幻灯片.

Basically, I am using Easing Slider Pro and Advanced Custom Fields. When the website owner edits a page, I want them to be able to add a slideshow by simply entering the slideshow ID into an Advanced Custom Field called 'slider'.

这是通常添加 PHP 以显示幻灯片的方式:

This is how one would normally add the PHP to display a slideshow:

<?php if ( function_exists('easingsliderpro') ) { easingsliderpro( 5 ); } ?>

5 是可以更改的幻灯片 ID 示例.

The 5 is an example of a slideshow ID that can be changed.

这是高级自定义字段的 PHP:

Here is the PHP for the advanced custom field:

<?php if( get_field('slider') ): ?><?php the_field('slider'); ?><?php endif; ?>

这两个都可以正常工作.但我想要一种将这两段代码结合起来的方法,以便在页面编辑器中网站管理员只需输入幻灯片的 ID.我对 PHP 了解不多,经常被它弄糊涂,但这是我的初步尝试:

Both of these work fine by themselves. But I want a way to combine these two pieces of code so that in the page editor the website manager only has to enter the ID of the slideshow. I don't know a lot about PHP and I am often confused by it, but this was my initial attempt:

<?php if( get_field('slider') ): ?>
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) { easingsliderpro( <?php the_field('slider'); ?> ); } ?></div>
<?php endif; ?>

它没有用,我假设是因为不允许在 PHP 代码中包含 PHP 代码.是否有任何人知道可以实现这一目标的解决方法?

It didn't work, I am assuming because you're not allowed to have PHP code within PHP code. Is there any workaround that anyone knows that could make this achievable?

非常感谢.

推荐答案

我疯了吗?你不能只是:

啊哈!

我想我看到了混乱:the_field 回显值,所以它被传递给 easingsliderpro() 就像 true,并显示值.

I think I see the confusion: the_field echoes the value out, so it gets passed to easingsliderpro() as just true, and displays the value.

您需要使用一个返回值的函数,以便您可以将其传递给下一个函数.

You need to use a function that returns the value, so you can pass it to the next function.

在这种情况下,它是 get_field():

In this case, it's get_field():

<?php if( get_field('slider') ): ?>
    <div id="sliderframe">
        <?php 
            if ( function_exists('easingsliderpro') ) :
                easingsliderpro( get_field('slider') );
            endif;
        ?>
    </div>
<?php endif; ?>

<?php endif;?>

See more in the documentation: http://www.advancedcustomfields.com/resources/functions/get_field/

这篇关于PHP 中的 Wordpress PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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