root/tags/2.1.1/wp-includes/post-template.php

Revision 4864, 11.7 kB (checked in by ryan, 2 years ago)

Check page ID only if is_page. fixes #3049

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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     if ( $page > count($pages) ) // if the requested page doesn't exist
80         $page = count($pages); // give them the highest numbered page that DOES exist
81
82     $content = $pages[$page-1];
83     if ( preg_match('/<!--more(.+?)?-->/', $content, $matches) ) {
84         $content = explode($matches[0], $content, 2);
85         if ( !empty($matches[1]) && !empty($more_link_text) )
86             $more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
87     } else {
88         $content = array($content);
89     }
90     if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
91         $stripteaser = 1;
92     $teaser = $content[0];
93     if ( ($more) && ($stripteaser) )
94         $teaser = '';
95     $output .= $teaser;
96     if ( count($content) > 1 ) {
97         if ( $more ) {
98             $output .= '<a id="more-'.$id.'"></a>'.$content[1];
99         } else {
100             $output = balanceTags($output);
101             if ( ! empty($more_link_text) )
102                 $output .= ' <a href="'. get_permalink() . "#more-$id\" class=\"more-link\">$more_link_text</a>";
103         }
104             
105     }
106     if ( $preview ) // preview fix for javascript bug with foreign languages
107         $output =    preg_replace('/\%u([0-9A-F]{4,4})/e',    "'&#'.base_convert('\\1',16,10).';'", $output);
108
109     return $output;
110 }
111
112
113 function the_excerpt() {
114     echo apply_filters('the_excerpt', get_the_excerpt());
115 }
116
117
118 function get_the_excerpt($fakeit = true) {
119     global $id, $post;
120     $output = '';
121     $output = $post->post_excerpt;
122     if ( !empty($post->post_password) ) { // if there's a password
123         if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) {  // and it doesn't match the cookie
124             $output = __('There is no excerpt because this is a protected post.');
125             return $output;
126         }
127     }
128
129     return apply_filters('get_the_excerpt', $output);
130 }
131
132
133 function wp_link_pages($args = '') {
134     if ( is_array($args) )
135         $r = &$args;
136     else
137         parse_str($args, $r);
138
139     $defaults = array('before' => '<p>' . __('Pages:'), 'after' => '</p>', 'next_or_number' => 'number', 'nextpagelink' => __('Next page'),
140             'previouspagelink' => __('Previous page'), 'pagelink' => '%', 'more_file' => '', 'echo' => 1);
141     $r = array_merge($defaults, $r);
142     extract($r);
143
144     global $id, $page, $numpages, $multipage, $more, $pagenow;
145     if ( $more_file != '' )
146         $file = $more_file;
147     else
148         $file = $pagenow;
149
150     $output = '';
151     if ( $multipage ) {
152         if ( 'number' == $next_or_number ) {
153             $output .= $before;
154             for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
155                 $j = str_replace('%',"$i",$pagelink);
156                 $output .= ' ';
157                 if ( ($i != $page) || ((!$more) && ($page==1)) ) {
158                     if ( '' == get_option('permalink_structure') )
159                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">';
160                     else
161                         $output .= '<a href="' . trailingslashit(get_permalink()) . $i . '/">';
162                 }
163                 $output .= $j;
164                 if ( ($i != $page) || ((!$more) && ($page==1)) )
165                     $output .= '</a>';
166             }
167             $output .= $after;
168         } else {
169             if ( $more ) {
170                 $output .= $before;
171                 $i = $page - 1;
172                 if ( $i && $more ) {
173                     if ( '' == get_option('permalink_structure') )
174                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">' . $previouspagelink . '</a>';
175                     else
176                         $output .= '<a href="' . get_permalink() . $i . '/">'.$previouspagelink.'</a>';
177                 }
178                 $i = $page + 1;
179                 if ( $i <= $numpages && $more ) {
180                     if ( '' == get_option('permalink_structure') )
181                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">'.$nextpagelink.'</a>';
182                     else
183                         $output .= '<a href="' . trailingslashit(get_permalink()) . $i . '/">' . $nextpagelink . '</a>';
184                 }
185                 $output .= $after;
186             }
187         }
188     }
189
190     if ( $echo )
191         echo $output;
192
193     return $output;
194 }
195
196
197 //
198 // Post-meta: Custom per-post fields.
199 //
200
201
202 function post_custom( $key = '' ) {
203     $custom = get_post_custom();
204
205     if ( 1 == count($custom[$key]) )
206         return $custom[$key][0];
207     else
208         return $custom[$key];
209 }
210
211
212 // this will probably change at some point...
213 function the_meta() {
214     global $id;
215
216     if ( $keys = get_post_custom_keys() ) {
217         echo "<ul class='post-meta'>\n";
218         foreach ( $keys as $key ) {
219             $keyt = trim($key);
220             if ( '_' == $keyt{0} )
221                 continue;
222             $values = array_map('trim', get_post_custom_values($key));
223             $value = implode($values,', ');
224             echo "<li><span class='post-meta-key'>$key:</span> $value</li>\n";
225         }
226         echo "</ul>\n";
227     }
228 }
229
230
231 //
232 // Pages
233 //
234
235 function wp_dropdown_pages($args = '') {
236     if ( is_array($args) )
237         $r = &$args;
238     else
239         parse_str($args, $r);
240
241     $defaults = array('depth' => 0, 'child_of' => 0, 'selected' => 0, 'echo' => 1,
242         'name' => 'page_id', 'show_option_none' => '');
243     $r = array_merge($defaults, $r);
244     extract($r);
245
246     $pages = get_pages($r);
247     $output = '';
248
249     if ( ! empty($pages) ) {
250         $output = "<select name='$name'>\n";
251         if ( $show_option_none )
252             $output .= "\t<option value=''>$show_option_none</option>\n";
253         $output .= walk_page_dropdown_tree($pages, $depth, $r);
254         $output .= "</select>\n";
255     }
256
257     $output = apply_filters('wp_dropdown_pages', $output);
258
259     if ( $echo )
260         echo $output;
261
262     return $output;
263 }
264
265 function wp_list_pages($args = '') {
266     if ( is_array($args) )
267         $r = &$args;
268     else
269         parse_str($args, $r);
270
271     $defaults = array('depth' => 0, 'show_date' => '', 'date_format' => get_option('date_format'),
272         'child_of' => 0, 'exclude' => '', 'title_li' => __('Pages'), 'echo' => 1, 'authors' => '');
273     $r = array_merge($defaults, $r);
274
275     $output = '';
276     $current_page = 0;
277
278     // sanitize, mostly to keep spaces out
279     $r['exclude'] = preg_replace('[^0-9,]', '', $r['exclude']);
280
281     // Allow plugins to filter an array of excluded pages
282     $r['exclude'] = implode(',', apply_filters('wp_list_pages_excludes', explode(',', $r['exclude'])));
283
284     // Query pages.
285     $pages = get_pages($r);
286
287     if ( !empty($pages) ) {
288         if ( $r['title_li'] )
289             $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
290
291         global $wp_query;
292         if ( is_page() )
293             $current_page = $wp_query->get_queried_object_id();
294         $output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
295
296         if ( $r['title_li'] )
297             $output .= '</ul></li>';
298     }
299
300     $output = apply_filters('wp_list_pages', $output);
301
302     if ( $r['echo'] )
303         echo $output;
304     else
305         return $output;
306 }
307
308 //
309 // Page helpers
310 //
311
312 function walk_page_tree() {
313     $walker = new Walker_Page;
314     $args = func_get_args();
315     return call_user_func_array(array(&$walker, 'walk'), $args);
316 }
317
318 function walk_page_dropdown_tree() {
319     $walker = new Walker_PageDropdown;
320     $args = func_get_args();
321     return call_user_func_array(array(&$walker, 'walk'), $args);
322 }
323
324 //
325 // Attachments
326 //
327
328 function the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
329     echo get_the_attachment_link($id, $fullsize, $max_dims);
330 }
331
332 function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
333     $id = (int) $id;
334     $_post = & get_post($id);
335
336     if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url() )
337         return __('Missing Attachment');
338
339     $post_title = attribute_escape($_post->post_title);
340
341     $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
342     return "<a href='$url' title='$post_title'>$innerHTML</a>";
343 }
344
345 function get_attachment_icon_src( $id = 0, $fullsize = false ) {
346     $id = (int) $id;
347     if ( !$post = & get_post($id) )
348         return false;
349
350     $imagedata = wp_get_attachment_metadata( $post->ID );
351
352     $file = get_attached_file( $post->ID );
353
354     if ( !$fullsize && $thumbfile = wp_get_attachment_thumb_file( $post->ID ) ) {
355         // We have a thumbnail desired, specified and existing
356
357         $src = wp_get_attachment_thumb_url( $post->ID );
358         $src_file = $thumbfile;
359         $class = 'attachmentthumb';
360     } elseif ( wp_attachment_is_image( $post->ID ) ) {
361         // We have an image without a thumbnail
362
363         $src = wp_get_attachment_url( $post->ID );
364         $src_file = & $file;
365         $class = 'attachmentimage';
366     } elseif ( $src = wp_mime_type_icon( $post->ID ) ) {
367         // No thumb, no image. We'll look for a mime-related icon instead.
368
369         $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' );
370         $src_file = $icon_dir . '/' . basename($src);
371     }
372
373     if ( !isset($src) || !$src )
374         return false;
375
376     return array($src, $src_file);
377 }
378
379 function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) {
380     $id = (int) $id;
381     if ( !$post = & get_post($id) )
382         return false;
383
384     if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) )
385         return false;
386
387     list($src, $src_file) = $src;
388
389     // Do we need to constrain the image?
390     if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {
391
392         $imagesize = getimagesize($src_file);
393
394         if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
395             $actual_aspect = $imagesize[0] / $imagesize[1];
396             $desired_aspect = $max_dims[0] / $max_dims[1];
397
398             if ( $actual_aspect >= $desired_aspect ) {
399                 $height = $actual_aspect * $max_dims[0];
400                 $constraint = "width='{$max_dims[0]}' ";
401                 $post->iconsize = array($max_dims[0], $height);
402             } else {
403                 $width = $max_dims[1] / $actual_aspect;
404                 $constraint = "height='{$max_dims[1]}' ";
405                 $post->iconsize = array($width, $max_dims[1]);
406             }
407         } else {
408             $post->iconsize = array($imagesize[0], $imagesize[1]);
409         }
410     }
411
412     $post_title = attribute_escape($post->post_title);
413
414     $icon = "<img src='$src' title='$post_title' alt='$post_title' $constraint/>";
415
416     return apply_filters( 'attachment_icon', $icon, $post->ID );
417 }
418
419 function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) {
420     $id = (int) $id;
421     if ( !$post = & get_post($id) )
422         return false;
423
424     if ( $innerHTML = get_attachment_icon($post->ID, $fullsize, $max_dims))
425         return $innerHTML;
426
427
428     $innerHTML = attribute_escape($post->post_title);
429
430     return apply_filters('attachment_innerHTML', $innerHTML, $post->ID);
431 }
432
433 function prepend_attachment($content) {
434     $p = '<p class="attachment">';
435     $p .= get_the_attachment_link(false, true, array(400, 300));
436     $p .= '</p>';
437     $p = apply_filters('prepend_attachment', $p);
438
439     return "$p\n$content";
440 }
441
442 //
443 // Misc
444 //
445
446 function get_the_password_form() {
447     $output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
448     <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
449     <p><label>' . __("Password:") . ' <input name="post_password" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . __("Submit") . '" /></p>
450     </form>
451     ';
452     return $output;
453 }
454
455 ?>
456
Note: See TracBrowser for help on using the browser.