具有多个子帖子的帖子的自定义帖子类型结构

时间:2013-12-10 作者:Andy

我正在为一家非洲语言翻译公司开发一个网站,目前正试图找到构建该网站的最佳方式

我有一个自定义的“语言”帖子类型,有阿拉伯语、索马里语、斯瓦希里语等帖子,但这些帖子中的每一个都需要“子”帖子,即历史、在哪里说、方言、字母和;书写系统

设置此项的最佳方式是什么?将有大约140种语言,因此我不认为为每种语言的3/4子页面设置单独的帖子是一种好方法,因为我们将看到超过500个帖子

我想知道我是否可以为3/4子页设置“基本”帖子,因为标题总是唯一的,只是内容会有所不同

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

我已经等待了很长一段时间了,我必须继续这个项目,所以我想我会自己回答

首先,我设置了自定义的post语言类型,然后连接到publish_language 以编程方式添加子帖子的操作如下:

function ta_insert_child_posts($post_id) {  
    if(($_POST[\'post_status\'] == \'publish\') && ($_POST[\'original_post_status\'] != \'publish\')) {
        $post = get_post($post_id);

        // Make sure it\'s a top level language being published
        if($post->post_parent == 0) {
            // Create our array of child post titles
            $child_posts = array(\'History\', \'Where is it spoken\', \'Also known as\', \'Dialects\', \'Alphabet & Writing System\');

            foreach($child_posts as $child_post_title) {
                // Insert each new post as a child of the new language
                wp_insert_post(array(
                    \'post_title\' => $child_post_title,
                    \'post_parent\' => $post_id,
                    \'post_type\' => \'language\',
                    \'post_status\' => $post->post_status 
                ));
            }
        }
    }
}
add_action(\'publish_language\', \'ta_insert_child_posts\');
接下来,我必须添加逻辑来删除/丢弃子帖子,因为他们的父帖子被挂接到before_delete_posttrash_language

function ta_delete_child_posts($post_id) {
    global $post_type;

    if($post_type != \'language\') return;

    $child_posts = get_posts(array(\'post_parent\' => $post_id, \'post_type\' => \'language\'));

    if(is_array($child_posts)) {
        foreach($child_posts as $child_post) {
            wp_delete_post($child_post->ID, true);
        }
    }
}
add_action(\'before_delete_post\', \'ta_delete_child_posts\');

function ta_trash_child_posts($post_id) {
    $child_posts = get_posts(array(\'post_parent\' => $post_id, \'post_type\' => \'language\'));

    if(is_array($child_posts)) {
        foreach($child_posts as $child_post) {
            wp_trash_post($child_post->ID);
        }
    }
}
add_action(\'trash_language\', \'ta_trash_child_posts\');
好了,我们现在有了与父语言同步发布和删除的子帖子。接下来,我必须确保在管理ui语言列表中只使用顶级语言,所以我连接到request 措施:

function ta_modify_request($request) {
    if(is_admin()) {
        $screen = get_current_screen();

        // We only want to retrieve top level language posts in the main request
        if($screen->post_type == \'language\') {
           $request[\'post_parent\'] = 0;
        }
    }

    return $request;
}
add_action(\'request\', \'ta_modify_request\');
最后,我必须通过挂接到admin_footer 它为每种语言添加了一个扩展/收缩链接,并对一个函数进行了ajax调用,该函数获取所选语言的子帖子,并以标准wordpress表格格式显示它们:

function ta_child_posts_scripts() {
    $screen = get_current_screen();

    if($screen->post_type == \'language\') {
?>
    <style type="text/css">
        #the-list tr .sorting-indicator {top:10px;position:relative;margin-top:0;cursor:pointer}
        #the-list tr .sorting-indicator.show:before {content:\'\'}
        #the-list tr:hover .sorting-indicator {display:inline-block}
    </style>
    <script type="text/javascript">
        jQuery(function($) {
            $(\'#the-list tr .row-title\').each(function() {
                $(this).after(\'<span class="sorting-indicator show" title="Show Child Posts"></span>\');
            });

            $(\'#the-list tr .sorting-indicator\').on(\'click\', function() {
                var tr = $(this).parents(\'tr\');

                if($(this).hasClass(\'show\')) {
                    var data = {
                        action: \'ta_child_posts\',
                        post_id: tr.attr(\'id\')
                    };

                    $.post(ajaxurl, data, function(response) {
                        $(response).hide().insertAfter(tr).fadeIn();
                    });

                    $(this).removeClass(\'show\').addClass(\'hide\');

                } else {
                    tr.nextUntil(\'.level-0\').fadeOut(function() { $(this).remove(); });

                    $(this).removeClass(\'hide\').addClass(\'show\');
                }
            });           

        });
    </script>
<?php
    }
}

add_action(\'admin_footer\', \'ta_child_posts_scripts\');
有了这个功能,我所要做的就是添加ajax回调函数,以获取基于所选语言的子帖子:

if(is_admin() && !class_exists(\'WP_List_Table\')){
    require_once( ABSPATH . \'wp-admin/includes/class-wp-list-table.php\' );
    require_once( ABSPATH . \'wp-admin/includes/class-wp-posts-list-table.php\' );
}

function ta_get_child_posts() {

    if(empty($_POST[\'post_id\'])) return;

    $post_id = explode(\'-\', $_POST[\'post_id\']);

    if(!isset($post_id[1])) return;

    $post_id = (int)$post_id[1];

    // Get child posts of the selected post
    $child_posts = get_posts(array(\'post_parent\' => $post_id, \'post_type\' => \'language\'));

    set_current_screen(\'language\');

    $ta_table = new WP_Posts_List_Table(array(\'screen\' => get_current_screen()));

    $ta_table->prepare_items();

    // Since WP_List_Table provides no way to return its data we print the output with display_rows but catch it in an output buffer
    ob_start();

    $ta_table->display_rows($child_posts, 1);
    $rows = ob_get_clean();

    // Return the rows to the ajax callback
    die(print($rows));
}

add_action(\'wp_ajax_ta_child_posts\', \'ta_get_child_posts\');
我希望这能帮助未来有类似问题的googler或其他浏览此网站的人

结束

相关推荐