从其他帖子类型的内容创建博客文章

时间:2013-07-05 12:00:13

标签: wordpress wordpress-theming

我正在处理的Wordpress网站上有一个“新闻”部分(这是常规博客/帖子),它将用于公司必须撰写的任何新闻。然后我有一个自定义的促销活动类型,它有自己的页面。

我希望客户能够通过自定义帖子类型添加其促销内容,该类型会在促销页面上进行,但我希望此内容也可以“交叉发布”到博客/新闻中,而不会强制使用客户要写两次。

有办法做到这一点吗?感谢。

只是一个注释:我将促销作为自定义类型的原因,而不仅仅是让他们从博客中完成所有这些是因为我需要自定义字段,这对任何其他类型的博客都是不必要的。< / p>

2 个答案:

答案 0 :(得分:0)

两个选项:

1)使用Shortcode API

在您的交叉帖子中,您需要添加短代码[crosspost id="POST-ID"]。其中POST-ID对应于其他帖子(帖子类型)的数字ID。可以使用标题而不是ID,请参阅函数get_page_by_title

Create your own plugin为此。从Codex添加一个示例短代码,并使用函数get_post获取交叉发布的内容。

2)使用Advanced Custom Fields插件

有了它,添加自定义字段的元框是轻而易举的。它有一个Post Object字段,基本上是一个Cross Post功能。

答案 1 :(得分:0)

您可以通过向wp_insert_data()添加过滤器来实现更多功能。例如,在主题的functions.php文件中添加以下内容:

add_filter('wp_insert_post_data', 'post_to_other', 99, 2);

然后,只要您添加新帖子,该过滤器就会运行。在函数post_to_other()中,您可以查看提交的帖子类型。如果是促销,则插入第二个副本作为新闻项目。

function post_to_other($post_id, $post){

/** check $post to see what type it is, if it's a promotion */
if($post->post_type == 'promotion'){
$second_post = array(
            'post_type'=> 'post',
            'post_title'=> $post->post_title,
            'post_name' =>$post->post_name,
            'post_content'=> $post->post_content,
            'post_author'=> $post->post_author,
            'post_status'=> 'publish',
            'tax_input'=> array('taxonomy_name'=>array('news'))
            );
            wp_insert_post($second_post);
}

}

我跑出了门,所以我没有时间仔细检查确切的代码,但这是它的基本结构。 tax_input位是可选的,允许您根据需要指定类别。您可能需要稍微调整一下,但这是基础知识。

相关问题