Documentation:UBC Content Management System/Shortcodes/link with content

From UBC Wiki
Jump to: navigation, search

Intro

As of WordPress 4.2.3, the shortcodes API changed meaning that nested shortcodes (i.e. loop, tabs, accordion) could not contain shortcodes within html attributes. That means that code such as the following, no longer produced the expected result;

[loop query="posts_per_page=5"]
<a href="[the_permalink]" title="[the_title]">[the_title]</a>
[/loop]

The nested [the_permalink] and [the_title] shortcodes do not get parsed. They simply are left as an empty string.

WordPress made this change for security reasons and sadly, this was a breaking change.

[link_with_content]

[link_with_content] is equivalent to <a href="[the_permalink]" title="[the_title]">[the_title]</a>

Parameters

[link_with_content] can take the following parameters;

  • link - defaults to the permalink of the current post (i.e. in a loop it is get_the_permalink()) but you can specify any link
  • content - defaults to the title of the current post (i.e. in a loop it is get_the_title()) but you can specify any content (html is allowed - the same html you can use in the post editor)
  • content_is_cf - defaults blank to a blank string. If set it will overwrite the the_title() with the value of the custom field with this name (i.e. content_is_cf="field_name" will fetch the value of the custom field named "field_name")
  • content_is_excerpt - defaults to a blank string. If specify with "true" or "1", i.e. content_is_excerpt="true" or content_is_excerpt="1" will use excerpt as link content. It also overrides other content fields listed above, content_is_cf and content.
  • link_class - defaults to a blank string. This is the class attribute of the anchor tag
  • link_before - defaults to a blank string. This is any content you wish to be placed before the link
  • link_after - defaults to a blank string. This is any content you wish to be placed after the link
  • link_target - defaults to _self. i.e. use _blank to open a new tab when someone clicks the link
  • link_is_id - defaults to a blank string. If this is set to a non-empty string then the link will be the ID of the current post in the loop (you *must* use link_prefix too i.e. set that to '#')
  • link_is_cf - defaults to a blank string. If this is set to a non-empty string then the link will be the value of the custom field with this name (i.e. link_is_cf="field_name" will fetch the value of the custom field named "field_name")
  • link_prefix - defaults to an empty string. If this is set to a non-empty string then the URL will be prefixed with whatever is placed here. (i.e. if used with link_is_id, then link_prefix='#' will output a link with a URL such as href="#123" where 123 is the post ID of the post)
  • after_url - defaults to a blank string. This will be added to end of URL (i.e. after_url="/#respond" will link to post's Leave a Reply section).

Examples

[loop query="posts_per_page=5"]
[link_with_content] 
[/loop]
[loop query="posts_per_page=5"]
[link_with_content link="google.com" content="Goes to google" link_class="class-1 class-2" link_before="Before the link" link_after="After the link" link_target="_blank"] 
[/loop]
[loop query="posts_per_page=5"]
[link_with_content link_is_id='yes' link_prefix='#'] 
[/loop]
[loop query="posts_per_page=5"]
[link_with_content link_is_cf='custom_field_name'] 
[/loop]

See Also

Other shortcodes were affected with the 4.2.3 upgrade. Please see the following;