[ Index ]

WordPress Source Cross Reference

title

Body

[close]

/wp-includes/ -> post-template.php (source)

   1  <?php
   2  
   3  //
   4  // "The Loop" post functions
   5  //
   6  
   7  function the_ID() {
   8      global $id;
   9      echo $id;
  10  }
  11  
  12  
  13  function get_the_ID() {
  14      global $id;
  15      return $id;
  16  }
  17  
  18  
  19  function the_title($before = '', $after = '', $echo = true) {
  20      $title = get_the_title();
  21      if ( strlen($title) > 0 ) {
  22          $title = apply_filters('the_title', $before . $title . $after, $before, $after);
  23          if ( $echo )
  24              echo $title;
  25          else
  26              return $title;
  27      }
  28  }
  29  
  30  
  31  function get_the_title($id = 0) {
  32      $post = &get_post($id);
  33  
  34      $title = $post->post_title;
  35      if ( !empty($post->post_password) )
  36          $title = sprintf(__('Protected: %s'), $title);
  37      else if ( 'private' == $post->post_status )
  38          $title = sprintf(__('Private: %s'), $title);
  39  
  40      return $title;
  41  }
  42  
  43  function the_guid( $id = 0 ) {
  44      echo get_the_guid($id);
  45  }
  46  
  47  function get_the_guid( $id = 0 ) {
  48      $post = &get_post($id);
  49  
  50      return apply_filters('get_the_guid', $post->guid);
  51  }
  52  
  53  function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
  54      $content = get_the_content($more_link_text, $stripteaser, $more_file);
  55      $content = apply_filters('the_content', $content);
  56      $content = str_replace(']]>', ']]&gt;', $content);
  57      echo $content;
  58  }
  59  
  60  
  61  function get_the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
  62      global $id, $post, $more, $single, $withcomments, $page, $pages, $multipage, $numpages;
  63      global $preview;
  64      global $pagenow;
  65      $output = '';
  66  
  67      if ( !empty($post->post_password) ) { // if there's a password
  68          if ( stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password ) {    // and it doesn't match the cookie
  69              $output = get_the_password_form();
  70              return $output;
  71          }
  72      }
  73  
  74      if ( $more_file != '' )
  75          $file = $more_file;
  76      else
  77          $file = $pagenow; //$_SERVER['PHP_SELF'];
  78  
  79      $content = $pages[$page-1];
  80      $content = explode('<!--more-->', $content, 2);
  81      if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
  82          $stripteaser = 1;
  83      $teaser = $content[0];
  84      if ( ($more) && ($stripteaser) )
  85          $teaser = '';
  86      $output .= $teaser;
  87      if ( count($content) > 1 ) {
  88          if ( $more )
  89              $output .= '<a id="more-'.$id.'"></a>'.$content[1];
  90          else
  91              $output = balanceTags($output . ' <a href="'. get_permalink() . "#more-$id\">$more_link_text</a>");
  92      }
  93      if ( $preview ) // preview fix for javascript bug with foreign languages
  94          $output =    preg_replace('/\%u([0-9A-F]{4,4})/e',    "'&#'.base_convert('\\1',16,10).';'", $output);
  95  
  96      return $output;
  97  }
  98  
  99  
 100  function the_excerpt() {
 101      echo apply_filters('the_excerpt', get_the_excerpt());
 102  }
 103  
 104  
 105  function get_the_excerpt($fakeit = true) {
 106      global $id, $post;
 107      $output = '';
 108      $output = $post->post_excerpt;
 109      if ( !empty($post->post_password) ) { // if there's a password
 110          if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) {  // and it doesn't match the cookie
 111              $output = __('There is no excerpt because this is a protected post.');
 112              return $output;
 113          }
 114      }
 115  
 116      return apply_filters('get_the_excerpt', $output);
 117  }
 118  
 119  
 120  function wp_link_pages($args = '') {
 121      parse_str($args, $r);
 122      if ( !isset($r['before']) )
 123          $r['before'] = '<p>' . __('Pages:');
 124      if ( !isset($r['after']) )
 125          $r['after'] = '</p>';
 126      if ( !isset($r['next_or_number']) )
 127          $r['next_or_number'] = 'number';
 128      if ( !isset($r['nextpagelink']) )
 129          $r['nextpagelink'] = 'Next page';
 130      if ( !isset($r['previouspagelink']) )
 131          $r['previouspagelink'] = 'Previous page';
 132      if ( !isset($r['pagelink']) )
 133          $r['pagelink'] = '%';
 134      if ( !isset($r['more_file']) )
 135          $r['more_file'] = '';
 136  
 137      link_pages($r['before'], $r['after'], $r['next_or_number'], $r['nextpagelink'], $r['previouspagelink'], $r['pagelink'], $r['more_file']);
 138  }
 139  
 140  
 141  function link_pages($before='<br />', $after='<br />', $next_or_number='number', $nextpagelink='next page', $previouspagelink='previous page', $pagelink='%', $more_file='') {
 142      global $id, $page, $numpages, $multipage, $more, $pagenow;
 143      if ( $more_file != '' )
 144          $file = $more_file;
 145      else
 146          $file = $pagenow;
 147      if ( $multipage ) {
 148          if ( 'number' == $next_or_number ) {
 149              echo $before;
 150              for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
 151                  $j = str_replace('%',"$i",$pagelink);
 152                  echo ' ';
 153                  if ( ($i != $page) || ((!$more) && ($page==1)) ) {
 154                      if ( '' == get_settings('permalink_structure') )
 155                          echo '<a href="' . get_permalink() . '&amp;page=' . $i . '">';
 156                      else
 157                          echo '<a href="' . trailingslashit( get_permalink() ) . $i . '/">';
 158                  }
 159                  echo $j;
 160                  if ( ($i != $page) || ((!$more) && ($page==1)) )
 161                      echo '</a>';
 162              }
 163              echo $after;
 164          } else {
 165              if ( $more ) {
 166                  echo $before;
 167                  $i = $page - 1;
 168                  if ( $i && $more ) {
 169                      if ( '' == get_settings('permalink_structure') )
 170                          echo '<a href="' . get_permalink() . '&amp;page=' . $i . '">'.$previouspagelink.'</a>';
 171                      else
 172                          echo '<a href="' . get_permalink() . $i . '/">'.$previouspagelink.'</a>';
 173                  }
 174                  $i = $page + 1;
 175                  if ( $i <= $numpages && $more ) {
 176                      if ( '' == get_settings('permalink_structure') )
 177                          echo '<a href="'.get_permalink() . '&amp;page=' . $i . '">'.$nextpagelink.'</a>';
 178                      else
 179                          echo '<a href="'.get_permalink().$i.'/">'.$nextpagelink.'</a>';
 180                  }
 181                  echo $after;
 182              }
 183          }
 184      }
 185  }
 186  
 187  
 188  //
 189  // Post-meta: Custom per-post fields.
 190  //
 191  
 192  
 193  function post_custom( $key = '' ) {
 194      $custom = get_post_custom();
 195  
 196      if ( 1 == count($custom[$key]) )
 197          return $custom[$key][0];
 198      else
 199          return $custom[$key];
 200  }
 201  
 202  
 203  // this will probably change at some point...
 204  function the_meta() {
 205      global $id, $post_meta_cache;
 206  
 207      if ( $keys = get_post_custom_keys() ) {
 208          echo "<ul class='post-meta'>\n";
 209          foreach ( $keys as $key ) {
 210              $keyt = trim($key);
 211              if ( '_' == $keyt{0} )
 212                  continue;
 213              $values = array_map('trim', get_post_custom_values($key));
 214              $value = implode($values,', ');
 215              echo "<li><span class='post-meta-key'>$key:</span> $value</li>\n";
 216          }
 217          echo "</ul>\n";
 218      }
 219  }
 220  
 221  
 222  //
 223  // Pages
 224  //
 225  
 226  function wp_dropdown_pages($args = '') {
 227      if ( is_array($args) )
 228          $r = &$args;
 229      else
 230          parse_str($args, $r);
 231  
 232      $defaults = array('depth' => 0, 'child_of' => 0, 'selected' => 0, 'echo' => 1,
 233          'name' => 'page_id', 'show_option_none' => '');
 234      $r = array_merge($defaults, $r);
 235      extract($r);
 236  
 237      $pages = get_pages($r);
 238      $output = '';
 239  
 240      if ( ! empty($pages) ) {
 241          $output = "<select name='$name'>\n";
 242          if ( $show_option_none )
 243              $output .= "\t<option value=''>$show_option_none</option>\n";
 244          $output .= walk_page_dropdown_tree($pages, $depth, $r);
 245          $output .= "</select>\n";
 246      }
 247  
 248      $output = apply_filters('wp_dropdown_pages', $output);
 249  
 250      if ( $echo )
 251          echo $output;
 252  
 253      return $output;
 254  }
 255  
 256  function wp_list_pages($args = '') {
 257      if ( is_array($args) )
 258          $r = &$args;
 259      else
 260          parse_str($args, $r);
 261  
 262      $defaults = array('depth' => 0, 'show_date' => '', 'date_format' => get_settings('date_format'),
 263          'child_of' => 0, 'title_li' => __('Pages'), 'echo' => 1);
 264      $r = array_merge($defaults, $r);
 265  
 266      $output = '';
 267  
 268      // Query pages.
 269      $pages = get_pages($r);
 270  
 271      if ( !empty($pages) ) {
 272          if ( $r['title_li'] )
 273              $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
 274  
 275          global $wp_query;
 276          $current_page = $wp_query->get_queried_object_id();
 277          $output .= walk_page_tree($pages, $r['depth'], $current_page, $r['show_date'], $r['date_format']);
 278  
 279          if ( $r['title_li'] )
 280              $output .= '</ul></li>';
 281      }
 282  
 283      $output = apply_filters('wp_list_pages', $output);
 284  
 285      if ( $r['echo'] )
 286          echo $output;
 287      else
 288          return $output;
 289  }
 290  
 291  //
 292  // Page helpers
 293  //
 294  
 295  function walk_page_tree() {
 296      $walker = new Walker_Page;
 297      $args = func_get_args();
 298      return call_user_func_array(array(&$walker, 'walk'), $args);
 299  }
 300  
 301  function walk_page_dropdown_tree() {
 302      $walker = new Walker_PageDropdown;
 303      $args = func_get_args();
 304      return call_user_func_array(array(&$walker, 'walk'), $args);
 305  }
 306  
 307  //
 308  // Attachments
 309  //
 310  
 311  function the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
 312      echo get_the_attachment_link($id, $fullsize, $max_dims);
 313  }
 314  
 315  function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
 316      $id = (int) $id;
 317      $_post = & get_post($id);
 318  
 319      if ( ('attachment' != $_post->post_type) || ('' == $_post->guid) )
 320          return __('Missing Attachment');
 321  
 322      if (! empty($_post->guid) ) {
 323          $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
 324  
 325          return "<a href=\"{$_post->guid}\" title=\"{$_post->post_title}\" >{$innerHTML}</a>";
 326  
 327      } else {
 328          $p .= __('Missing attachment');
 329      }
 330      return $p;
 331  }
 332  
 333  function get_attachment_icon($id = 0, $fullsize = false, $max_dims = false) {
 334      $id = (int) $id;
 335      $post = & get_post($id);
 336  
 337      $mime = $post->post_mime_type;
 338  
 339      $imagedata = get_post_meta($post->ID, '_wp_attachment_metadata', true);
 340  
 341      $file = get_post_meta($post->ID, '_wp_attached_file', true);
 342  
 343      if ( !$fullsize && !empty($imagedata['thumb'])
 344              && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file))
 345              && file_exists($thumbfile) ) {
 346  
 347          // We have a thumbnail desired, specified and existing
 348  
 349          $src = str_replace(basename($post->guid), $imagedata['thumb'], $post->guid);
 350          $src_file = $thumbfile;
 351          $class = 'attachmentthumb';
 352  
 353      } elseif ( substr($mime, 0, 6) == 'image/'
 354              && file_exists($file) ) {
 355  
 356          // We have an image without a thumbnail
 357  
 358          $src = $post->guid;
 359          $src_file = & $file;
 360          $class = 'attachmentimage';
 361      } elseif (! empty($mime) ) {
 362  
 363          // No thumb, no image. We'll look for a mime-related icon instead.
 364          $icon_dir = apply_filters('icon_dir', get_template_directory().'/images');
 365          $icon_dir_uri = apply_filters('icon_dir_uri', get_template_directory_uri().'/images');
 366  
 367          $types = array(substr($mime, 0, strpos($mime, '/')), substr($mime, strpos($mime, '/') + 1), str_replace('/', '_', $mime));
 368          $exts = array('jpg', 'gif', 'png');
 369          foreach ($types as $type) {
 370              foreach ($exts as $ext) {
 371                  $src_file = "$icon_dir/$type.$ext";
 372                  if ( file_exists($src_file) ) {
 373                      $src = "$icon_dir_uri/$type.$ext";
 374                      break 2;
 375                  }
 376              }
 377          }
 378      }
 379  
 380      if (! isset($src) )
 381          return false;
 382  
 383      // Do we need to constrain the image?
 384      if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {
 385  
 386          $imagesize = getimagesize($src_file);
 387  
 388          if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
 389              $actual_aspect = $imagesize[0] / $imagesize[1];
 390              $desired_aspect = $max_dims[0] / $max_dims[1];
 391  
 392              if ( $actual_aspect >= $desired_aspect ) {
 393                  $height = $actual_aspect * $max_dims[0];
 394                  $constraint = "width=\"{$max_dims[0]}\" ";
 395                  $post->iconsize = array($max_dims[0], $height);
 396              } else {
 397                  $width = $max_dims[1] / $actual_aspect;
 398                  $constraint = "height=\"{$max_dims[1]}\" ";
 399                  $post->iconsize = array($width, $max_dims[1]);
 400              }
 401          } else {
 402              $post->iconsize = array($imagesize[0], $imagesize[1]);
 403          }
 404      }
 405  
 406      $icon = "<img src=\"{$src}\" title=\"{$post->post_title}\" alt=\"{$post->post_title}\" {$constraint}/>";
 407  
 408      return apply_filters('attachment_icon', $icon, $post->ID);
 409  }
 410  
 411  function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) {
 412      $id = (int) $id;
 413  
 414      if ( $innerHTML = get_attachment_icon($id, $fullsize, $max_dims))
 415          return $innerHTML;
 416  
 417      $post = & get_post($id);
 418  
 419      $innerHTML = $post->post_title;
 420  
 421      return apply_filters('attachment_innerHTML', $innerHTML, $post->ID);
 422  }
 423  
 424  function prepend_attachment($content) {
 425      $p = '<p class="attachment">';
 426      $p .= get_the_attachment_link(false, true, array(400, 300));
 427      $p .= '</p>';
 428      $p = apply_filters('prepend_attachment', $p);
 429  
 430      return "$p\n$content";
 431  }
 432  
 433  //
 434  // Misc
 435  //
 436  
 437  function get_the_password_form() {
 438      $output = '<form action="' . get_settings('siteurl') . '/wp-pass.php" method="post">
 439      <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
 440      <p><label>' . __("Password:") . ' <input name="post_password" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . __("Submit") . '" /></p>
 441      </form>
 442      ';
 443      return $output;
 444  }
 445  
 446  ?>