如何将一种自定义帖子类型的所有帖子显示到另一种自定义帖子框中?

利亚姆·理查森(Liam Richardson)

所以,我正在尝试做一些稍微复杂的事情,这比我现在使用WordPress时的技术水平要高一些!

我有一个名为“ Products的自定义帖子类型我希望能够将产品帖子类型中的每个“帖子”拉入网站上其他所有帖子类型(标准和自定义)中的选择元框。

然后,我希望能够使用该选择将信息拉到页面上,以便将其显示给用户。

我什至不知道我怎么会开始这样做。我可以制作meta框和自定义帖子类型,我只是不知道如何将它们粘合在一起!

Naresh Kumar P

我已经创建了相同的代码,这是从其中拉出帖子post-type并将其显示在帖子类型的选择标记中的代码。

您必须创建一个meta框,此后,您需要从另一个帖子类型中拉出帖子,以便它将填充如下创建的meta框的select标记中的帖子名称。

我已经为创建了它destination_category您可以post_type按顺序更改和显示位置,以使代码获得成功。

<?php
//Creation of Meta Box for post type "destination_category" (Start)
add_action( 'admin_init', 'my_destination_category' );
//destination_sub_category_admin -  is the required HTML id attribute
//Select Destination Sub Category -  is the text visible in the heading of the meta box section
//display_destination_subcategory_meta_box - is the callback which renders the contents of the meta box
//destination_category - is the name of the custom post type where the meta box will be displayed
// normal - defines the part of the page where the edit screen section should be shown
// high - defines the priority within the context where the boxes should show

function my_destination_category() {
    add_meta_box( 'destination_sub_category_admin','Select Destination Sub Category','display_destination_subcategory_meta_box',     'destination_category', 'normal', 'high');
    function display_destination_subcategory_meta_box( $select_category ) {
    // Retrieve Current Selected Category ID based on the Category Created   
    global $wpdb;
    $selectcat="SELECT * FROM ".$wpdb->prefix."posts WHERE `post_type`='destination_category' AND `post_status`='publish' ORDER BY `ID` DESC";
    $resultant = $wpdb->get_results($selectcat);
    $rescount=count($resultant);
    $category_selected_id = intval( get_post_meta( $select_category->ID, 'destination_category_id', true ) );
    ?>
    <link rel="stylesheet" type="text/css" href="<?php echo plugins_url('css/metabox.css',__FILE__ ) ?>" />
    <table>
        <tr>
            <td style="width: 150px">Select Category</td>
            <td>
                <select style="width: 100px" name="category_selection" id="meta_box_category" style="float:left; width:50%; !important">
                <?php               
                if($rescount==0)
                {?>
                <option value="null">No Posts have been created</option>
                <?php
                }
                else
                {                
                // Generate all items of drop-down list
                foreach($resultant as $singleresultant)
                {
                ?>
                    <option value="<?php echo $singleresultant->ID; ?>" <?php echo selected( $singleresultant->ID, $category_selected_id ); ?>>
                    <?php echo $singleresultant->post_title; ?>
                    </option>
                    <?php
                    }
                    }
                    ?>
                </select>
            </td>
        </tr>
    </table>
    <?php
}
// Registering a Save Post Function
add_action( 'save_post', 'destination_admin_sub_category', 10, 2 );
function destination_admin_sub_category( $select_category_id, $select_category ) {
    // Check post type for movie reviews
    if ( $select_category->post_type == 'destination_category' ) {
        // Store data in post meta table if present in post data
           if ( isset( $_POST['category_selection'] ) && $_POST['category_selection'] != '' ) {
            echo update_post_meta( $select_category_id, 'destination_category_id', $_POST['category_selection'] );
        }
    }
}
}
?>

我在这里提供了详细的代码说明,以便您非常轻松地开发代码。

跳,这样对您有很大的帮助,以解决您的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在另一种帖子类型中显示自定义帖子类型的缩略图?

如何使用自定义帖子类型仅显示一种分类法?

如何从两种自定义帖子类型中获取所有帖子?

根据类别在3个不同的主页位置显示一种自定义帖子类型

如何查询帖子类型的分类,然后包含另一种没有税的帖子类型

如何在Wordpress中显示最后三种自定义帖子类型

如何将一种泛型的结构复制到另一种泛型,用自定义值替换原始值?

通过使用一个类别显示所有帖子和自定义帖子类型

当自定义类型帖子与另一个自定义类型帖子有连接时,如何获取自定义类型帖子数据?

自定义帖子类型分类页面仍显示所有帖子

在分类存档页面上以自定义帖子类型显示所有帖子

自定义帖子类型中的“ __(”

自定义字段未显示在自定义帖子类型的帖子中

自定义帖子类型仅显示最近的帖子

WordPress的自定义帖子类型帖子不显示

WordPress:如何将帖子类型循环到自定义仪表板菜单?

将自定义结构转换为另一种模板类型

在元框中循环自定义帖子类型

5种自定义帖子类型以使用相同的自定义单个模板

为什么所有自定义帖子类型都显示在“最近帖子”小部件中?

如何显示自定义帖子类型的Pod数据

在Wordpress中显示自定义帖子类型的内容

编辑器中另一种文章类型的Wordpress自定义文章类型下拉列表

如何在wordpress中获取所有自定义帖子类型?

如何在页面或自定义帖子类型中复制一组自定义字段?

通过单击另一个页面模板中的帖子标题,可以在特定的自定义wordpress模板中打开自定义帖子类型。

列出所有自定义帖子类型的所有帖子标题,并包含组

使用自定义帖子类型作为另一个帖子类型 wordpress 的类别

您如何编写包含另一种类型的自定义对象数组的自定义对象,以在javascript中进行存储?