简体   繁体   English

Wordpress:无法在functions.php中检索帖子内容

[英]Wordpress: Unable to retrieve post content in functions.php

I have written something that will send me an email with the contents of a post once it is published. 我写了一些东西,一旦发布,就会给我发送一封包含帖子内容的电子邮件。 I am able to get the title, category, author and permalink from the post and show it in the mail, but not the content! 我可以从帖子中获取标题,类别,作者和永久链接,并将其显示在邮件中,但不能显示内容!

<?php

// Function that runs when a post is published
function run_when_post_published() {

    $post = get_post($post_id);
    $author = get_userdata($post->post_author);
    $category = get_the_category();
    $author_id = $post->post_author;
    $author = get_the_author_meta( 'nickname', $author_id ); 
    $title = get_the_title();
    $permalink = get_permalink();
    $my_content = get_the_content();

    $email_subject = '(' . $category[0]->cat_name . ') Case fra kundelogg: ' . $title;
    $message = $my_content . 'Written by: ' . $author . ', Link: ' . $permalink;
    $headers = 'From: CRM System <mail@mail.com>';

// Advanced custom field to check if post is supposed to be mailed or not
if( !empty($_POST['fields']['field_52fc8615d3526'] ) ) {

    $email = 'myemail@mycompany.com';
    wp_mail( $email, $email_subject, $message );

}
else { // Do nothing }

}

// Makes sure email only gets sent the first time a post is published
add_action('new_to_publish', 'run_when_post_published');        
add_action('draft_to_publish', 'run_when_post_published');      
add_action('pending_to_publish', 'run_when_post_published');
?>

I have tried numerous ways of getting the content to show up in the mail, and in some cases i acctually got between two to six characters to show up. 我尝试了多种方法来使内容显示在邮件中,在某些情况下,我偶然发现了2至6个字符。 But not the whole content. 但不是全部内容。 It is like the content is truncated randomly, but this is not the case. 就像内容被随机截断一样,但事实并非如此。 The example in the code above with get_the_content() does not show anything though. 上面代码中带有get_the_content()的示例未显示任何内容。

Replace $my_content inside $message = ... with apply_filters( 'the_content', $post->post_content ) 将$ message = ...中的$ my_content替换为apply_filters apply_filters( 'the_content', $post->post_content )

$message = apply_filters( 'the_content', $post->post_content ) . ' Written by: ' . $author . ', Link: ' . $permalink;

Remove $post = get_post( $post_id ); 删除$post = get_post( $post_id );

Add $post as a function argument. 添加$ post作为函数参数。

In the final version below I have made the changes I listed above however I haven't made any attempt to cleanup your code. 在下面的最终版本中,我进行了上面列出的更改,但是我并未尝试清理您的代码。 Here's the full version: 这是完整版本:

<?php

// Function that runs when a post is published
function run_when_post_published( $post ) {
    $author = get_userdata($post->post_author);
    $category = get_the_category();
    $author_id = $post->post_author;
    $author = get_the_author_meta( 'nickname', $author_id ); 
    $title = get_the_title();
    $permalink = get_permalink();

    $email_subject = '(' . $category[0]->cat_name . ') Case fra kundelogg: ' . $title;
    $message = apply_filters( 'the_content', $post->post_content ) . 'Written by: ' . $author . ', Link: ' . $permalink;
    $headers = 'From: CRM System <mail@mail.com>';

    // Advanced custom field to check if post is supposed to be mailed or not
    if( !empty($_POST['fields']['field_52fc8615d3526'] ) ) {

        $email = 'myemail@mycompany.com';
        wp_mail( $email, $email_subject, $message );

    }
}

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

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