简体   繁体   English

Wordpress将常规帖子与自定义帖子类型帖子相关联

[英]Wordpress Relate regular posts with custom post type posts

I would like to associate custom post types posts which are products with the standard wordpress posts. 我想将定制产品类型的帖子与标准的wordpress帖子相关联。 Can anyone point me in the right direction how to accomplish this? 谁能指出正确的方向如何实现这一目标?

The reason being is i want to write blog articles and some of my blog articles may relate to a specific product. 原因是我想写博客文章,而我的某些博客文章可能与特定产品有关。 So on the products page i want to be able to have a link of related articles to that product. 因此,在产品页面上,我希望能够找到该产品的相关文章链接。

I would suggest to save an array or string of the IDs of related posts / related product as using a custom field in the backend of your Website. 我建议您使用网站后端的自定义字段来保存相关帖子/相关产品ID的数组或字符串。

On the frontend of your Website you can get the associated IDs from the Database and Make Links using the functions <?php echo get_the_title(ID); ?> 在您网站的前端,您可以使用<?php echo get_the_title(ID); ?>函数从数据库中获取关联的ID,并进行链接<?php echo get_the_title(ID); ?> <?php echo get_the_title(ID); ?> and <?php echo get_page_link(ID); ?> <?php echo get_the_title(ID); ?><?php echo get_page_link(ID); ?> <?php echo get_page_link(ID); ?> . <?php echo get_page_link(ID); ?>

Depending on how comfortable the backend must be to edit, you can add custom Meta Boxes to the backend, using the following code in functions.php. 根据后端编辑的舒适程度,您可以使用functions.php中的以下代码将自定义元框添加到后端。

add_action('admin_init', 'register_meta');
add_action('save_post', 'save_metadaten');


function register_meta(){
        add_meta_box("produkt_meta","Daten des Produkts","produkt_meta","produkt","normal","high");
}

function produkt_meta() {
    global $post;
    $custom = get_post_custom($post->ID);
    $preis = $custom["produkt_preis"][0]; ?>

    <h4>Produkt-Daten</h4>
    <p style="padding-bottom:4px;"><label style="width:200px; display:inline-block;">Preis:</label><input size="5" name="produkt_preis" value="<?php echo $preis; ?>" /> €</p>
    <?php
}

function save_metadaten(){
    global $post;

    // check if there are associated post IDs set somehow, and prepare these to save them in the database

    if($_POST["produkt_preis"]) {update_post_meta($post->ID, "produkt_preis", $_POST["produkt_preis"]);}
}

Replace "produkt_preis" with "associated_post_ids" or whatever. 用“ associated_post_ids”或其他替换“ produkt_preis”。

For maximum comfort you can add a JavaScript to the produkt_meta function which lists your posts so you just click it and the meta-field is automatically filled, but this is the part you should create on your own now ;-) 为了获得最大的舒适度,您可以在produkt_meta函数中添加一个JavaScript,该函数会列出您的帖子,因此您只需单击它即可自动填充meta字段,但这是您现在应该自己创建的部分;-)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM