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

Wordpress:无法在functions.php中检索帖子内容,php,wordpress,email,Php,Wordpress,Email,我写了一些东西,一旦发布,就会给我发一封包含帖子内容的电子邮件。我可以从文章中获得标题、类别、作者和永久链接,并在邮件中显示,但不能显示内容 <?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);

我写了一些东西,一旦发布,就会给我发一封包含帖子内容的电子邮件。我可以从文章中获得标题、类别、作者和永久链接,并在邮件中显示,但不能显示内容

<?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');
?>


我已经尝试了很多方法让内容出现在邮件中,在某些情况下,我偶然发现了两到六个字符。但不是全部内容。这就像内容被随机截断,但事实并非如此。上面代码中的示例get_The_content()没有显示任何内容

替换$message=。。。使用
apply_过滤器('the_content',$post->post_content)

删除
$post=get\u post($post\u id)

添加$post作为函数参数

在下面的最终版本中,我做了上面列出的更改,但是我没有尝试清理您的代码。以下是完整版本:

<?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 );

    }
}

谢谢你,内森,成功了!我曾经尝试过在apply_filters(…)中以这种方式使用它,但我肯定做得非常错误!:)剩下的问题是将内容显示为HTML。我尝试使用:add_filter('wp_mail_content_type','set_html_content_type');但我只收到以下错误消息:(下一条注释)警告:call_user_func_array()[function.call user func array]:第一个参数应该是有效的回调,/home/kundelog/public_html/wp includes/plugin.php第199行的/home/kundelg/public_html/wp includes/plugin.php中给出了“set_html_content_type”。在文档中查找wp\u邮件您是否将set\u html\u content\u type定义为函数?将其设置为函数解决了此问题,谢谢!我在这里找到了答案:
<?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 );

    }
}