我怎样才能在我的WordPress帖子中订购元博客呢?

时间:2013-07-12 作者:Ahmed Aly Mohammed

你好,我添加了一个客户文件,这是我的代码

add_action( \'add_meta_boxes\', \'cd_meta_box_add\' );
function cd_meta_box_add()
{
    add_meta_box( \'my-meta-box-id\', \'المعلومات الرئيسية لفلل مشروع\', \'cd_meta_box_cb\', \'post\', \'normal\', \'high\' );
}

function cd_meta_box_cb( $post )
{
    $values = get_post_custom( $post->ID , true);
    $text1 = get_post_meta( $post->ID, \'my_meta_box_text1\', true );
    $text2 = get_post_meta( $post->ID, \'my_meta_box_text2\', true );
    $text3 = get_post_meta( $post->ID, \'my_meta_box_text3\', true );
    $text4 = get_post_meta( $post->ID, \'my_meta_box_text4\', true );
    $text5 = get_post_meta( $post->ID, \'my_meta_box_text5\', true );
    $text6 = get_post_meta( $post->ID, \'my_meta_box_text6\', true );
    $textarea = get_post_meta( $post->ID, \'my_meta_box_textarea\', true );
    $text7 = get_post_meta( $post->ID, \'my_meta_box_text7\', true );
    $text8 = get_post_meta( $post->ID, \'my_meta_box_text8\', true );
    $text9 = get_post_meta( $post->ID, \'my_meta_box_text9\', true );
    $text10 = get_post_meta( $post->ID, \'my_meta_box_text10\', true );
    $text11 = get_post_meta( $post->ID, \'my_meta_box_text11\', true );
    $textarea2 = get_post_meta( $post->ID, \'my_meta_box_textarea2\', true );
    $text12 = get_post_meta( $post->ID, \'my_meta_box_text12\', true );
    $text13 = get_post_meta( $post->ID, \'my_meta_box_text13\', true );
    $text14 = get_post_meta( $post->ID, \'my_meta_box_text14\', true );
    $text15 = get_post_meta( $post->ID, \'my_meta_box_text15\', true );
    wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' );
    ?>
        <p>
        <label for="my_meta_box_text1">حالة المشروع</label>
        <input type="text" name="my_meta_box_text1" id="my_meta_box_text1" value="<?php echo $text1; ?>" />
    </p>
    <p>
        <label for="my_meta_box_text2">الموقع</label>
        <input type="text" name="my_meta_box_text2" id="my_meta_box_text2" value="<?php echo $text2; ?>" />
    </p>
        <p>
        <label for="my_meta_box_text3">مساحة الأرض</label>
        <input type="text" name="my_meta_box_text3" id="my_meta_box_text3" value="<?php echo $text3; ?>" />
    </p>
        <p>
        <label for="my_meta_box_text4">مساحة البناء</label>
        <input type="text" name="my_meta_box_text4" id="my_meta_box_text4" value="<?php echo $text4; ?>" />
    </p>
        <p>
        <label for="my_meta_box_text5">عدد الادوار</label>
        <input type="text" name="my_meta_box_text5" id="my_meta_box_text5" value="<?php echo $text5; ?>" />
    </p>
        <p>
        <label for="my_meta_box_text6">عدد الغرف</label>
        <input type="text" name="my_meta_box_text6" id="my_meta_box_text6" value="<?php echo $text6; ?>" />
    </p>

    <p>
        <label for="my_meta_box_textarea">المواصفات</label>
        <textarea name="my_meta_box_textarea" id="my_meta_box_textarea"><?php echo $textarea; ?></textarea>
    </p>
            <p>
        <label for="my_meta_box_text7">نوع الفيلا</label>
        <input type="text" name="my_meta_box_text7" id="my_meta_box_text7" value="<?php echo $text7; ?>" />
    </p>
        <p>
        <label for="my_meta_box_text8">التشطيب ونوعية التصميم</label>
        <input type="text" name="my_meta_box_text8" id="my_meta_box_text8" value="<?php echo $text8; ?>" />
    </p>
        <p>
        <label for="my_meta_box_text9">تأسيس المصاعد ويشمل</label>
        <input type="text" name="my_meta_box_text9" id="my_meta_box_text9" value="<?php echo $text9; ?>" />
    </p>
        <p>
        <label for="my_meta_box_text10">تأسيس تمديدات التكييف ويشمل</label>
        <input type="text" name="my_meta_box_text10" id="my_meta_box_text10" value="<?php echo $text10; ?>" />
    </p>
        <p>
        <label for="my_meta_box_text11">المسبح</label>
        <input type="text" name="my_meta_box_text11" id="my_meta_box_text11" value="<?php echo $text11; ?>" />
    </p>

        <p>
        <label for="my_meta_box_textarea2">الضمانات</label>
        <textarea name="my_meta_box_textarea2" id="my_meta_box_textarea2"><?php echo $textarea2; ?></textarea>
    </p>
            <p>
        <label for="my_meta_box_text12">ضمان المبنى - سنوات</label>
        <input type="text" name="my_meta_box_text12" id="my_meta_box_text12" value="<?php echo $text12; ?>" />
    </p>

        <p>
        <label for="my_meta_box_text13">ضمان تمديدات الكهرباء - سنوات</label>
        <input type="text" name="my_meta_box_text13" id="my_meta_box_text13" value="<?php echo $text13; ?>" />
    </p>

        <p>
        <label for="my_meta_box_text14">ضمان تمديدات السباكة - سنوات</label>
        <input type="text" name="my_meta_box_text14" id="my_meta_box_text14" value="<?php echo $text14; ?>" />
    </p>

        <p>
        <label for="my_meta_box_text15">ضمان المسابح - سنوات</label>
        <input type="text" name="my_meta_box_text15" id="my_meta_box_text15" value="<?php echo $text15; ?>" />
    </p>



    <?php   
}



add_action( \'save_post\', \'cd_meta_box_save\' );
function cd_meta_box_save( $post_id )
{
    // Bail if we\'re doing an auto save
    if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn\'t there, or we can\'t verify it, bail
    if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'my_meta_box_nonce\' ) ) return;

    // if our current user can\'t edit this post, bail
    if( !current_user_can( \'edit_post\' ) ) return;

    // now we can actually save the data
    $allowed = array( 
        \'a\' => array( // on allow a tags
            \'href\' => array() // and those anchords can only have href attribute

        )
    );

    // Probably a good idea to make sure your data is set
            if( isset( $_POST[\'my_meta_box_text1\'] ) )
        update_post_meta( $post_id, \'<font color="#900">حالة المشروع</font>\', wp_kses( $_POST[\'my_meta_box_text1\'], $allowed ) );
    if( isset( $_POST[\'my_meta_box_text2\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/skins/1-default/Location-icon2.png" width="48" height="48" />الموقع</font>\', wp_kses( $_POST[\'my_meta_box_text2\'], $allowed ) );

            if( isset( $_POST[\'my_meta_box_text3\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/skins/1-default/Location-icon.png" width="16" height="48" />مساحة الأرض</font>\', wp_kses( $_POST[\'my_meta_box_text3\'], $allowed ) );
            if( isset( $_POST[\'my_meta_box_text4\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/skins/1-default/city_area.png" width="48" height="48" />مساحة البناء</font>\', wp_kses( $_POST[\'my_meta_box_text4\'], $allowed ) );
            if( isset( $_POST[\'my_meta_box_text5\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/skins/1-default/New-room-icon.png" width="48" height="48" />عدد الغرف</font>\', wp_kses( $_POST[\'my_meta_box_text5\'], $allowed ) );
            if( isset( $_POST[\'my_meta_box_text6\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/skins/1-default/office-building-icon.png" width="48" height="48" />عدد الأدوار</font>\', wp_kses( $_POST[\'my_meta_box_text6\'], $allowed ) );

    if( isset( $_POST[\'my_meta_box_textarea\'] ) )
        update_post_meta( $post_id, \'<img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/skins/1-default/mowasafat-icon.png" /><br />\', esc_attr( $_POST[\'my_meta_box_textarea\'] ) );
                    if( isset( $_POST[\'my_meta_box_text7\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/images/grid-small-dot-icon.png" width="16" height="16" />نوع الفيلا</font>\', wp_kses( $_POST[\'my_meta_box_text7\'], $allowed ) );
                    if( isset( $_POST[\'my_meta_box_text8\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/images/grid-small-dot-icon.png" width="16" height="16" />التشطيب ونوعية التصميم</font>\', wp_kses( $_POST[\'my_meta_box_text8\'], $allowed ) );
        if( isset( $_POST[\'my_meta_box_text9\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/images/grid-small-dot-icon.png" width="16" height="16" />تأسيس المصاعد ويشمل</font>\', wp_kses( $_POST[\'my_meta_box_text9\'], $allowed ) );
        if( isset( $_POST[\'my_meta_box_text10\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/images/grid-small-dot-icon.png" width="16" height="16" />تأسيس تمديدات التكييف ويشمل</font>\', wp_kses( $_POST[\'my_meta_box_text10\'], $allowed ) );
        if( isset( $_POST[\'my_meta_box_text11\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/images/grid-small-dot-icon.png" width="16" height="16" />المسابح</font>\', wp_kses( $_POST[\'my_meta_box_text11\'], $allowed ) );
            if( isset( $_POST[\'my_meta_box_textarea2\'] ) )
        update_post_meta( $post_id, \'<img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/skins/1-default/damanat-icon.png"/><br />\', esc_attr( $_POST[\'my_meta_box_textarea2\'] ) );
        if( isset( $_POST[\'my_meta_box_text12\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/images/grid-small-dot-icon.png" width="16" height="16" />ضمان المبنى - سنوات</font>\', wp_kses( $_POST[\'my_meta_box_text12\'], $allowed ) );
        if( isset( $_POST[\'my_meta_box_text13\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/images/grid-small-dot-icon.png" width="16" height="16" />ضمان تمديدات الكهرباء - سنوات</font>\', wp_kses( $_POST[\'my_meta_box_text13\'], $allowed ) );
        if( isset( $_POST[\'my_meta_box_text14\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/images/grid-small-dot-icon.png" width="16" height="16" />ضمان تمديدات السباكة - سنوات</font>\', wp_kses( $_POST[\'my_meta_box_text14\'], $allowed ) );
        if( isset( $_POST[\'my_meta_box_text15\'] ) )
        update_post_meta( $post_id, \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/images/grid-small-dot-icon.png" width="16" height="16" />ضمان المسابح - سنوات</font>\', wp_kses( $_POST[\'my_meta_box_text15\'], $allowed ) );

}
我有两个问题

第一个无法编辑,因为它在保存后变为空值

第二个问题是,它通过排序代码的方式来处理

帖子里的秩序变得很糟糕

第一个元变为第二个元,第五个元变为10个元

订单太差了

很抱歉我的英语不好,但我真的需要帮助,请:(

我快发疯了,我不知道我该如何修复帖子中的元顺序

2 个回复
SO网友:s_ha_dum

您写道:

我确实在我的帖子的代码中按照这个顺序写了meta,我希望在帖子中按照同样的顺序写

我认为这意味着您希望前端的输出顺序与您在此处建立的顺序相匹配:

$values = get_post_custom( $post->ID , true);
$text1 = get_post_meta( $post->ID, \'my_meta_box_text1\', true );
$text2 = get_post_meta( $post->ID, \'my_meta_box_text2\', true );
$text3 = get_post_meta( $post->ID, \'my_meta_box_text3\', true );
$text4 = get_post_meta( $post->ID, \'my_meta_box_text4\', true );
$text5 = get_post_meta( $post->ID, \'my_meta_box_text5\', true );
或此处:

<p>
    <label for="my_meta_box_text1">حالة المشروع</label>
    <input type="text" name="my_meta_box_text1" id="my_meta_box_text1" value="<?php echo $text1; ?>" />
</p>
<p>
    <label for="my_meta_box_text2">الموقع</label>
    <input type="text" name="my_meta_box_text2" id="my_meta_box_text2" value="<?php echo $text2; ?>" />
</p>
某些东西进入数据库的顺序不是它出来的顺序,因为数据库中的项没有内在的顺序。你必须按照你需要的顺序来要求这些物品。你必须这样做:

$values = get_post_custom( $post->ID , true);
$text1 = get_post_meta( $post->ID, \'my_meta_box_text1\', true );
$text2 = get_post_meta( $post->ID, \'my_meta_box_text2\', true );
$text3 = get_post_meta( $post->ID, \'my_meta_box_text3\', true );
$text4 = get_post_meta( $post->ID, \'my_meta_box_text4\', true );
$text5 = get_post_meta( $post->ID, \'my_meta_box_text5\', true );
$text6 = get_post_meta( $post->ID, \'my_meta_box_text6\', true );
重新打印,并按您想要的顺序打印。

另一种选择是使用get_post_custom:

$mbt = get_post_custom();
echo $mbt[\'my_meta_box_text1\'];
echo $mbt[\'my_meta_box_text2\'];
echo $mbt[\'my_meta_box_text3\'];
经过进一步的调查,我开始怀疑这个问题是否不在save函数中。您的save函数看起来确实很奇怪。update_post_meta 应如下所示:

update_post_meta(
  $post_id, 
  $meta_key, 
  $meta_value, 
  $prev_value
);
取而代之的是:

update_post_meta( 
  $post_id, 
  \'<font color="#900"><img src="http://milleniumhouses.com/manazelelalfya/wp-content/themes/manazelelalfya/skins/1-default/Location-icon2.png" width="48" height="48" />الموقع</font>\', 
  wp_kses( $_POST[\'my_meta_box_text2\'], 
    $allowed 
  ) 
);
第二个参数应该是meta_key-- 用于尝试检索值的同一个键--get_post_meta( $post->ID, \'my_meta_box_text2\', true );. 相反,您有一个包含标记的长字符串。老实说,我甚至不知道您想用它做什么,但它不会与您的其余代码一起工作。钥匙不匹配。另外,那根绳子truncated at 255 characters. 这不是保存数据的方法。例如,它应该是:

update_post_meta( 
  $post_id, 
  \'my_meta_box_text2\', 
  wp_kses( $_POST[\'my_meta_box_text2\'], 
    $allowed 
  ) 
);

SO网友:Ahmed Aly Mohammed
                        <?php
$ck = get_post_custom_keys($post_id); //Array
    foreach ($ck as $k) {
         if (substr ($k, 0, 1) == \'_\')
         {   // skip keys starting with \'_\'
             continue;
         }
         $cv = get_post_custom_values($k, $post_id );  //Array
            foreach ($cv as $c) {
                if (empty ($c))
                {   // skip empty value
                    continue;
                }

                echo ($k . \': \' . $c . \'<br/>\');
            }
    }

?>
结束

相关推荐

在earch form.php中使用不带“%s”字段的earch.php

很抱歉,如果这是一个简单/愚蠢的问题,但我仍在掌握WordPress搜索的功能。我想用只查询特定自定义帖子类型及其元字段的自定义搜索来完全替换模板中的标准搜索。我有一个搜索表单,可以执行此操作并进行搜索。返回正确数据的php。However, 搜索将不起作用,除非我包含一个名为“s”的输入字段,并且它不是空的。我想WP需要这个来访问搜索。php?我不需要搜索字段,因为我的搜索是基于自定义分类法和元字段的。那么,如果没有此字段,我如何仍然使用搜索?这可能吗?