Forums

Nested foreach arguments (4 posts)

  1. almemedia
    Member
    Posted 4 months ago #

    I have a problem, probably because my bad php coding but I am trying to get the title of a post depending on a custom field.

    <?php if (get_field('matcher') && get_field('matcher') != "") { ?>
    <?php foreach(get_field('matcher') as $matcher): ?>
    <?php foreach(get_post_meta($matcher->ID, vinnare) as $vinnare): ?>
    <?php echo get_the_title($vinnare->ID) ?>
    <?php endforeach; ?>
    <?php endforeach; ?>
    <?php } ?>

    Both matcher and vinnare has the ID of respective post, so if I change $vinnare->ID to $matcher->ID, I get the title of matcher but I need another post linked via a custom field to be displayed. How do I do that???

    Thanks in advance!

  2. vtxyzzy
    Member
    Posted 4 months ago #

    It may not be the cause of the problem, but it looks like this line:

    <?php foreach(get_post_meta($matcher->ID, vinnare) as $vinnare): ?>

    is missing some quotes:

    <?php foreach(get_post_meta($matcher->ID, 'vinnare') as $vinnare): ?>
  3. almemedia
    Member
    Posted 4 months ago #

    Thanks, but that didn't solve it...

  4. vtxyzzy
    Member
    Posted 4 months ago #

    I think the problem is that you used get_field() for $matcher, but get_post_meta() for $vinnare.

    get_field() apparently returns an object, get_post_meta() returns values.

    Please try changing this:

    <?php echo get_the_title($vinnare->ID) ?>

    to this:

    <?php echo get_the_title($vinnare) ?>

Reply

You must log in to post.

About this Topic