Guest User

Untitled

a guest
Nov 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. <div id="single-post-container"></div>
  2.  
  3. <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  4.  
  5. <a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>">
  6.  
  7. <?php the_title(); ?>
  8.  
  9. </a>
  10.  
  11. <?php endwhile; endif; ?>
  12.  
  13. <?php
  14.  
  15. $post = get_post($_POST['id']);
  16.  
  17. ?>
  18. <div id="single-post post-<?php the_ID(); ?>">
  19.  
  20. <?php while (have_posts()) : the_post(); ?>
  21.  
  22. <?php the_title();?>
  23.  
  24. <?php the_content();?>
  25.  
  26. <?php endwhile;?>
  27.  
  28. </div>
  29.  
  30. $(document).ready(function(){
  31.  
  32. $.ajaxSetup({cache:false});
  33. $(".post-link").click(function(){
  34. var post_link = $(this).attr("href");
  35.  
  36. $("#single-post-container").html("content loading");
  37. $("#single-post-container").load(post_link);
  38. return false;
  39. });
  40.  
  41. });
  42.  
  43. <div id="project-container"></div>
  44.  
  45. <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  46.  
  47. <a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>">
  48.  
  49. <?php the_title(); ?>
  50.  
  51. </a>
  52.  
  53. <?php endwhile; endif; ?>
  54.  
  55. <div id="single-post post-<?php the_ID(); ?>">
  56.  
  57. <?php while (have_posts()) : the_post(); ?>
  58.  
  59. <?php the_title();?>
  60.  
  61. <?php the_content();?>
  62.  
  63. <?php endwhile;?>
  64.  
  65. </div>
  66.  
  67. /**
  68. * Enqueue scripts and styles.
  69. */
  70. function starter_scripts() {
  71. wp_deregister_script( 'jquery' );
  72. wp_register_script( 'jquery', includes_url( '/js/jquery/jquery.js' ), false, NULL, true );
  73. wp_enqueue_script( 'jquery' );
  74.  
  75. wp_enqueue_style( 'starter-style', get_stylesheet_uri() );
  76. wp_enqueue_script( 'includes', get_template_directory_uri() . '/js/min/includes.min.js', array('jquery'), '', true );
  77. wp_enqueue_script( 'gray', get_template_directory_uri() . '/js/min/jquery.gray.min.js', array('jquery'), '', true );
  78. wp_localize_script( 'includes', 'site', array(
  79. 'theme_path' => get_template_directory_uri(),
  80. 'ajaxurl' => admin_url('admin-ajax.php')
  81. )
  82. );
  83. }
  84. add_action( 'wp_enqueue_scripts', 'starter_scripts' );
  85.  
  86. /**
  87. * AJAX nopriv
  88. */
  89. add_action('wp_ajax_load-single-post', 'prefix_ajax_single_post');
  90. add_action('wp_ajax_nopriv_load-single-post', 'prefix_ajax_single_post');
  91.  
  92. function prefix_ajax_single_post() {
  93. $pid = (int) filter_input(INPUT_GET, 'pID', FILTER_SANITIZE_NUMBER_INT);
  94. if ($pid > 0) {
  95. global $post;
  96. $post = get_post($pid);
  97. setup_postdata($post);
  98. printf('<div id="single-post post-%d">', $pid);
  99. the_title();
  100. the_content();
  101. echo '</div>';
  102. }
  103. exit();
  104. }
  105.  
  106. // Load posts via AJAX
  107. (function($, D){
  108.  
  109. $.ajaxSetup({cache:false});
  110.  
  111. $(".post-link").click(function(){
  112.  
  113. var postID = $(this).attr('rel');
  114. var $container = $("#project-container");
  115. $container.html("content loading");
  116. $.get(D.ajaxurl, {action: 'load-single-post', pID: postID}, function(content) {
  117. $container.html(content);
  118. });
  119.  
  120. });
  121.  
  122. })(jQuery, site);
  123.  
  124. $post = get_post($_POST['id']);
  125.  
  126. function is_ajax() {
  127. return isset($_SERVER['HTTP_X_REQUESTED_WITH'])
  128. && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
  129. }
  130.  
  131. if (is_ajax()) {
  132. // open here <html>, <head> and <body> tags
  133. }
  134.  
  135. // page content here
  136.  
  137. if (is_ajax()) {
  138. // close here <html>, <head> and <body> tags
  139. }
  140.  
  141. add_action('wp_enqueue_scripts', function() {
  142. // $script_url is the full url to your js file
  143. wp_enqueue_script('myjs', $script_url, array('jquery'), true, null, true);
  144. wp_localize_script('myjs', 'myData', array('ajaxurl' => admin_url('admin-ajax.php));
  145. });
  146.  
  147. add_action('wp_ajax_load-single-post', 'prefix_ajax_single_post');
  148. add_action('wp_ajax_nopriv_load-single-post', 'prefix_ajax_single_post');
  149.  
  150. function prefix_ajax_single_post() {
  151. $pid = (int) filter_input(INPUT_GET, 'pID', FILTSER_SANITIZE_NUMBER_INT);
  152. if ($pid > 0) {
  153. global $post;
  154. $post = get_post($pid);
  155. setup_postdata($post);
  156. printf('<div id="single-post post-%d">', $pid);
  157. the_title();
  158. the_content();
  159. echo '</div>';
  160. }
  161. exit();
  162. }
  163.  
  164. (function($, D){
  165.  
  166. $.ajaxSetup({cache:false});
  167.  
  168. $(".post-link").click(function(){
  169.  
  170. var postID = $(this).attr('rel');
  171. var $container = $("#single-post-container");
  172. $container.html("content loading");
  173. $.get(D.ajaxurl, {action: 'load-single-post', pID: postID}, function(content) {
  174. $container.html(content);
  175. });
  176.  
  177. });
  178.  
  179. })(jQuery, myData);
Add Comment
Please, Sign In to add comment