Guest User

Untitled

a guest
Aug 3rd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Page children
  4. Description: Provides [b3317133_children] shortcode
  5. Version: 0.2
  6. Author: b3317133
  7. */
  8. function b3317133_children_init() {
  9.     function b3317133_children_func( $atts, $content = null ) {
  10.         $output = '';
  11.         $navi = array();
  12.         global $post;
  13.         $args = array(
  14.             'posts_per_page' => -1,
  15.             'post_type' => $post->post_type,
  16.             'post_status' => 'publish',     // 'draft' fuer Entwurf Status
  17.             'post_parent' => $post->ID,
  18.             'orderby' => 'title',
  19.             'order' => 'ASC'
  20.         );
  21.         $posts = get_posts( $args );
  22.         foreach( $posts as $post ) {
  23.             setup_postdata( $post );
  24.             // for navigation
  25.             $navi[] = get_the_title();
  26.             // use buffer to get content including the_content filters & wptexturize
  27.             ob_start();
  28. ?>
  29. <h1 id="<?php echo esc_attr( get_the_title() ); ?>"><?php the_title(); ?></h1>
  30. <?php the_content(); ?>
  31. <hr class="b3317133-up" />
  32. <?php
  33.             $output .= ob_get_clean();
  34.         }
  35.         wp_reset_postdata();
  36.         // build navigation
  37.         array_walk( $navi, function( &$n ) {
  38.             $n = '<a href="#' . esc_attr( $n ) . '">' . $n . '</a>';
  39.         });
  40.         // add navigation
  41.         $output = '<p id="b3317133-nav">' . implode( ' ', $navi ) . '</p><hr />' . $output;
  42.         return $output;
  43.     }
  44.     add_shortcode( 'b3317133_children', 'b3317133_children_func' );
  45. }
  46. add_action('init', 'b3317133_children_init');
  47.  
  48. function b3317133_children_enqueue_scripts() {
  49.     wp_enqueue_script( 'jquery' );
  50. }
  51. add_action( 'wp_enqueue_scripts', 'b3317133_children_enqueue_scripts' );
  52.  
  53. function b3317133_children_wp_head() {
  54. ?>
  55. <style type="text/css">
  56. #b3317133-nav a {
  57.     padding-right: 5px; /* distance after each navi */
  58. }
  59. hr.b3317133-up {
  60.     position: relative;
  61. }
  62. hr.b3317133-up::after {
  63.     position: absolute;
  64.     right: 0;
  65.     top: -10px;
  66.     content: '^';
  67.     font-size: 18px;
  68.     background-color: #fff;
  69.     cursor: pointer;
  70.     padding-left: 5px;
  71. }
  72. </style>
  73. <script type="text/javascript">
  74. jQuery(document).ready(function(){
  75.     jQuery(document).on('click', 'hr.b3317133-up', function(event) {
  76.         event.preventDefault();
  77.         //location.hash = '#b3317133-nav';
  78.         jQuery('html, body').animate({
  79.             'scrollTop': jQuery('#b3317133-nav').offset().top - 18
  80.         }, 200);
  81.     });
  82. });
  83. </script>
  84. <?php
  85. }
  86. add_action('wp_head', 'b3317133_children_wp_head', 999);
Add Comment
Please, Sign In to add comment