如何在WordPress中添加自定义文章定时发布功能?

玖比肆 WordPress 29

请提供在WordPress中创建和管理自定义文章定时发布功能的方法。

回复

共1条回复 我来回复
  • 玖比肆的头像
    玖比肆 评论

    方法如下:

    1. 创建一个WordPress主题或子主题。如果您已经有自己的主题,可以跳过此步骤。

    2. 在theme的functions.php中添加以下代码来创建一个自定义日期字段

    ```
    function custom_meta_box_date() {
    add_meta_box(
    'custom_meta_box_date',
    __( 'Custom Publish Date', 'textdomain' ),
    'custom_meta_box_date_callback',
    'post',
    'side',
    'high'
    );
    }
    add_action( 'add_meta_boxes', 'custom_meta_box_date' );

    function custom_meta_box_date_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'custom_meta_box_date_nonce' );
    $date_value = get_post_meta( $post->ID, '_custom_publish_date', true );
    ?>


    60,
    'display' => __( 'Custom Schedule' ),
    );
    }
    return $schedules;
    }
    add_filter( 'cron_schedules', 'custom_cron_schedule' );
    ```

    4. 编写一个函数来处理定时发布

    ```
    function custom_publish_post( $post_id ) {
    $date = get_post_meta( $post_id, '_custom_publish_date', true );
    $now = current_time( 'mysql' );

    if ( ! $date || $date < $now ) { return; } wp_publish_post( $post_id );}```5. 添加一个函数来计划一个发布```function custom_schedule_post( $post_id ) { $date = get_post_meta( $post_id, '_custom_publish_date', true ); if ( ! $date ) { return; } $timestamp = strtotime( $date ); wp_schedule_single_event( $timestamp, 'custom_publish_post', array( $post_id ) );}add_action( 'save_post', 'custom_schedule_post' );```6. 添加一个定时发布计划```function custom_cron_schedule_post() { $args = array( 'post_type' => 'post',
    'post_status' => 'draft',
    'meta_query' => array(
    array(
    'key' => '_custom_publish_date',
    'value' => date( 'Y-m-d H:i:s' ),
    'compare' => '<=', 'type' => 'DATETIME'
    ),
    ),
    );

    $posts = get_posts( $args );

    if ( empty( $posts ) ) {
    return;
    }

    foreach ( $posts as $post ) {
    custom_publish_post( $post->ID );
    }
    }

    add_action( 'custom_publish_post', 'custom_publish_post' );
    add_action( 'wp', 'custom_cron_schedule_post' );
    ```

    这些步骤将在您的WordPress中创建自定义文章定时发布功能。现在,您可以在编辑页面中设置自定义发布日期,当到达该日期时,文章将自动发布。

    2023年 5月 14日 下午4:55 0条评论
微信客服
分享本页
返回顶部
玖比肆(9Bi4)专注于帮助企业实现数字化转型,提供全方位的网站建设、网络运营、网络推广、网络营销和SEO优化等一系列数字化营销服务。