Php inside php to process custom field in wordpres

2019-12-16 21:40发布

I have a code which returns gallery number 1 with the set of images.

<?php echo photo_gallery(1); ?>

But I need to change number "1" to number "2", "3" and so on with custom fields in every post using this code

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

inside the first code

I need something like this:

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

Result:

<?php echo photo_gallery(2); ?>

or

<?php echo photo_gallery(3); ?>

标签: php wordpress
2条回答
再贱就再见
2楼-- · 2019-12-16 22:03

Something like this?

<?php
echo photo_gallery(the_field('images'));
?>
查看更多
你好瞎i
3楼-- · 2019-12-16 22:03

You don't need nested PHP tags. Omit the php tags inside and your code should run.

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

Note the difference between get_field and the_field. ACF docs explain which to use in which situation.

Essentially, the functions inside (to the right) get executed first, returning a value which can be used by the outer function (to the left).

In the background, a programming language takes these steps:

  1. execute get_field('one') and return 1.
  2. execute photo_gallery(1) and return (photo).
  3. echo (photo)

There are some courses on PHP, many of which are free - that might help you sort through some of these issues.

查看更多
登录 后发表回答