WordPress.org

Forums

[resolved] What's the correct syntax for quotes within a php statement? (3 posts)

  1. reneequinn
    Member
    Posted 4 years ago #

    Hi there,

    Hacking my way through PHP - most of the time okay but cannot work this out so thought I'd ask a far greater power!

    I'm trying to echo a field from setup using the advanced custom fields plugin. The query's working fine on another template but I can't get the echo to work and I'm sure it's to do with my 'newbie' status.

    The code I'm getting blurry eyed over is ,,,

    <?php $args = array(
        'numberposts' => 3,
        'offset' => 0,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'publications',
        'meta_query' => array(
                array(
                        'key' => 'project',
                        'value' => $post->ID,
                )
        )
    );
    $posts_array = get_posts( $args );
    if( $posts_array ) {
        foreach( $posts_array as $related ) {
    		echo '<a href="' . $related->guid . '">' . linked words . '</a>';
        }
    }

    I want it to echo a field from the related post rather than the guid:

    echo '<a href="<?php the_field('field_name'); ?>">' . linked words . '</a>';

    But the " and the ' syntax isn't correct - the ' ' around the field_name stuffs up the code.

    There's probably an easy, easy answer but stuffed it I can work it out.

    Any one got any ideas?

    Thanks in advance

  2. luckdragon
    Member
    Posted 4 years ago #

    the positioning of your ' and " are correct, however you are doing <? and ?> within an echo statement, which is already between <? and ?> so that is confusing the situation, not the quotations

    echo '<a href="'.the_field('field_name').'">'.$linked_words.'</a>';
  3. reneequinn
    Member
    Posted 4 years ago #

    Thanks LuckyDragon.

Topic Closed

This topic has been closed to new replies.

About this Topic