Introduction
This shortcode allows you to conditionally display content on the page avoiding the potential for duplicate content issues.
The content to be displayed can be either pulled in from another post or from the shortcode enclosed content. I recommend the former as that method can be easier to set up. Please see the Example section below.
Usage
The most common scenario is to add content above the directory. Here’s a walkthru example of how to set this up correctly to avoid duplicate content.
- Create a new page.
- Add the content you want to display above the directory on this page. The content can be built with the Classic Editor, Block Editor, or other page editors like the Divi builder. For best compatibility, the Classic Editor or the Block Editor is recommended.
- Save the new page with the Directory page as the parent page. This is not strictly required but is recommended for organizations.
- When saving the page, it is recommended to set the page to Private since it is not necessary to have this page publicly visible.
- After saving, WordPress will assign the page an id. The page id can be found in the browser’s address bar. Please take note of the
id
as it will be required for the shortcode. - Edit the page which contains the
[connections]
shortcode. - Add the following shortcode before the
[connections]
shortcode and save the page. Change then
in the example to the pageid
.
[cn-content id=n/]
When you view the directory page, you will see the contents of the new page displayed above the directory.
Important Notes
- This shortcode must come before the
[connections]
shortcode. - This shortcode will not be triggered by the
[connections]
shortcode options. It will only be triggered by HTTP queries such as Entry permalinks or filtering by category. - If using both enclosing and self-closing instances of this shortcode on the same page, make sure to add a trailing forward slash (
/
) to the self-closing instances of the shortcode. The WordPress shortcode parser does not handle the mixing of enclosing and non-enclosing forms of the same shortcode, and you will not get the results you want.
This shortcode has been tested and confirmed to work with the WordPress Block Editor, Classic Editor, and the Divi Builder plugin.
Important
Options
condition
The condition must be met in order to display the content.
current_user_can
:: Whether or not the current logged-in user can do capability.- The
parameter
option is required. - The
parameter
option must be a valid role capability.
- The
current_user_has_role
:: Whether or not the current user has been assigned the defined role.- The
parameter
option is required. - The
parameter
option must be a valid role.
- The
in_category
:: Whether or not the current Entry is attached to the specified category.- The
parameter
option is required. - This is effectively a combined
is_single
andis_category
condition.
- The
is_category
:: Whether or not the current query is a category query. If the parameter is supplied, then it will check whether or not the current category matches the parameter.- The
parameter
option is optional.
- The
is_front_page
:: Whether or not a query is been made. Example an Entry or Category query.- The
paramter
option is not utilized. - NOTE: This is the default condition.
- The
is_home
:: Whether or not the current page is the page set as the Directory Homepage.- NOTE: This will only display content if the
is_front_page
condition is also met.
- NOTE: This will only display content if the
is_search
:: Whether or not a keyword search is being performed.is_single
:: Whether or not the current view is the single Entry detail/profile page.- The
parameter
option is optional.
- The
is_user_logged_in
:: Whether or not the current user is logged in.
parameter
Can be used in conjunction with the current_user_can
, current_user_has_role
, in_category
, is_category
and is_single
condition to target a specific Entry or Category by its ID or Slug.
Valid values when using the current_user_can
condition:
- The list of default capabilities can be found on the following WordPress documentation page:
Valid values when using the current_user_has_role
condition:
- Any valid role can be used. Here is a list of the default Roles in WordPress
- administrator
- editor
- author
- contributor
- subscriber
Valid values when using the in_category
and is_category
conditions:
- Category
id
(recommended) - Category
slug
Valid values when using the is_single
condition:
- Entry
id
(recommended) - Entry
slug
id
The WordPress Page/Post ID to pull content from.
block
The part of the WordPress Page/Post to display.
- content (default)
- title
insert
Where to insert the content. The default is to display the content in place of the [cn-content]
shortcode. Adding the insert option and setting it to either head
or foot
will display the content in the head or foot of the [connections]
shortcode content.
- head
- foot
NOTE: Any valid Connections action can be utilized.
Examples
Insert the contents of post ID 3, and display it at the location of the shortcode only when viewing the Entry results list.
[cn-content id=3/]
Insert the contents of post ID 18005, and display it at the location of the shortcode only when viewing the single Entry detail/profile page.
[cn-content id=18005 condition=is_single/]
Insert the contents of the post id
18005, display it at the location of the shortcode only when viewing the Entry with the id of 56.
[cn-content id=18005 condition=is_single parameter=56/]
Insert the enclosed content only in the Entry search results view.
[cn-content condition=is_search]Display this content on the search results view only.[/cn-content]
Filters
The following filter can be used to add custom conditions:
Connections_Directory/Shortcode/Conditional_Content/is_condition/{condition}
The filter passes three parameters:
false
:: Returntrue
orfalse
based on whether or not the custom condition is met.$parameter
:: the value of the parameter shortcode option.$shortcode
:: the shortcode instance.
/** * Example 1 * * Adds a custom condition named "is_search__and__is_category". * * This will return false if the condition is BOTH a search and a category * and true if only a category. * * The purpose is to display content only when the is category condition is met, * not when searhcing wihtin category. */ add_filter( 'Connections_Directory/Shortcode/Conditional_Content/is_condition/is_search__and__is_category', function( $condition, $parameter, $shortcode ) { if ( $shortcode->isSearch() && $shortcode->isCategory( $parameter ) ) { return false; } if ( $shortcode->isCategory( $parameter ) ) { return true; } return $condition; }, 10, 3 );