Advertisement
Guest User

Untitled

a guest
Nov 29th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. <?php
  2. /**
  3. * Template Name: Article List Page
  4. * -------------------------------------
  5. * This is an example template that displays a single arlima article list.
  6. * In this file we will go through all the different callbacks that you can
  7. * hook into during the rendering of the article list.
  8. *
  9. * @package Arlima
  10. */
  11.  
  12. get_header();
  13. ?>
  14. <div id="primary" class="arlima-content">
  15. <div id="content" role="main">
  16. <?php
  17.  
  18. // Define the width of our list. This is later used to crop article images
  19. // This width is also defined in /plugins/arlima/css/template.css, so make
  20. // sure you change on both places if you want to change the width
  21. define('TMPL_ARTICLE_WIDTH', 480);
  22.  
  23. // Get id of current page
  24. $page_id = false; while ( have_posts() ) : the_post(); global $post; $page_id = $post->ID; endwhile;
  25.  
  26. // Get arlima slug added to this page in a custom field
  27. $arlima_slug = get_post_meta($page_id, 'arlima', true);
  28. if( !$arlima_slug ) {
  29. echo '<p>'.__('No list slug is defined. Please add custom field &quot;arlima&quot; to this page with the slug name of the list that you want to display', 'arlima').'</p>';
  30. }
  31. else {
  32.  
  33. // Load the arlima list
  34. $version = arlima_is_requesting_preview() ? 'preview' : '';
  35. $list = Arlima_ListFactory::load($arlima_slug, $version);
  36.  
  37. // List does not exist
  38. if( !$list->exists ) {
  39. echo '<p>'.__('It does not exist any arlima list with the slug', 'arlima').' &quot;'.$arlima_slug.'&quot;</p>';
  40. }
  41. else {
  42.  
  43. // Show a link that takes logged in users directly to wp-admin
  44. // where current list can be edited
  45. arlima_edit_link($list);
  46.  
  47. // Initiate template renderer that's responsible of
  48. // rendering current arlima list.
  49. $arlima_renderer = new Arlima_ListTemplateRenderer($list);
  50.  
  51. // Callback for article image
  52. $arlima_renderer->setGetImageCallback(function($article) {
  53.  
  54. $has_img = !empty($article['image_options']) && !empty( $article['image_options']['attach_id'] );
  55. $has_giant_tmpl = !empty($article['options']['template']) && $article['options']['template'] == 'giant';
  56.  
  57. if( $has_img && !$has_giant_tmpl ) {
  58.  
  59. $attach_meta = wp_get_attachment_metadata($article['image_options']['attach_id']);
  60. if( !$attach_meta )
  61. return false;
  62.  
  63. $article_width = empty($article['parent']) || $article['parent'] == -1 ? TMPL_ARTICLE_WIDTH : round(TMPL_ARTICLE_WIDTH * 0.5);
  64.  
  65. switch($article['image_options']['size']) {
  66. case 'half':
  67. $width = round($article_width * 0.5);
  68. $size = array($width, round( $attach_meta['height'] * ($width / $attach_meta['width'])));
  69. break;
  70. case 'third':
  71. $width = round($article_width * 0.33);
  72. $size = array($width, round( $attach_meta['height'] * ($width / $attach_meta['width'])));
  73. break;
  74. case 'quarter':
  75. $width = round($article_width * 0.25);
  76. $size = array($width, round( $attach_meta['height'] * ($width / $attach_meta['width'])));
  77. break;
  78. default:
  79. $size = array($article_width, round( $attach_meta['height'] * ($article_width / $attach_meta['width'])));
  80. break;
  81. }
  82.  
  83. $img_class = $article['image_options']['size'].' '.$article['image_options']['alignment'];
  84. $img_alt = htmlspecialchars( $article['title'] );
  85. $attach_url = wp_get_attachment_url( $article['image_options']['attach_id'] );
  86. $resized_img = image_resize( WP_CONTENT_DIR .'/uploads/'. $attach_meta['file'], $size[0], null, false, null, null, 98);
  87. if ( !is_wp_error($resized_img) ) {
  88. $img_url = dirname($attach_url) . '/' . basename($resized_img);
  89. } else {
  90. $img_url = $attach_url;
  91. }
  92. return sprintf('<img src="%s" width="%s" alt="%s" class="%s" />', $img_url, $size[0], $img_alt, $img_class);
  93. }
  94.  
  95. return false;
  96. });
  97.  
  98. // Callback used when a future posts comes up in the list, will only fire when looking at
  99. // a preview version of the list
  100. $arlima_renderer->setFuturePostCallback(function($post, $article, $list) {
  101. ?>
  102. <div class="arlima future-post">
  103. Hey dude, <a href="<?php echo admin_url('post.php?action=edit&amp;post='.$post->ID) ?>" target="_blank">this post</a>
  104. will not show up in the list until it's published, unless you're not previewing the list that is...
  105. </div>
  106. <?php
  107. });
  108.  
  109. // Modify text content
  110. $arlima_renderer->setTextModifierCallback(function($article, $is_post, $post) {
  111. $article['text'] = apply_filters('the_arlima_content', $article['text']);
  112. return arlima_link_entrywords(trim($article['text']), $article['url']);
  113. });
  114.  
  115. // Callback for related posts
  116. $arlima_renderer->setRelatedPostsCallback(function($article, $is_post) {
  117. return $is_post ? arlima_related_posts('inline', null, false) : '';
  118. });
  119.  
  120. // Callback taking place before every article is rendered
  121. $arlima_renderer->setBeforeArticleCallback(function($article_counter, $article) {
  122. // ...
  123. });
  124.  
  125. // Callback taking place after every article is rendered
  126. $arlima_renderer->setAfterArticleCallback(function($article_counter, $article) {
  127. // ...
  128. });
  129.  
  130. // The list doesn't have any articles :(
  131. if( !$arlima_renderer->havePosts() ) {
  132. echo '<p><em>'.__('Please feed me some articles, I\'m hungry', 'arlima').'</em></p>';
  133. }
  134.  
  135. // Let the magic happen...
  136. else {
  137. $arlima_renderer->renderList();
  138. }
  139. }
  140. }
  141. ?>
  142. </div>
  143. </div>
  144.  
  145. <?php get_sidebar(); ?>
  146.  
  147. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement