Marian Rick Marian Rick - 5 months ago 12
PHP Question

how to insert php var in code

I have got this working code using

wordpress
and
advanced custom fields
:

$include = get_pages('include=1575');
$title = apply_filters('the_title',$include[0]->post_title);
$content = apply_filters('the_content',$include[0]->post_content);


I want to create a
var
replacing the
post_id
in
include=1575
. I get this var using:

$site_id = the_field('id_grundgedanke'); // output exp: 1575


How I can I combine these? I have tried the following:

$site_id = the_field('id_grundgedanke');
$include = get_pages('include=' . $site_id);
$title = apply_filters('the_title',$include[0]->post_title);
$content = apply_filters('the_content',$include[0]->post_content);


Which does not work.

Answer

Use get_field() instead of the_field().

get_field() - Returns the value of the specified field.

the_field() - Displays the value of the specified field. This function is the same as echo get_field($field_name);

Corrected code:

 $site_id = get_field('id_grundgedanke'); // use get_field
 $include = get_pages("include=" . $site_id);
 $title = apply_filters('the_title',$include[0]->post_title);
 $content = apply_filters('the_content',$include[0]->post_content);