Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我的自定义获取摘录()可以';无法通过ID获取摘录_Php_Wordpress_Function - Fatal编程技术网

Php 我的自定义获取摘录()可以';无法通过ID获取摘录

Php 我的自定义获取摘录()可以';无法通过ID获取摘录,php,wordpress,function,Php,Wordpress,Function,在我的single.php页面上,我试图创建一个函数,通过ID为我提供特定文章的自定义长度摘录 下面是我正在运行的两个函数 /* Custom get_the_excerpt to allow getting Post excerpt by ID */ function custom_get_the_excerpt($post_id) { global $post; $save_post = $post; $post = get_post($post_id); $output =

在我的single.php页面上,我试图创建一个函数,通过ID为我提供特定文章的自定义长度摘录

下面是我正在运行的两个函数

/* Custom get_the_excerpt to allow getting Post excerpt by ID */
function custom_get_the_excerpt($post_id) {
  global $post;
  $save_post = $post;
  $post = get_post($post_id);
  $output = get_the_excerpt($post);
  $post = $save_post;
  return $output;
}

/* Change Excerpt length */
function excerpt($num, $post_id = '') {
    $limit = $num+1;
    $excerpt = explode(' ', custom_get_the_excerpt($post_id), $limit);
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt)."&#8230";
    echo $excerpt;
}
我用什么来调用函数

<?php $previous = get_previous_post();
echo excerpt('30', $previous -> ID); ?>

仍然没有更改。

以下函数获得两个参数: -$num:要显示的字符数 -$post\u id:要获取内容的帖子的id

以及: -如果内容中有标记,则返回文本,直到标记 -如果$num=0,则返回内容直到第一个句号 否则,返回$num上指定的字符数,并在字符串末尾添加“…”

function my_custom_excerpt($num = 0,$post_id=''){
$post=get_post($post_id);
$content=$post->post_content;
if(strpos($content,'&lt;!&#8211;more&#8211;&gt;')>0){
    return substr($content,0,strpos($content,'&lt;!&#8211;more&#8211;&gt;'));
}else{
    if($num===0){
        return substr($content,0,strpos($content,'.')+1);
    }else{
        return substr($content,0,$num).((strlen($content)>$num)?"...":"");
    }
}
}
添加设置_postdata($post);修正了我的问题

function custom_get_the_excerpt($post_id) {
  global $post;
  $save_post = $post;
  $post = get_post($post_id);
  setup_postdata($post);
  $output = get_the_excerpt($post);
  wp_reset_postdata();
  $post = $save_post;
  return $output;
}

echo摘录('30',$previous->ID)中,在“->”附近有空格,这是打字错误吗我很确定这不会起作用-我怀疑这会导致自定义调用中未定义$post_id-因此它都使用当前的$post。另外,我很确定你不必费心于
get\u post
-只需传递你的post\u id就可以获得\u摘录。没有变化,周围没有空格“->”同样,你是对的,我可以传递$post\u id,但这也不会改变问题(
function custom_get_the_excerpt($post_id) {
  global $post;
  $save_post = $post;
  $post = get_post($post_id);
  setup_postdata($post);
  $output = get_the_excerpt($post);
  wp_reset_postdata();
  $post = $save_post;
  return $output;
}