【问题标题】:Php inside php to process custom field in wordpressphp里面的php处理wordpress中的自定义字段
【发布时间】:2016-11-20 12:18:16
【问题描述】:

我有一个代码,它返回带有一组图像的画廊编号 1。

<?php echo photo_gallery(1); ?>

但我需要使用此代码在每个帖子中使用自定义字段将数字“1”更改为数字“2”、“3”等等

<?php the_field('number'); ?>

在第一个代码中

我需要这样的东西:

<?php echo photo_gallery(   <?php the_field('number'); ?>   ); ?>

结果:

<?php echo photo_gallery(2); ?>

<?php echo photo_gallery(3); ?>

【问题讨论】:

  • 请传递 the_field('images') 返回的内容,无需再次打开 php 标签,只需将 the_field('images') 存储在变量中并传入函数内部即可。 @user3369654
  • 你想达到什么目的?
  • 请再看问题

标签: php wordpress


【解决方案1】:

这样的?

<?php
echo photo_gallery(the_field('images'));
?>

【讨论】:

  • 结果是“1”...不是照片。
  • 函数photo_gallery() 期望什么?数组?什么?
  • photo_gallery(1) photo_gallery(2) photo_gallery(3) photo_gallery(4) ... 返回插件创建的图片库。使用代码 the_field('number') 我可以在每个帖子中使用自定义字段来更改数字 (1,,2,3...)
  • 好的,所以如果photo_gallery() 期待一个数字,例如1,那么the_field("images") 返回什么......?
  • ?只运行the_field("images") 会返回什么?
【解决方案2】:

您不需要嵌套的 PHP 标记。省略里面的 php 标签,你的代码应该可以运行了。

<?php 
echo photo_gallery(get_field('number'));
?>

注意get_fieldthe_field 之间的区别。 ACF 文档解释了在哪种情况下使用哪个。

本质上,内部(右侧)的函数首先执行,返回一个可供外部函数(左侧)使用的值。

在后台,编程语言采取以下步骤:

  1. 执行get_field('one')return 1
  2. 执行photo_gallery(1)return (photo)
  3. echo (photo)

有一些关于 PHP 的课程,其中许多是免费的 - 这可能会帮助您解决其中的一些问题。

【讨论】:

  • 但它只返回“1”并且页面停止加载。
  • 作品 作品 不工作
  • 我更新了-the_field,根据文档,回声。你想要get_field。
  • 非常感谢。现在一切正常。你能告诉我怎么做吗:如果“数字”为空字段,则不处理 photo_gallery()
猜你喜欢
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
相关资源
最近更新 更多