Advertisement
verygoodplugins

Untitled

Oct 13th, 2020
1,911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.98 KB | None | 0 0
  1. function wpf_documentation_sidebar() {
  2.  
  3.     ob_start();
  4.  
  5.     $category = get_query_var( 'documentation_category' );
  6.     global $post;
  7.  
  8.     ?>
  9.  
  10.     <div id="categories-sidebar" class="sidebar line-top">
  11.         <h3>In this chapter</h3>
  12.  
  13.         <?php $posts = get_posts( array('post_type' => 'documentation', 'documentation_category' => $category, 'fields' => 'ids', 'orderby' => 'title', 'order' =>'ASC', 'posts_per_page' => -1) ); ?>
  14.  
  15.         <ul class="category-posts">
  16.  
  17.             <?php foreach($posts as $id) : ?>
  18.                 <li><a <?php if($post->ID == $id) echo 'class="active" '; ?> href="<?php echo get_permalink( $id ) ?>" title="<?php echo get_the_title($id) ?>"><?php echo get_the_title( $id ); ?></a></li>
  19.             <?php endforeach; ?>
  20.  
  21.         </ul>
  22.  
  23.     </div>
  24.  
  25.     <?
  26.  
  27.     return ob_get_clean();
  28.  
  29. }
  30.  
  31. add_shortcode( 'wpf_documentation_sidebar', 'wpf_documentation_sidebar' );
  32.  
  33. function wpf_documentation_nav() {
  34.  
  35.     ob_start();
  36.  
  37.     global $post;
  38.  
  39.     $depth = 4;
  40.  
  41.     $pattern  = '/<h[2-' . $depth . ']*[^>]*>.*?<\/h[2-' . $depth . ']>/';
  42.     $whocares = preg_match_all( $pattern, $post->post_content, $winners );
  43.  
  44.     foreach ( $winners[0] as $i => $winner ) {
  45.  
  46.         $winners[0][ $i ] = str_replace( '<h4>', '<h4><a href="#' . sanitize_title( $winner ) . '">', $winners[0][ $i ] );
  47.         $winners[0][ $i ] = str_replace( '</h4>', '</a></h4>', $winners[0][ $i ] );
  48.  
  49.         $winners[0][ $i ] = str_replace( '<h3>', '<h3><a href="#' . sanitize_title( $winner ) . '">', $winners[0][ $i ] );
  50.         $winners[0][ $i ] = str_replace( '</h3>', '</a></h3>', $winners[0][ $i ] );
  51.  
  52.     }
  53.  
  54.     //reformat the results to be more usable
  55.     $heads = implode( "\n", $winners[0] );
  56.     $heads = preg_replace( '/<h([1-' . $depth . '])>/', '<li class="toc$1">', $heads );
  57.     $heads = preg_replace( '/<\/h[1-' . $depth . ']>/', '</li>', $heads );
  58.  
  59.     ?>
  60.  
  61.     <div id="nav-sidebar" class="sidebar line-top">
  62.         <h3>On this page</h3>
  63.  
  64.         <ul><?php echo $heads; ?></ul>
  65.  
  66.     </div>
  67.  
  68.     <?php
  69.  
  70.     return ob_get_clean();
  71.  
  72. }
  73.  
  74. add_shortcode( 'wpf_documentation_nav', 'wpf_documentation_nav' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement