Below, you’ll find a detailed guide on how to add the SEOPress – On-site SEO Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the SEOPress – On-site SEO Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the SEOPress – On-site SEO Plugin and the shortcodes it provides:
"SEOPress – On-site SEO is a powerful WordPress plugin designed to optimize your website for search engines. It helps improve your site's visibility, increasing traffic and conversions."
- [seopress_html_sitemap]
SEOPress [seopress_html_sitemap] Shortcode
The SEOPress HTML Sitemap shortcode is used to generate an HTML sitemap for your website. It fetches the sitemap options, post types, and categories, and organizes them in an HTML structure. The shortcode takes ‘cpt’ as an attribute to specify custom post types. It also allows for exclusion of certain IDs and customization of the order and orderby parameters. It generates a list of all post types and their respective posts, ordered and filtered according to the specified parameters. The output is wrapped in HTML tags for better visibility and SEO optimization.
Shortcode: [seopress_html_sitemap]
Parameters
Here is a list of all possible seopress_html_sitemap shortcode parameters and attributes:
cpt
– defines custom post types to include in the sitemap
Examples and Usage
Basic example – Displaying the HTML sitemap without any specific parameters.
[seopress_html_sitemap]
Advanced example – Displaying the HTML sitemap for specific custom post types (CPT). This example includes two custom post types, ‘product’ and ‘book’.
[seopress_html_sitemap cpt="product,book"]
Another advanced example could be using the shortcode to exclude certain pages or posts from the sitemap. This can be done by using the ‘exclude’ attribute and providing the IDs of the posts or pages you want to exclude.
[seopress_html_sitemap exclude="1,2,3"]
It is also possible to control the order of the sitemap entries using the ‘order’ and ‘orderby’ attributes. For example, you could order the sitemap by post date in descending order.
[seopress_html_sitemap order="DESC" orderby="date"]
PHP Function Code
In case you have difficulties debugging what causing issues with [seopress_html_sitemap]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('seopress_html_sitemap', 'seopress_xml_sitemap_html_hook');
Shortcode PHP function:
function seopress_xml_sitemap_html_hook($html)
{
// Attributes
$atts = shortcode_atts(
[
'cpt' => '',
],
$html,
'[seopress_html_sitemap]'
);
$product_cat_slug = 'product_cat';
$product_cat_slug = apply_filters('seopress_sitemaps_html_product_cat_slug', $product_cat_slug);
//Exclude IDs
if ('' !== seopress_get_service('SitemapOption')->getHtmlExclude()) {
$seopress_xml_sitemap_html_exclude_option = seopress_get_service('SitemapOption')->getHtmlExclude();
} else {
$seopress_xml_sitemap_html_exclude_option = '';
}
//Order
if ('' !== seopress_get_service('SitemapOption')->getHtmlOrder()) {
$seopress_xml_sitemap_html_order_option = seopress_get_service('SitemapOption')->getHtmlOrder();
} else {
$seopress_xml_sitemap_html_order_option = '';
}
//Orderby
if ('' !== seopress_get_service('SitemapOption')->getHtmlOrderBy()) {
$seopress_xml_sitemap_html_orderby_option = seopress_get_service('SitemapOption')->getHtmlOrderBy();
} else {
$seopress_xml_sitemap_html_orderby_option = '';
}
$html = '';
//CPT
if (!empty(seopress_get_service('SitemapOption')->getPostTypesList())) {
$html .= '<div class="wrap-html-sitemap sp-html-sitemap">';
$seopress_xml_sitemap_post_types_list_option = seopress_get_service('SitemapOption')->getPostTypesList();
if (isset($seopress_xml_sitemap_post_types_list_option['page'])) {
$seopress_xml_sitemap_post_types_list_option = ['page' => $seopress_xml_sitemap_post_types_list_option['page']] + $seopress_xml_sitemap_post_types_list_option; //Display page first
}
if (! empty($atts['cpt'])) {
unset($seopress_xml_sitemap_post_types_list_option);
$cpt = explode(',', $atts['cpt']);
foreach ($cpt as $key => $value) {
$seopress_xml_sitemap_post_types_list_option[$value] = ['include' => '1'];
}
}
$seopress_xml_sitemap_post_types_list_option = apply_filters('seopress_sitemaps_html_cpt', $seopress_xml_sitemap_post_types_list_option);
foreach ($seopress_xml_sitemap_post_types_list_option as $cpt_key => $cpt_value) {
if (! empty($cpt_value)) {
$html .= '<div class="sp-wrap-cpt">';
}
$obj = get_post_type_object($cpt_key);
if ($obj) {
$cpt_name = $obj->labels->name;
$cpt_name = apply_filters('seopress_sitemaps_html_cpt_name', $cpt_name, $obj->name);
$html .= '<h2 class="sp-cpt-name">' . $cpt_name . '</h2>';
}
foreach ($cpt_value as $_cpt_key => $_cpt_value) {
if ('1' == $_cpt_value) {
$args = [
'posts_per_page' => 1000,
'order' => $seopress_xml_sitemap_html_order_option,
'orderby' => $seopress_xml_sitemap_html_orderby_option,
'post_type' => $cpt_key,
'post_status' => 'publish',
'meta_query' => [['key' => '_seopress_robots_index', 'value' => 'yes', 'compare' => 'NOT EXISTS']],
'fields' => 'ids',
'exclude' => $seopress_xml_sitemap_html_exclude_option,
'suppress_filters' => false,
'no_found_rows' => true,
'nopaging' => true,
];
$args_cat_query = [
'orderby' => 'name',
'order' => 'ASC',
'meta_query' => [['key' => '_seopress_robots_index', 'value' => 'yes', 'compare' => 'NOT EXISTS']],
'exclude' => $seopress_xml_sitemap_html_exclude_option,
'suppress_filters' => false,
];
if ('post' === $cpt_key) {
$args_cat_query = apply_filters('seopress_sitemaps_html_cat_query', $args_cat_query);
$cats = get_categories($args_cat_query);
} elseif ('product' === $cpt_key) {
$args_cat_query = apply_filters('seopress_sitemaps_html_product_cat_query', $args_cat_query);
$cats = get_terms($product_cat_slug, $args_cat_query);
}
if ('post' !== $cpt_key && 'product' !== $cpt_key) {
$cats = apply_filters('seopress_sitemaps_html_hierarchical_terms_query', $cpt_key, $args_cat_query);
}
if (is_array($cats) && ! empty($cats)) {
$html .= '<div class="sp-wrap-cats">';
foreach ($cats as $cat) {
if ( ! is_wp_error($cat) && is_object($cat)) {
$html .= '<div class="sp-wrap-cat">';
$html .= '<h3 class="sp-cat-name"><a href="'. get_term_link($cat->term_id) .'">' . $cat->name . '</a></h3>';
if ('post' === $cpt_key) {
unset($args['cat']);
$args['cat'][] = $cat->term_id;
} elseif ('product' === $cpt_key) {
unset($args['tax_query']);
$args['tax_query'] = [[
'taxonomy' => $product_cat_slug,
'field' => 'term_id',
'terms' => $cat->term_id,
]];
}
if ('post' !== $cpt_key && 'product' !== $cpt_key) {
$args['tax_query'] = apply_filters('seopress_sitemaps_html_hierarchical_tax_query', $cpt_key, $cat, $args);
}
require dirname(__FILE__) . '/sitemap/template-html-sitemap.php';
$html .= '</div>';
}
}
$html .= '</div>';
} else {
require dirname(__FILE__) . '/sitemap/template-html-sitemap.php';
}
}
}
if (! empty($cpt_value)) {
$html .= '</div>';
}
}
$html .= '</div>';
}
return $html;
}
Code file location:
wp-seopress/wp-seopress/inc/functions/options-sitemap.php
Conclusion
Now that you’ve learned how to embed the SEOPress – On-site SEO Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply