【问题标题】:WordPress set default taxonomy term on publishWordPress 在发布时设置默认分类术语
【发布时间】:2015-01-13 22:01:09
【问题描述】:

如果您在不检查分类的情况下发布帖子,有没有办法控制它默认使用哪个术语?

我有一个称为帖子类型和术语的分类法:下载、链接和博客文章。如果发布的帖子没有经过检查,我需要将其默认为 blog-post。它默认为下载。

我试过用这个:

http://wordpress.mfields.org/2010/set-default-terms-for-your-custom-taxonomies-in-wordpress-3-0/

/**
 * Define default terms for custom taxonomies in WordPress 3.0.1
 *
 * @author    Michael Fields     http://wordpress.mfields.org/
 * @props     John P. Bloch      http://www.johnpbloch.com/
 * @props     Evan Mulins        http://circlecube.com/
 *
 * @since     2010-09-13
 * @alter     2013-01-31
 *
 * @license   GPLv2
 */
function mfields_set_default_object_terms( $post_id, $post ) {
    if ( 'publish' === $post->post_status ) {
        $defaults = array(
            'posttypes' => array( 'blog-post' )
            );
        $taxonomies = get_object_taxonomies( $post->post_type );
        foreach ( (array) $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $post_id, $taxonomy );
            if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
                wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
            }
        }
    }
}
add_action( 'save_post', 'mfields_set_default_object_terms', 100, 2 );

但它似乎不起作用。

【问题讨论】:

  • 这个问题似乎跑题了,因为它是关于 WordPress 的,属于 wordpress.stackexchange.com
  • 你记得使用'add_action'吗?
  • 忘记复制了。

标签: php wordpress taxonomy term


【解决方案1】:
function mfields_set_default_object_terms( $post_id, $post ) {
    if ( 'publish' === $post->post_status ) {
        $defaults = array(
            'posttypes' => array( 'blog-post' )

【讨论】:

  • 你的答案应该包含对你的代码的解释和它如何解决问题的描述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多