Php wordpress中的博客摘录
我已经购买了一个主题,博客摘录在他们的演示网站上运行良好,但在我的网站的博客概述页面上没有。 这是我找到的代码,但这是wordpress post-template.php 这个主题没有任何博客设置,这让我觉得这是一个wordpress问题 希望有人能帮忙 非常感谢 Php新手,请解释我如何解决这个问题Php wordpress中的博客摘录,php,wordpress,Php,Wordpress,我已经购买了一个主题,博客摘录在他们的演示网站上运行良好,但在我的网站的博客概述页面上没有。 这是我找到的代码,但这是wordpress post-template.php 这个主题没有任何博客设置,这让我觉得这是一个wordpress问题 希望有人能帮忙 非常感谢 Php新手,请解释我如何解决这个问题 function get_the_excerpt( $deprecated = '' ) { if ( !empty( $deprecated ) ) _deprecat
function get_the_excerpt( $deprecated = '' ) {
if ( !empty( $deprecated ) )
_deprecated_argument( __FUNCTION__, '2.3' );
$post = get_post();
if ( empty( $post ) ) {
return '';
}
if ( post_password_required() ) {
return __( 'There is no excerpt because this is a protected post.' );
}
/**
* Filter the retrieved post excerpt.
*
* @since 1.2.0
*
* @param string $post_excerpt The post excerpt.
*/
return apply_filters( 'get_the_excerpt', $post->post_excerpt );
}
/**
* Whether post has excerpt.
*
* @since 2.3.0
*
* @param int|WP_Post $id Optional. Post ID or post object.
* @return bool
*/
function has_excerpt( $id = 0 ) {
$post = get_post( $id );
return ( !empty( $post->post_excerpt ) );
}
/**
* Display the classes for the post div.
*
* @since 2.7.0
*
* @param string|array $class One or more classes to add to the class list.
* @param int|WP_Post $post_id Optional. Post ID or post object.
*/
function post_class( $class = '', $post_id = null ) {
// Separates classes with a single space, collates classes for post DIV
echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
}
默认情况下,_extract()返回摘录字段中的文本。尽管您可以使用筛选器更改其返回的内容:
add_filter( 'get_the_excerpt', 'my_custom_excerpt' );
当调用_extract()时,它将触发此函数:
function my_custom_excerpt($excerpt) {
if(empty($excerpt)) {
return get_custom_excerpt();
} else {
return $excerpt;
}
}
这样,如果摘录字段为空,将使用自定义摘录。以下是get\u custom\u extract
(它将执行“智能”停止)的代码:
函数打印\u摘录($length)
{
全球$员额;
$text=$post->post_摘录;
如果(''=$text){
$text=获取内容(“”);
$text=apply_filters('the_content',$text);
$text=str_replace(']]>',']]]>',$text);
}
$text=strip\u短代码($text);
$text=strip_标记($text,);//将其更改为要保留的标记
if(preg_match('/^.{1',.$length.'}\b/s',$text,$match))
$text=$match[0];
$extracpt=reverse_strrchr($text,array('.','!'),1100);
如果(节选){
echo apply_过滤器('the_extract',$extract);
}否则{
echo apply_过滤器('the_摘录',$text.'…);
}
}
函数反转\u strrchr($haystack、$pinder、$trail、$offset=0)
{
返回strrposa($haystack,$needle,$offset)?substr($haystack,0,strrposa($haystack,$needle,$offset)+$trail):false;
}
//阵列模式下的strrpos
函数strrposa($haystack,$needers=array(),$offset=0)
{
如果(!is_数组($针))
$TINERS=阵列($TINERS);
foreach($作为$查询){
if(strrpos($haystack,$query,$offset)!==false)
返回strrpos($haystack,$query,$offset);
}
返回false;
}
您在帖子中添加了摘录了吗?嗨,安德鲁,谢谢您的回复,我不知道我必须这么做,我知道它通常会从帖子正文中提取摘录并在概述中生成。我这样想是不是错了?根据文件:-在编辑后的页面上有一个“摘录”框,你需要明确地在其中放一个摘录,这样才能将其提取出来。嗨,安德鲁,我几秒钟前刚刚读过。似乎有三种不同的类型。我需要一个自动生成的博客概述页面上的摘要。谢谢;然后是时候做一些调试了。尝试手动将摘录添加到帖子中,然后查看是否恢复正常。如果是,则自动生成的摘录被破坏。我想说的是,$post->post\u摘录
检查只查找手动添加的摘录,但我对WordPress的内部gubbins有些生疏。嗨,vard,非常感谢你的回答。如果我将它添加到child-theme-functions.php中,或者我需要替换任何现有代码,这会起作用吗?谢谢是的,这些都需要添加到您的子functions.php主题文件中。我已经复制了所有内容并将其添加到functions.php中,但它实际上不起作用(在您的帖子模板文件中,是名为?或?内容?这是我的post-template.php,但它是原始的wordpress文件,而不是主题文件,因为主题没有。
function print_excerpt($length)
{
global $post;
$text = $post->post_excerpt;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
}
$text = strip_shortcodes($text);
$text = strip_tags($text, '<a>'); // change this to the tags you want to keep
if (preg_match('/^.{1,'.$length.'}\b/s', $text, $match))
$text = $match[0];
$excerpt = reverse_strrchr($text, array('.','!'), 1, 100);
if($excerpt) {
echo apply_filters('the_excerpt',$excerpt);
} else {
echo apply_filters('the_excerpt',$text.'...');
}
}
function reverse_strrchr($haystack, $needle, $trail, $offset=0)
{
return strrposa($haystack, $needle, $offset) ? substr($haystack, 0, strrposa($haystack, $needle, $offset) + $trail) : false;
}
// strrpos in Array mode
function strrposa($haystack, $needles=array(), $offset=0)
{
if(!is_array($needles))
$needles = array($needles);
foreach($needles as $query) {
if(strrpos($haystack, $query, $offset) !== false)
return strrpos($haystack, $query, $offset);
}
return false;
}