How to use TOC with ACF?

时间:2015-08-21 作者:nisr

我正在使用ACF advanced custom fields插件为我的CPT创建自定义字段,我将我的CPT的内容编辑器替换为来自ACF的自定义Wysiwyg编辑器字段(字段名称:full\\u text),我在其中输入CPT的主文本(出于特定原因,我确实需要它)。但是,当我尝试使用toc+从自定义字段编辑器中的文本生成一个内容表时,它没有显示任何内容。实际上,他们在插件页面上写道:

Custom post types are supported, however, auto insertion works only when `the_content()` has been used by the custom post type. Each post type will appear in the options panel, so enable the ones you want.
在插件toc中。php文件我注意到:

        public function extract_headings( &$find, &$replace, $content = \'\' )
    {
        $matches = array();
        $anchor = \'\';
        $items = false; 
        // reset the internal collision collection as the_content may have been triggered elsewhere
        // eg by themes or other plugins that need to read in content such as metadata fields in
        // the head html tag, or to provide descriptions to twitter/facebook
        $this->collision_collector = array();
        if ( is_array($find) && is_array($replace) && $content ) {
            // get all headings
            // the html spec allows for a maximum of 6 heading depths
            if ( preg_match_all(\'/(<h([1-6]{1})[^>]*>).*<\\/h\\2>/msuU\', $content, $matches, PREG_SET_ORDER) ) {
                // remove undesired headings (if any) as defined by heading_levels
                if ( count($this->options[\'heading_levels\']) != 6 ) {
                    $new_matches = array();
                    for ($i = 0; $i < count($matches); $i++) {
                        if ( in_array($matches[$i][2], $this->options[\'heading_levels\']) )
                            $new_matches[] = $matches[$i];
                    }
                    $matches = $new_matches;
                }
                // remove specific headings if provided via the \'exclude\' property
                if ( $this->options[\'exclude\'] ) {
                    $excluded_headings = explode(\'|\', $this->options[\'exclude\']);
                    if ( count($excluded_headings) > 0 ) {
                        for ($j = 0; $j < count($excluded_headings); $j++) {
                            // escape some regular expression characters
                            // others: http://www.php.net/manual/en/regexp.reference.meta.php
                            $excluded_headings[$j] = str_replace(
                                array(\'*\'), 
                                array(\'.*\'), 
                                trim($excluded_headings[$j])
                            );
                        }
                        $new_matches = array();
                        for ($i = 0; $i < count($matches); $i++) {
                            $found = false;
                            for ($j = 0; $j < count($excluded_headings); $j++) {
                                if ( @preg_match(\'/^\' . $excluded_headings[$j] . \'$/imU\', strip_tags($matches[$i][0])) ) {
                                    $found = true;
                                    break;
                                }
                            }
                            if (!$found) $new_matches[] = $matches[$i];
                        }
                        if ( count($matches) != count($new_matches) )
                            $matches = $new_matches;
                    }
                }
                // remove empty headings
                $new_matches = array();
                for ($i = 0; $i < count($matches); $i++) {
                    if ( trim( strip_tags($matches[$i][0]) ) != false )
                        $new_matches[] = $matches[$i];
                }
                if ( count($matches) != count($new_matches) )
                    $matches = $new_matches;
                // check minimum number of headings
                if ( count($matches) >= $this->options[\'start\'] ) {
                    for ($i = 0; $i < count($matches); $i++) {
                        // get anchor and add to find and replace arrays
                        $anchor = $this->url_anchor_target( $matches[$i][0] );
                        $find[] = $matches[$i][0];
                        $replace[] = str_replace(
                            array(
                                $matches[$i][1],                // start of heading
                                \'</h\' . $matches[$i][2] . \'>\'   // end of heading
                            ),
                            array(
                                $matches[$i][1] . \'<span id="\' . $anchor . \'">\',
                                \'</span></h\' . $matches[$i][2] . \'>\'
                            ),
                            $matches[$i][0]
                        );
                        // assemble flat list
                        if ( !$this->options[\'show_heirarchy\'] ) {
                            $items .= \'<li><a href="#\' . $anchor . \'">\';
                            if ( $this->options[\'ordered_list\'] ) $items .= count($replace) . \' \';
                            $items .= strip_tags($matches[$i][0]) . \'</a></li>\';
                        }
                    }
                    // build a hierarchical toc?
                    // we could have tested for $items but that var can be quite large in some cases
                    if ( $this->options[\'show_heirarchy\'] ) $items = $this->build_hierarchy( $matches );
                }
            }
        }
        return $items;
    }
您可以注意到该函数与$content. 但是有没有办法让它与使用ACF创建的自定义字段一起工作?或者,你有什么建议来解决这个问题吗?

1 个回复
最合适的回答,由SO网友:JHoffmann 整理而成

您可以使用acf/save_post 将自定义“全文”字段中的内容复制到正常的帖子内容字段,以便toc+和其他插件可以自动访问。

add_action( \'acf/save_post\', \'wpse199256\', 20 );

function wpse199256( $post_ID ) {
    global $wpdb;
    $post = get_post( $post_ID );
    $wpdb->update( $wpdb->posts, array( \'post_content\' => get_field( \'full_text\', $post_ID ), array( \'ID\' => $post_id ) );
}
根据您使用CPT的方式,如果输出内容字段(直到现在还是空的),则必须调整一些模板等。

结束

相关推荐

问题:wp_INSERT_POST和POST_CONTENT字段错误无法更新数据库中的POST

我在创建要在WordPress站点中使用的导入/缓存脚本时遇到问题。此提要来自单个文本文件,然后将其解析并作为数组返回。当运行提要中的每个项目时,我使用wp\\u insert\\u post函数添加数据(见下文):$post = array( \'post_title\' => $property[\'DISPLAY_ADDRESS\'], \'post_name\' => sanitize_title($property[\'D