【问题标题】:How to add post into a category programmatically如何以编程方式将帖子添加到类别中
【发布时间】:2014-06-19 12:57:58
【问题描述】:

我是 Wordpress 和 php 的新手,很抱歉我的基本问题。 我想以编程方式在一个类别中添加一些帖子。 例如,每天我都想选择一些帖子并将它们插入到带有 php 代码的类别中,然后在第二天替换一些其他帖子。 可能吗?如何? 提前致谢。

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    你可以使用wp_insert_posthttps://codex.wordpress.org/Function_Reference/wp_insert_post

    查看 Codex,这里有很多例子:

    // Create post object
    $my_post = array(
      'post_title'    => 'My post',
      'post_content'  => 'This is my post.',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_category' => array(8,39)
    );
    
    // Insert the post into the database
    wp_insert_post( $my_post );
    

    要更新帖子,请参阅 wp_update_post https://codex.wordpress.org/Function_Reference/wp_update_post

    【讨论】:

    • 我不想将新帖子插入数据库。我只想以编程方式将数据库中存在的帖子添加到类别中。
    • 阅读?最后两行给你答案!
    【解决方案2】:

    使用wp_set_post_categories

    wp_set_post_categories( $post_id, array( 1, 2 ) );
    

    【讨论】:

    • 如果我为我的帖子设置了新类别,我可以将其返回到以前的类别吗?例如我想,当用户访问我的网站时,将一些帖子放在一个类别中,当用户离开我的网站时,帖子返回到以前的类别。
    • 我不确定你想要达到什么目的?
    • 因为我正在使用Post Rating 插件为帖子评分,而且我的主题应该获得一个在滑块上显示的类别。我想在滑块上显示大多数评分帖子。我该怎么办? :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    相关资源
    最近更新 更多