获取分类名称作为Wordpress页面标题

时间:2014-08-21 05:02:57

标签: wordpress taxonomy page-title

我正在尝试将选定的分类法保存为页面标题的一部分。我是一个相当新的编码(1个月前)所以基本上,我的头现在即将爆炸。希望有人可以帮助我。谢谢你的帮助。

            // CRED SAVE DATA HOOK
            add_action('cred_save_data', 'my_save_data_action',10,2);
            function my_save_data_action($post_id, $form_data)
            {
                // if a specific form
                if ($form_data['id']==9) 
                {
                    {
                    if (isset($_POST["property-new-name"])) 
                    {
                        $terms = $_POST['property-new-name'];
                        $taxonomy = 'property-name';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["route"])) 
                    {
                        $terms = $_POST['route'];
                        $taxonomy = 'street';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["township"])) 
                    {
                        $terms = $_POST['township'];
                        $taxonomy = 'township';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["locality"])) 
                    {
                        $terms = $_POST['locality'];
                        $taxonomy = 'city';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["postal_code"])) 
                    {
                        $terms = $_POST['postal_code'];
                        $taxonomy = 'postal-code';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["administrative_area_level_1"])) 
                    {
                        $terms = $_POST['administrative_area_level_1'];
                        $taxonomy = 'state';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["country"])) 
                    {
                        $terms = $_POST['country'];
                        $taxonomy = 'country';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["country_short"])) 
                    {
                        $terms = $_POST['country_short'];
                        $taxonomy = 'country-code';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }


                    {

                        $taxonomy = 'property-name';
                        $term_list = wp_get_post_terms( $post->ID, $taxonomy, array("fields" => "names"));
                        $term = $term_list[0]; // getting first term, if there are multiple terms
                        $proname = $term;

                        $contract = get_post_meta( get_the_ID(), "wpcf-contract", true );
                        $title = $proname ." for " . $contract . " " . $_POST["township"]; 
                        // Update the post into the database
                        $my_post = array(
                            'ID'           => $post_id,
                            'post_title' => $title
                        );
                        $my_post['post_status'] = $_REQUEST['post_status'];
                        wp_update_post( $my_post ); 
                 } 
             }

            }
            }

2 个答案:

答案 0 :(得分:0)

我已更新您的代码。请查看以下

    $taxonomy = 'pro-name';
    $term_list = wp_get_post_terms( $post->ID, $taxonomy, array("fields" => "names"));
    $term = $term_list[0]; // getting first term, if there are multiple terms
    $proname = $term

    $contract = get_post_meta( get_the_ID(), "wpcf-contract", true );

    $title = $proname ." for " . $contract . " " . $_POST["town"]; 
    // Update the post into the database
    $my_post = array(
        'ID'           => $post_id,
        'post_title' => $title
    );
    wp_update_post( $my_post ); 

答案 1 :(得分:0)

我明白了。这是代码:

        $list = wp_get_post_terms( get_the_ID(), 'property-name', array("fields" => "names"));
        $proname = $list[0];

谢谢@chiragswadia