PHP 显示特定的WordPress帖子/页面
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 显示特定的WordPress帖子/页面相关的知识,希望对你有一定的参考价值。
<?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);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text, '<a>');
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$words = preg_split('/(<a.*?a>)|\n|\r|\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
}
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&raquo;", $tags = '<a>', $permalink = null) {
global $post;
$excerpt = strip_tags($excerpt, $tags);
$excerpt = strip_shortcodes($excerpt);
$string_check = explode(' ', $excerpt);
if ($permalink === null) {
$permalink = get_permalink();
}
if (count($string_check, COUNT_RECURSIVE) > $excerpt_length) {
$new_excerpt_words = explode(' ', $excerpt, $excerpt_length+1);
array_pop($new_excerpt_words);
$excerpt_text = implode(' ', $new_excerpt_words);
$temp_content = strip_tags($excerpt_text, $tags);
$short_content = preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s','',$temp_content);
$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(']]>', ']]&gt;', 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&raquo;', '<a>', get_permalink($my_post_id) ); ?></p>
<!-- this will output all of the content from that post/page -->
<?php echo $my_content; ?>
以上是关于PHP 显示特定的WordPress帖子/页面的主要内容,如果未能解决你的问题,请参考以下文章
Wordpress 类别模板显示来自所有类别的帖子,而不是特定类别的帖子
控制 wordpress 存档页面上的帖子数量
PHP 特定页面的Wordpress查询帖子
使用下拉菜单动态过滤 Wordpress 帖子(使用 php 和 ajax)
PHP Wordpress - 仅在特定类别的第一页上显示精选帖子
PHP Wordpress:在任何Wordpress页面上显示最新的博客帖子