简体   繁体   English

如何自动为发布到 WordPress 的内容为空的帖子添加随机描述?

[英]How can I automatically add random description to posts with empty content posted to WordPress?

I would like to automate the process of adding descriptions for articles published on my site.我想自动化为我网站上发布的文章添加描述的过程。 I would like to automatically insert a random description from a list of pre-edited (Advanced Custom Fields) descriptions when a new post is published as draft .当新帖子作为草稿发布时,我想自动从预先编辑的(高级自定义字段)描述列表中插入随机描述。 eg: "description_1" or "description_2" or "description_100" (randomly)例如:“description_1”或“description_2”或“description_100”(随机)

I have this code, which seems to do the trick, but I still have to insert the parameter to randomize the choice of description (randomly choose a number between 1 and 100 just after the word "description") on the line: // Custom post content $content =.我有这段代码,这似乎可以解决问题,但我仍然需要插入参数以随机选择描述(在“描述”一词之后随机选择 1 到 100 之间的数字)://自定义发布内容 $content =。 $description_1. $描述_1。 ; ; of this code above.上面这段代码。 I would also like to add these two little conditions:我还想添加这两个小条件:

  • The content of the article is empty文章内容为空
  • The status of the article is set to "draft" Thanks for any response!文章状态设置为“草稿” 感谢您的回复!
function show_update_postdata( $value, $post_id, $field ) {

        // Get values from POST
        $description_1 = $_POST['acf']['field_5dd00e0582125'];
        $description_2 = $_POST['acf']['field_5dd00e0582125'];
        $description_50 = $_POST['acf']['field_5dd00e0582125'];
        $description_100 = $_POST['acf']['field_5dd00e0582125'];

        // Custom post content
        $content = . $description_1 . ;
        $postdata = array(
             'ID'          => $post_id,
             'post_content'  => $content,
             'post_type'   => 'post',
        );

        wp_update_post( $postdata );
        return $value;

}
add_filter('acf/update_value/name=description_1', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_2', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_50', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_100', 'show_update_postdata', 10, 3);``` 

You can use two fucntion fisrt for content get_post_field() and the second is for check post status get_post_status() .您可以对内容使用两个功能get_post_field() ,第二个用于检查帖子状态get_post_status() check below code.检查下面的代码。

function show_update_postdata( $value, $post_id, $field ) {

    $content = apply_filters( 'the_content', get_post_field( 'post_content', $post_id ) );

    if( $content == '' && get_post_status( $post_id ) == 'draft' ){

        // Get values from POST
        $description_1 = $_POST['acf']['field_5dd00e0582125'];
        $description_2 = $_POST['acf']['field_5dd00e0582125'];
        $description_50 = $_POST['acf']['field_5dd00e0582125'];
        $description_100 = $_POST['acf']['field_5dd00e0582125'];

        $random_content = array( $description_1, $description_2, $description_50, $description_100 );
        $random_content = $random_content[ array_rand( $random_content ) ];

        $postdata = array(
             'ID'            => $post_id,
             'post_content'  => $random_content,
             'post_type'     => 'post',
        );

        wp_update_post( $postdata );
    }
    
    return $value;


}
add_filter('acf/update_value/name=description_1', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_2', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_50', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_100', 'show_update_postdata', 10, 3);

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

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