Guest User

Untitled

a guest
Apr 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. /**
  2. * Code bai viet lien quan cuoi moi bai trong AMP thay so 7 bang so ban muon
  3. */
  4. function my_amp_related_posts( $count = 7 ) {
  5. global $post;
  6. $taxs = get_object_taxonomies( $post );
  7. if ( ! $taxs ) {
  8. return;
  9. }
  10. // ignoring post formats
  11. if( ( $key = array_search( 'post_format', $taxs ) ) !== false ) {
  12. unset( $taxs[$key] );
  13. }
  14. // try tags first
  15. if ( ( $tag_key = array_search( 'post_tag', $taxs ) ) !== false ) {
  16. $tax = 'post_tag';
  17. $tax_term_ids = wp_get_object_terms( $post->ID, $tax, array( 'fields' => 'ids' ) );
  18. }
  19. // if no tags, then by cat or custom tax
  20. if ( empty( $tax_term_ids ) ) {
  21. // remove post_tag to leave only the category or custom tax
  22. if ( $tag_key !== false ) {
  23. unset( $taxs[ $tag_key ] );
  24. $taxs = array_values($taxs);
  25. }
  26.  
  27. $tax = $taxs[0];
  28. $tax_term_ids = wp_get_object_terms( $post->ID, $tax, array('fields' => 'ids') );
  29.  
  30. }
  31. if ( $tax_term_ids ) {
  32. $args = array(
  33. 'post_type' => $post->post_type,
  34. 'posts_per_page' => $count,
  35. 'orderby' => 'rand',
  36. 'tax_query' => array(
  37. array(
  38. 'taxonomy' => $tax,
  39. 'field' => 'id',
  40. 'terms' => $tax_term_ids
  41. )
  42. ),
  43. 'post__not_in' => array ($post->ID),
  44. );
  45. $related = new WP_Query( $args );
  46.  
  47. if ($related->have_posts()) : ?>
  48.  
  49.  
  50. <aside>
  51.  
  52. <h3>Xem thêm</h3>
  53.  
  54.  
  55. <ul>
  56.  
  57. <?php while ( $related->have_posts() ) : $related->the_post(); ?>
  58.  
  59.  
  60. <li><a href="<?php echo amp_get_permalink( get_the_id() ); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
  61.  
  62.  
  63. <?php endwhile; wp_reset_postdata(); ?>
  64.  
  65. </ul>
  66.  
  67. </aside>
  68.  
  69.  
  70. <?php endif; } } function my_add_related_posts_to_amp( $template ) { ?>
  71.  
  72. <div class="amp-wp-content">
  73. <?php my_amp_related_posts(); ?>
  74. </div>
  75.  
  76. <?php
  77. }
  78. add_action( 'amp_post_template_footer', 'my_add_related_posts_to_amp' );
  79. /**
  80. * Het code bai viet lien quan cuoi moi bai trong AMP thay so 7 bang so ban muon
  81. */
Add Comment
Please, Sign In to add comment