Last active
April 20, 2023 14:54
-
-
Save vovafeldman/60532ce0b7efb565162208eeff755b16 to your computer and use it in GitHub Desktop.
Freemius - weDocs KB Breadcrumbs Rich-Snipets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Override weDocs breadcrumb to add schema.org rich snippets metadata. | |
* | |
* @author Vova Feldman (@svovaf) | |
* | |
* @return void | |
*/ | |
function freemius_wedocs_breadcrumbs() { | |
global $post; | |
$args = apply_filters( 'wedocs_breadcrumbs', array( | |
'delimiter' => '<li class="delimiter">→</li>', | |
'home' => __( 'Home', 'wedocs' ), | |
'before' => '<li><span class="current">', | |
'after' => '</span></li>' | |
) ); | |
$breadcrumb_position = 1; | |
echo '<ol class="wedocs-breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList">'; | |
echo freemius_wedocs_get_breadcrumb( $args['home'], home_url( '/' ), $breadcrumb_position ); | |
echo $args['delimiter']; | |
$breadcrumb_position ++; | |
if ( $post->post_type == 'docs' && $post->post_parent ) { | |
$parent_id = $post->post_parent; | |
$breadcrumbs = array(); | |
while ( $parent_id ) { | |
$page = get_post( $parent_id ); | |
$breadcrumbs[] = freemius_wedocs_get_breadcrumb( get_the_title( $page->ID ), get_permalink( $page->ID ), $breadcrumb_position ); | |
$parent_id = $page->post_parent; | |
$breadcrumb_position ++; | |
} | |
$breadcrumbs = array_reverse( $breadcrumbs ); | |
for ( $i = 0; $i < count( $breadcrumbs ); $i ++ ) { | |
echo $breadcrumbs[ $i ]; | |
if ( $i != count( $breadcrumbs ) - 1 ) { | |
echo $args['delimiter']; | |
} | |
} | |
echo ' ' . $args['delimiter'] . ' ' . $args['before'] . get_the_title() . $args['after']; | |
} | |
echo '</ol>'; | |
} | |
/** | |
* @author Vova Feldman (@svovaf) | |
* | |
* @param string $label | |
* @param string $permalink | |
* @param int $position | |
* | |
* @return string | |
*/ | |
function freemius_wedocs_get_breadcrumb( $label, $permalink, $position = 1 ) { | |
return '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"> | |
<a itemprop="item" href="' . esc_attr( $permalink ) . '"> | |
<span itemprop="name">' . esc_html( $label ) . '</span></a> | |
<meta itemprop="position" content="' . $position . '" /> | |
</li>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IIt turns out very gracefully.
The biggest complaint about this plugin is that the developers refer to "home" as the main page of the website, but users want "home" to be the main page of the documents.
If you close this, there will definitely be more than one comment here.
Thank you!