Support

Account

Home Forums Front-end Issues Merge an ACF URL and Text field to create a href

Solved

Merge an ACF URL and Text field to create a href

  • Hi

    I am looking to display a block on a web page the content of which will be a news story headline, date and publisher. It’s effectively an aggregated news ticker, the content of which I will curate through posts.

    There will be several blocks on a page and the fields they display will be either category or category/tag related with the whole process being driven by the following conditional php statement –

     <?php
    
    $hb1 = in_category( 
               array('News','Features','Tech'), 
               $this->post->ID 
    );
    $hb2 = in_category( array('Research'), $this->post->ID );
    $hb3 = has_tag( 
             array( 'aged care', 'anxiety/stress', 'autism', 'cognitive', 'Dementia', "Parkinson's", 'sleep' , 'well-being' ), 
             $this->post->ID
    );
    
    if ( $hb1 )
    {
      the_field('media_outlet',     $this->post->ID);
     the_field('media_outlet_url', $this->post->ID);
     the_field('country',   $this->post->ID);
    }
    else if ( $hb2 && $hb3 )
    {
       the_field('media_outlet',   $this->post->ID);
       the_field('published_date', $this->post->ID);
       the_field('country',        $this->post->ID);
       echo $this->get_category();
      
    }
    ?>

    What I would like to do is combine the ACF media-outlet and media_outlet_url so that the media outlet name is displayed on the web page but behind it sits a URL that when clicked opens another browser page and takes the user to the relevant site.

    I have tried a number of code versions but each crashes my Newspaper theme. The latest version being –

    <?php
    
    $hb1 = in_category( 
               array('News','Features','Tech'), 
               $this->post->ID 
    );
    $hb2 = in_category( array('Research'), $this->post->ID );
    $hb3 = has_tag( 
             array( 'aged care', 'anxiety/stress', 'autism', 'cognitive', 'Dementia', "Parkinson's", 'sleep' , 'well-being' ), 
             $this->post->ID
    );
    $link = get_field('media_outlet_url');
    
    if ( $hb1 )
    {
       <a href="<?php echo $link; ?>">the_field('media_outlet',$this->post->ID);</a>;
       the_field('published_date',   $this->post->ID);
       the_field('country',   $this->post->ID);
    }
    else if ( $hb2 && $hb3 )
    {
       the_field('media_outlet',   $this->post->ID);
       the_field('published_date', $this->post->ID);
       the_field('country',   $this->post->ID);
      
    }
    ?>
    </div>

    I created an $link variable that was called a few lines down but didn’t work.

    All and any insight appreciated.

    Best Wishes

    Gary

  • 
    $link = get_field('media_outlet_url', $this->post->ID);
    
    if ( $hb1 )
    {
       ?>
        <a href="<php echo $link; ?>" target="_blank"><?php 
            the_field('media_outlet',$this->post->ID); ?</a>
       <?php
      /// code continues ...
    
  • Hi John

    Thanks for getting back to me. I have tried to implement your suggestion but seem to be missing something – I am newish to php so my skills are improving daily but clearly not good enough to identify the problem.

    I implemented this off the back of your suggestion –

    <?php
    
    $hb1 = in_category( 
               array('News','Features','Tech'), 
               $this->post->ID 
    );
    
    $hb2 = in_category( array('Research'), $this->post->ID );
    $hb3 = has_tag( 
             array( 'aged care', 'anxiety/stress', 'autism', 'cognitive', 'Dementia', "Parkinson's", 'sleep' , 'well-being' ), 
             $this->post->ID
    );
    
    $link = get_field('media_outlet_url', $this->post->ID);
    
    if ( $hb1 )
    {
     ?>
        <a href="<php echo $link; ?>" target="_blank"><?php the_field('media_outlet',$this->post->ID); ?</a>
       
    <?php
    
       the_field('published_date',   $this->post->ID);
       the_field('country',   $this->post->ID);
    }
    
    else if ( $hb2 && $hb3 )
    
    {
       the_field('media_outlet',   $this->post->ID);
       the_field('published_date', $this->post->ID);
       the_field('country',   $this->post->ID);
      
    }
    ?>

    But get an error.

    Any help appreciated.

    Thanks.

    Best

    G

  • Hi John

    I’ve now got this to work –

    <?php
    
    $hb1 = in_category( 
               array('News','Features','Tech'), 
               $this->post->ID 
    );
    
    $hb2 = in_category( array('Research'), $this->post->ID );
    $hb3 = has_tag( 
             array( 'aged care', 'anxiety/stress', 'autism', 'cognitive', 'Dementia', "Parkinson's", 'sleep' , 'well-being' ), $this->post->ID
    );
    
    if ( $hb1 )
    {
    $link = get_field('media_outlet_url', $this->post->ID);
    $media = get_field ('media_outlet', $this->post->ID);
    $country = get_field ('country', $this->post->ID);
    $publicationdate = get_field ('publication_date', $this->post->ID);
    
    echo "<a href=$link target="_blank">$media</a>";
    echo $publicationdate;
    echo $country; 
    }
    
    else if ( $hb2 && $hb3 )
    
    {
       the_field('media_outlet',   $this->post->ID);
       the_field('published_date', $this->post->ID);
       the_field('country',        $this->post->ID);
    }
    
    ?>

    Just trying to work out how to get the link to open in a blank page.

    Best

    G

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.