显示特定的WordPress帖子/页面
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了显示特定的WordPress帖子/页面相关的知识,希望对你有一定的参考价值。
Code used to get the content, title, excerpt, etc for a specific page or post in WordPress. Replace the post id with your own specific ID.
<?php /** * put the following into functions.php **/ function new_wp_trim_excerpt($text) { $raw_excerpt = $text; if ( '' == $text ) { $text = get_the_content(''); $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split('/(<a.*?a>)| | | |s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE ); $text = $text . $excerpt_more; } else { } } return apply_filters('new_wp_trim_excerpt', $text, $raw_excerpt); } remove_filter('get_the_excerpt', 'wp_trim_excerpt'); add_filter('get_the_excerpt', 'new_wp_trim_excerpt'); function my_excerpt($excerpt = '', $excerpt_length = 50, $readmore = "Read more»", $tags = '<a>', $permalink = null) { global $post; $excerpt = strip_shortcodes($excerpt); if ($permalink === null) { $permalink = get_permalink(); } $short_content .= '... <a href="' . $permalink . '">' . $readmore . '</a>'; return $short_content; } else { return $excerpt . ' <a href="' . $permalink . '">' . $readmore . '</a>'; } } ?> <?php /** * put the following in your template and change the post id **/ ?> <?php $my_post_id = 495; /* change this to match the post you are trying to retreive */ ?> <?php $my_post = get_post($my_post_id); ?> <h2><?php echo $my_post->post_title; ?></h2> <?php $my_content = str_replace(']]>', ']]>', apply_filters('the_content', $my_post->post_content)); ?> <!-- this will output the content in excerpt format. change the '80' to whatever length you want. --> <p><?php echo my_excerpt($my_content, 80, 'Read more»', '<a>', get_permalink($my_post_id) ); ?></p> <!-- this will output all of the content from that post/page --> <?php echo $my_content; ?>
以上是关于显示特定的WordPress帖子/页面的主要内容,如果未能解决你的问题,请参考以下文章