简体   繁体   中英

Issue using the function home_url() with an anchor

I am trying to use an anchor in the function home_url() of WordPress. The idea is simple: the user is browsing on the homepage, he clicks on the link and he is redirected to an anchor 'target' in the page 'cabinet-dentaire'.

When I write this code:

<a href="'esc_url( home_url( '/cabinet-dentaire/' ) )'">En savoir plus</a>

It is working great, and the user is redirected to websitename/cabinet-dentaire/

But when I'm writing this in order to add the anchor:

<a href="'esc_url( home_url( '/cabinet-dentaire/#target' ) )'">En savoir plus</a>

The result is websitename/cabinet-dentaire/#target'))'

I don't know why there is written '))' at the end as it normally comes from the code.

Your HTML lacks the required opening <?php and closing ?> PHP tags, so the PHP functions in your code will not be processed correctly.

You should use the following code:

<a href="<?php echo esc_url( home_url( '/cabinet-dentaire' ) ); ?>#target">En savoir plus</a>

Some of the things that have been changed:

  1. It includes the required <?php ... ?> opening/closing tags
  2. It includes the echo required to print the values on screen. The esc_url() functions returns code rather than printing directly to screen
  3. The #target shouldn't be put inside the home_url() function.
  4. Removed the extra ' apostrophes in the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM