I’m looking for a way to transform a static piece of code into a widget to display in different sidebars containing a simple internal link.
The basic code is:
<div class="my-link-box">
<a href="<?php echo get_page_link(90); ?>">My link text</a>
</div>
The main focus is to make it future proof so no matter the site url it should be working without a search and replace or other manual intervention.
I would like to avoid:
- php in widget
- hardcoded link
Maybe a widget specific shortcode?
Any help is much appreciated 🙂
The page ID can vary in different installation, and it can’t be changed, so an option is to use
get_page_by_path
because the page slug can be easily changed:However this is not a great solution as well.
Once you are writing a custom widget, you can setup the option to select the page whose to show the link. e.g.:
However, if your widget contain only links to page(s), consider to create a custom menu and use the “Custom Menu” widget.