How to link a word comprised of a custom field with another?

I want to link content comprised of a custom field(field1_details) using another custom field(field2_link) which is a URL type. the_field('field1_details') is text_area field and get_field('field2_link') is a website field (using a ACF addon).

(These custom fields are created using Advanced custom fields plugin)

Here is the code I am trying in loop.php:

span class="c_f"a href="?php get_field('field2_link'); ?"?php if( function_exists('the_field') ) the_field('field1_details'); ?/a/span

Output-

 a href=""The text content/a ( Doesn't out put the link)

And tried this without the if

span class="c_f"a href="?php get_field('field2_link'); ?"?php the_field('field1_details'); ?/a/span

Trying with the_field instead of get_field

span class="c_f"a href="?php the_field('field2_link'); ?"?php the_field('field1_details'); ?/a/span

Output-

spana href="lt;a href=" http:="" google.com"="" target="_blank"google.com/a"gt;This is the test content/span

=====================================================

The two custom fields works fine if I use them separately like this:

Field 1- field1_details

Output HTML-

div class="field-wrap"span class="field icon-edit"This is the Text
content/span/div

Field 2- field2_link

or without the if

span class="link-field"?php the_field('field2_link'); ?/span

Out put-

 span class="link-field"a href="http://google.com"
 target="_blank"google.com/a/span

I think I am not using it in a right way. Could any one tell me how to link the field with a URL entered in the another field in loop.php?

Also tell me how would you do it in the functions.php instead of the templates like loop.php

Topic php links custom-field Wordpress

Category Web


I think your URL is blank because you are simply not printing the result. You can either store the URL in a variable and echo it later:

<?php $myURL = get_field('field2_link'); ?>
<a href="<?php echo $myURL; ?>">

or echo it directly:

<a href="<?php ECHO get_field('field2_link'); ?>">

Either way you need to use the echo "function" to print the results.


According to the documentation, you should display a field like:

<p><?php the_field('field_name'); ?></p>

Change your get_field to the_field:

<span class="c_f"><a href="<?php the_field('field2_link'); ?>"><?php the_field('field1_details'); ?></a></span>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.