简体   繁体   English

WordPress的get_the_excerpt而不是$ post-> post_excerpt

[英]Wordpress get_the_excerpt instead of $post->post_excerpt

I'm trying to replace $post->post_content in the function below with get_the_excerpt that simply deducts an excerpt from the_content on the fly . 我正在尝试使用get_the_excerpt替换下面函数中的$post->post_content ,该方法简单地从the_content摘录。

Original function: 原始功能:

function getExcerpt($post) {
    $summary = apply_filters('the_content', $post->post_content);
    return apply_filters("mc-message", $summary);
}

I could have replaced $post->post_content with $post->post_excerpt but that would return nothing as there is no hard-corded excerpt entered in the Wordpress editor's excerpt meta box. 我可以将$post->post_content替换$post->post_content $post->post_excerpt但不会返回任何内容,因为在Wordpress编辑器的摘录元框中没有输入任何硬性摘录。 Read that get_the_excerpt creates a summary out of the post content. 读到get_the_excerpt会根据帖子内容创建摘要。 But it didn't pass any value 但它没有任何价值

function getExcerpt($post) {
    $deducted_excerpt = get_the_excerpt();
    $summary = apply_filters('the_content', $deducted_excerpt);
    return apply_filters("mc-message", $summary);
}

Yes, you are right. 是的,你是对的。 get_the_excerpt generates word-counted trimmed-down version of the full post content, when there is no user-supplied excerpt. 如果没有用户提供的摘录,则get_the_excerpt生成完整帖子内​​容的字数精简版本。 But here, you are not using get_the_excerpt inside the loop. 但是在这里,您没有在循环内使用get_the_excerpt So, you will need to pass a post object or post ID as parameter to fetch the excerpt. 因此,您将需要传递post对象或post ID作为参数以摘录。

From codex: 从法典:

If this function is used outside The Loop and the post doesn't have a custom excerpt, this function will use wp_trim_excerpt() to generate an excerpt. 如果在The Loop之外使用此函数,并且帖子没有自定义摘录,则此函数将使用wp_trim_excerpt()生成摘录。 That function uses get_the_content() , which must be used with The Loop and will cause problems if get_the_excerpt() is being used outside The Loop. 该函数使用get_the_content()它必须与回路一起使用,并且如果将导致问题get_the_excerpt()被循环外使用。 In order to avoid the issues, use setup_postdata() prior to calling get_the_excerpt() to set up the global $post object. 为了避免这些问题,使用setup_postdata()之前调用get_the_excerpt()建立全球$post对象。

Your code should be: 您的代码应为:

function getExcerpt($post) {
    $deducted_excerpt = get_the_excerpt($post);//<==== see here. $post object is passed as parameter.
    $summary = apply_filters('the_content', $deducted_excerpt);
    return apply_filters("mc-message", $summary);
}

More details on get_the_excerpt() function 有关get_the_excerpt()函数的更多详细信息

I had initially passed the post id to it and still didn't return the truncated content excerpt I was hoping for. 我最初将帖子ID传递给了它,但仍然没有返回我希望的截断的内容摘录。 Since the Codex suggests using the seup_postdata , I preceded my function with that. 由于食典建议使用seup_postdata ,因此我在函数之前添加了它。 And solved the issue: 并解决了问题:

function getExcerpt($post) {
    setup_postdata($post);
    $deducted_excerpt = custom_excerpt($post);
    $summary = apply_filters('the_content', $deducted_excerpt);
    return apply_filters("mc-message", $summary);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM