Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取分类名称作为Wordpress页面标题_Wordpress_Taxonomy_Page Title - Fatal编程技术网

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

获取分类名称作为Wordpress页面标题,wordpress,taxonomy,page-title,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)

我正在尝试将所选分类法保存为页面标题的一部分。我对编码相当陌生(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 ); 
                 } 
             }

            }
            }

我已经更新了你的代码。检查下面

    $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 ); 

我明白了。代码如下:

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

谢谢@chiragswadia

遗憾的是,它不起作用。我还加了,;对于第4行($proname=$term;),仍然存在错误。知道怎么回事吗?@AnilAstrio在
$my\u post
数组中使用它之前,请检查
$post\u id
是否存在!另外,如果您可以粘贴整个代码段,那么调试将很容易。谢谢您的帮助。我刚刚更新了代码。非常感谢您为什么将
wp\u update\u post
代码块包装在
{}
块中。我没有看到任何其他条件以及。我还看到您在if块中放置了一个空{!