【问题标题】:Wordpress: Scheduled category change (workflow). How to do?Wordpress:计划的类别更改(工作流程)。怎么做?
【发布时间】:2011-12-23 21:41:21
【问题描述】:

我认为这是一个常见的情况:我有三个类别:Past、Current、Upcoming。

现在我写一篇关于下个月活动的帖子。我把这篇文章放在即将发布的类别中。

我想要的是预定的类别更改。

即:

此活动从 12 月 1 日到 12 月 10 日举行。从现在到 11 月 30 日,此帖子属于即将发布的类别(我在创建此帖子时选择了此类别)。

12 月 1 日,此帖子将自动归入当前类别,直到 12 月 10 日。

12 月 11 日,此帖子将自动归入过去类别。

我搜索了一下,没有找到这样的插件。

基本上,我希望发布页面有两个额外的选项:

选项 1:更改为类别 _ on _

选项 2:更改为类别 _ on _

这听起来像是一个工作流程问题。我搜索了与工作流相关的插件,但仍然没有运气。

关于如何实现这一点的任何建议?我可以写一个插件,但我是 WP 新手。有人可以建议我使用哪些 API/函数吗?

谢谢!

【问题讨论】:

    标签: php cron wordpress schedule


    【解决方案1】:

    首先:可能有一个插件可以处理您想要实现的目标。如果您需要一个简单的活动日历,我几乎可以肯定这可以使用现有的插件来完成。在我的脑海中,MyCalendar 就是这样一个插件。在自己开始编写代码之前,您可能需要搜索 wordpress plugin directory 以获取更多选项。

    话虽如此,如果你不能避免自己构建这个,因为你的情况是专门的,这应该让你开始:

    要么使用custom fields 添加开始和结束日期的额外元数据,要么将事件设为自己的custom post type。详细解释自定义帖子类型的使用超出了简洁 SO 答案的范围。

    如果您选择添加两个名为 startend (或类似)的自定义字段的更简单方法,则必须将 php 脚本作为通过您的服务器进行 cronjob 或让我们使用 WP-Cron Functions 将当前时间与开始和结束日期进行比较,并相应地更改类别。

    为了给你提供一些有用的代码(将进入你自己编写的插件),下面的 php sn-p 应该为你指明正确的方向:

    register_activation_hook(__FILE__, 'your_activation');
    add_action('your_daily_event', 'change_categories');
    
    function your_activation() {
        $first_time = time(); // you probably want this to be shortly after midnight
        $recurrence = 'daily';
        wp_schedule_event($first_time, $recurrence, 'your_daily_event');
    }
    
    function change_categories() {
        $old_name = 'Upcoming'; // category to delete
        $taxonomy = 'category';
        // fetch category ID (amongst other data) of 'Upcoming':
        $term = get_term_by('name',$old_name, $taxonomy);
        // fetch all posts in 'Upcoming' category:
        $objects = get_objects_in_term($term->term_id,$taxonomy);
        // the $objects array now contains the post IDs of all upcoming events
    
        // now, let's loop through them to manipulate:
        foreach($objects as $object) {
             // get start date:
             $key = 'start'; // the name of the custom field
             $start = get_post_meta($object, $key, true); // start date
             $todays_date = date('Y-m-d'); // get current date
             // Assuming, your dates in the custom fields are formatted YYYY-MM-DD:
             if ($start < $todays_date) {
                 // change category:
                 $new_name = 'Current';
                 wp_set_post_terms( $object, $new_name, $taxonomy, false );
             }
        }
    
    ?>
    

    几点说明:

    • 显然,必须更改上述内容才能从“当前”更改为“过去”。
    • 它也可以很容易地调整为包括时间。
    • cronjobs 应在午夜后不久启动
    • $first_time 必须是 UNIX timestamp
    • 查看wordpress function reference 了解有关上述 wp 函数的更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 2021-01-20
      • 2020-10-01
      • 2011-07-28
      相关资源
      最近更新 更多