在下拉菜单中显示帖子按定制帖子排序

时间:2013-05-29 作者:Luismin

在我的WordPress后端中,我有一些自定义帖子,在其中一个帖子中,我想在下拉字段中显示一些与相关但按帖子类型排序的帖子。我想展示这样的东西。

<select multiple="multiple" style="height:200px; width:300px">
<optgroup label="Productos">
<option>Producto 1</option>
<option>Producto 2</option>
<option>Producto 3</option>
<option>Producto 4</option>
<option>Producto 5</option>
<option>Producto 6</option>
</optgroup>
<optgroup label="Paso 1">
<option>Subpagina 1</option>
<option>Subpagina 2</option>
<option>Subpagina 3</option>
</optgroup>
</optgroup>
<optgroup label="Paso 2">
<option>Subpagina 1</option>
<option>Subpagina 2</option>
<option>Subpagina 3</option>
</optgroup>
</optgroup>
<optgroup label="Paso 3">
<option>Subpagina 1</option>
<option>Subpagina 2</option>
<option>Subpagina 3</option>
</optgroup>
</select>
下面是显示我想要但没有顺序的所有帖子的代码。我发现我必须使用自定义帖子类型的id,但我不知道如何:

{
// Add the Meta Box  

function add_custom_meta_box_related() {  
    add_meta_box(  
        \'custom_meta_box_related\', // $id  
        \'Related Information\', // $title   
        \'show_custom_meta_box_related\', // $callback  
        \'related\', // $page  
        \'normal\', // $context  
        \'high\'); // $priority  
}  
add_action(\'add_meta_boxes\', \'add_custom_meta_box_related\');

    // Field Array  
    $prefix_related = \'custom_\';  
    $custom_meta_fields_related = array(  

        array(  
            \'label\' => \'Related Items\',  
            \'desc\' => \'Select a related item(s)\',  
            \'id\'    =>  $prefix_materiales.\'post_id\',  
            \'type\' => \'post_list\',  
            \'post_type\' => array(\'products\',\'paso1\',\'paso2\',\'paso3\',\'paso4\',\'compra\'),  
)
    );  

    // The Callback  
function show_custom_meta_box_related() {  
global $custom_meta_fields_related, $post;  
// Use nonce for verification  
echo \'<input type="hidden" name="custom_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';  

    // Begin the field table and loop  
    echo \'<table class="form-table">\';  
    foreach ($custom_meta_fields_related as $field_related) {  
        // get value of this field if it exists for this post  
        $meta_related = get_post_meta($post->ID, $field_related[\'id\'], true);  
        // begin a table row with  
        echo \'<tr> 
                <th><label for="\'.$field_related[\'id\'].\'">\'.$field_related[\'label\'].\'</label></th> 
                <td>\';  
                switch($field_related[\'type\']) {  
                    // case items will go here 

                    // post_list  
                    case \'post_list\':  
                    $items = get_posts( array (  
                        \'post_type\' => $field_related[\'post_type\'],  
                        \'posts_per_page\' => -1  
                    ));  
                    echo \'<select multiple name="\'.$field_related[\'id\'].\'" id="\'.$field_related[\'id\'].\'"> 
                    <option value="">Select One or more</option>\'; // Select One  
                    foreach($items as $item) {  
                        echo \'<option value="\'.$item->ID.\'"\',$meta_related == $item->ID ? \' selected="selected"\' : \'\',\'> \'.$item->post_title.\'</option>\';  
                    } // end foreach  
                    echo \'</select><br /><span class="description">\'.$field_related[\'desc\'].\'</span>\';  
                    break;
                } //end switch  
        echo \'</td></tr>\';  
    } // end foreach  
    echo \'</table>\'; // end table  
}

// Save the Data  
function save_custom_meta_related($post_id) {  
    global $custom_meta_fields_related;  

    // verify nonce  
    if (!wp_verify_nonce($_POST[\'custom_meta_box_nonce\'], basename(__FILE__)))   
        return $post_id;  
    // check autosave  
    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)  
        return $post_id;  
    // check permissions  
    if (\'page\' == $_POST[\'post_type\']) {  
        if (!current_user_can(\'edit_page\', $post_id))  
            return $post_id;  
        } elseif (!current_user_can(\'edit_post\', $post_id)) {  
            return $post_id;  
    }  

    // loop through fields and save the data  
    foreach ($custom_meta_fields_related as $field_related) {  
        $old = get_post_meta($post_id, $field_related[\'id\'], true);  
        $new = $_POST[$field_related[\'id\']];  
        if ($new && $new != $old) {  
            update_post_meta($post_id, $field_related[\'id\'], $new);  
        } elseif (\'\' == $new && $old) {  
            delete_post_meta($post_id, $field_related[\'id\'], $old);  
        }  
    } // end foreach  
}  
add_action(\'save_post\', \'save_custom_meta_related\');


}

1 个回复
SO网友:Luismin

现在我可以订购de post\\u类型。有关于多次保存的帮助吗?

// The Callback  
函数show\\u custom\\u meta\\u box\\u related(){
全局$custom\\u meta\\u fields\\u related,$post;
//使用nonce进行验证
回显“”;

// Begin the field table and loop  
echo \'<table class="form-table">\';  
foreach ($custom_meta_fields_related as $field_related) {  
    // get value of this field if it exists for this post  
    $meta_related = get_post_meta($post->ID, $field_related[\'id\'], true);  
    // begin a table row with  
    echo \'<tr> 
            <th><label for="\'.$field_related[\'id\'].\'">\'.$field_related[\'label\'].\'</label></th> 
            <td>\';  
            switch($field_related[\'type\']) {  
                // case items will go here 

                // post_list  
                case \'post_list\': 
                echo \'<select multiple style="height:200px; width:300px" name="\'.$field_related[\'id\'].\'" id="\'.$field_related[\'id\'].\'">\';
                echo \'<option value=""></option>\'; // Select One
                foreach($field_related[\'post_type\'] as $tipo_post){
                $items = get_posts( array (  
                    \'post_type\' => $tipo_post,  
                    \'posts_per_page\' => -1  
                )); 

                foreach($items as $item) {  
                    echo \'<option value="\'.$item->ID.\'"\',$meta_related == $item->ID ? \' selected="selected"\' : \'\',\'> \'.$item->post_title. \'</option>\';  
                } // end foreach

                }
                echo \'</select><br /><span class="description">\'.$field_related[\'desc\'].\'</span>\';  
                break;
            } //end switch  
    echo \'</td></tr>\';  
} // end foreach  
echo \'</table>\'; // end table  
}

结束

相关推荐

将wp_Dropdown_Categories变为一个变量,该变量将一直存在,直到更改为止?

用户从wp\\u dropdown\\u categories中选择Categoriy1,如果用户在任何时候单击tag2,他们只会看到Categoriy1中带有tag2的帖子,而不是所有带有tag2的帖子。是否有方法将所选类别存储为变量,并使用该变量过滤所有内容,直到该变量被更改或删除?