Advertisement
firoze

Json data dynamic in wordpress

Mar 2nd, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. // json data dynamic in wordpress
  2.  
  3. <?php
  4. $loop = 0;
  5. // Example argument that defines three posts per page.
  6. $args = array( 'posts_per_page' => -1,'post_type'=>'json' );
  7.  
  8. // Variable to call WP_Query.
  9. $the_query = new WP_Query( $args );
  10.  
  11. if ( $the_query->have_posts() ) :
  12. // Start the Loop
  13. while ( $the_query->have_posts() ) : $the_query->the_post();
  14. $loop ++;
  15. ?><div id="demo<?php echo $loop;?>"></div><?php
  16. // End the Loop
  17. endwhile;
  18. else:
  19. // If no posts match this query, output this text.
  20. _e( 'Sorry, no posts matched your criteria.', 'textdomain' );
  21. endif;
  22. wp_reset_postdata();
  23.  
  24.  
  25.  
  26. ?>
  27.  
  28.  
  29.  
  30.  
  31.  
  32. <script>
  33. function _(x){return window.document.getElementById(x)}
  34. var text = '{ "employees" : [' +
  35. '{ "firstName":"<?php the_title();?>" , "lastName":"<?php the_title();?>" },'+
  36. <?php
  37. $loop = 0;
  38. // Example argument that defines three posts per page.
  39. $args = array( 'posts_per_page' => -1,'post_type'=>'json' );
  40.  
  41. // Variable to call WP_Query.
  42. $the_query = new WP_Query( $args );
  43.  
  44. if ( $the_query->have_posts() ) :
  45. // Start the Loop
  46. while ( $the_query->have_posts() ) : $the_query->the_post();
  47. $last_name = get_post_meta( $post->ID, 'last_name', true );
  48. $loop++;
  49. ?>
  50. '{ "firstName":"<?php the_title();?>" , "lastName":"<?php echo $last_name;?>" },'+
  51. <?php
  52. // End the Loop
  53. endwhile;
  54. else:
  55. // If no posts match this query, output this text.
  56. _e( 'Sorry, no posts matched your criteria.', 'textdomain' );
  57. endif;
  58. wp_reset_postdata();
  59.  
  60. ?>'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
  61.  
  62. var obj = JSON.parse(text);
  63.  
  64. //console.log(obj.employees.length); // length 4
  65.  
  66. var totalpost = obj.employees.length-1; // 4-1 = 3
  67.  
  68. for(var i = 1;i<totalpost;i++){
  69. _("demo"+i).innerHTML = obj.employees[i].firstName + " " + obj.employees[i].lastName;
  70.  
  71. //console.log( ("demo"+i) );
  72. }
  73.  
  74.  
  75. </script>
  76.  
  77.  
  78. ***********************************************************************************
  79. function json_setup_post_type() {
  80. register_post_type( 'json',
  81. array(
  82. 'labels' => array(
  83. 'name' => __( 'Json Post' ),
  84. 'singular_name' => __( 'Json Post Type' ),
  85. 'add_new'=>_('Add New Json')
  86. ),
  87. 'public' => true,
  88. 'menu_icon'=> 'dashicons-camera', /* For Dashicons Menu */
  89. 'has_archive' => true,
  90. 'rewrite'=> array( 'slug' => 'json' ),
  91. 'supports'=> array( 'title' )
  92.  
  93. )
  94. );
  95. }
  96. add_action( 'init', 'json_setup_post_type' );
  97.  
  98.  
  99. **************************************************************************************
  100.  
  101. // custom mete box CMB2
  102. function yourprefix_register_demo_metabox() {
  103. $prefix = 'yourprefix_demo_';
  104.  
  105. /**
  106. * Sample metabox to demonstrate each field type included
  107. */
  108. $cmb_demo = new_cmb2_box( array(
  109. 'id' => 'metabox',
  110. 'title' => esc_html__( 'Test Metabox', 'cmb2' ),
  111. 'object_types' => array( 'json'), // Post type
  112. // 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value
  113. // 'context' => 'normal',
  114. // 'priority' => 'high',
  115. // 'show_names' => true, // Show field names on the left
  116. // 'cmb_styles' => false, // false to disable the CMB stylesheet
  117. // 'closed' => true, // true to keep the metabox closed by default
  118. // 'classes' => 'extra-class', // Extra cmb2-wrap classes
  119. // 'classes_cb' => 'yourprefix_add_some_classes', // Add classes through a callback.
  120. ) );
  121.  
  122.  
  123. $cmb_demo->add_field( array(
  124. 'name' => esc_html__( 'Test Text Small', 'cmb2' ),
  125. 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
  126. 'id' =>'last_name',
  127. 'type' => 'text',
  128. 'default'=>''
  129. // 'repeatable' => true,
  130. // 'column' => array(
  131. // 'name' => esc_html__( 'Column Title', 'cmb2' ), // Set the admin column title
  132. // 'position' => 2, // Set as the second column.
  133. // );
  134. // 'display_cb' => 'yourprefix_display_text_small_column', // Output the display of the column values through a callback.
  135. ) );
  136.  
  137. $cmb_demo->add_field( array(
  138. 'name' => esc_html__( 'Color Picker', 'cmb2' ),
  139. 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
  140. 'id' =>'text_color',
  141. 'type' => 'colorpicker',
  142. 'default'=>'#111'
  143. ) );
  144.  
  145. $cmb_demo->add_field( array(
  146. 'name' => esc_html__( 'Color Picker', 'cmb2' ),
  147. 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
  148. 'id' =>'bg_color',
  149. 'type' => 'colorpicker',
  150. 'default'=>'#fff'
  151. ) );
  152.  
  153.  
  154.  
  155.  
  156.  
  157. }
  158.  
  159.  
  160.  
  161. // dynamic css file
  162. ob_start();
  163. require_once( get_stylesheet_directory().'/css/filestyle.php' );
  164. file_put_contents( get_stylesheet_directory()."/css/options.css",ob_get_clean());
  165.  
  166.  
  167. ************************************************************************************************************
  168.  
  169. // css dynamic
  170.  
  171. <?php
  172. $loop = 0;
  173. // Example argument that defines three posts per page.
  174. $args = array( 'posts_per_page' => -1,'post_type'=>'json' );
  175.  
  176. // Variable to call WP_Query.
  177. $the_query = new WP_Query( $args );
  178.  
  179. if ( $the_query->have_posts() ) :
  180. // Start the Loop
  181. while ( $the_query->have_posts() ) : $the_query->the_post();
  182. $text_color = get_post_meta( $post->ID, 'text_color', true );
  183. $bg_color = get_post_meta( $post->ID, 'bg_color', true );
  184. $loop ++;
  185. ?>
  186. #demo<?php echo $loop;?>{color:<?php echo $text_color;?>;background-color:<?php echo $bg_color;?>;}
  187. <?php
  188.  
  189.  
  190. // End the Loop
  191. endwhile;
  192. else:
  193. // If no posts match this query, output this text.
  194. _e( 'Sorry, no posts matched your criteria.', 'textdomain' );
  195. endif;
  196. wp_reset_postdata();
  197.  
  198. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement