根据其他文章类型的内容创建博客文章



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

我希望客户能够通过促销页面上的自定义帖子类型添加他的促销内容,但我希望这些内容也能"交叉发布"到博客/新闻中,而不会强迫客户写两次。

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

只需注意:我之所以将促销作为一种自定义类型,而不是让他们从博客中完成所有操作,是因为我需要自定义字段,这对于任何其他类型的博客文章来说都是不必要的。

两个选项:

1) 使用短代码API

在你的交叉帖子中,你会添加缩写[crosspost id="POST-ID"]。其中POST-ID对应于另一个帖子的数字ID(帖子类型)。可以使用标题而不是ID,请参阅函数get_page_by_title

为此创建您自己的插件。从Codex中添加一个示例快捷代码,并使用函数get_post来获取交叉帖子的内容。

2) 使用高级自定义字段插件

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

您可以通过在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位是可选的,如果你愿意,可以指定一个类别。你可能需要稍微调整一下,但这是最基本的。

最新更新