如何将CPT保持在特定的位置?

时间:2016-06-02 作者:ReynierPM

我已经创建了一个CPT(esp-publicitarios) 使用Metabox 具有以下代码的插件:

add_filter(\'rwmb_meta_boxes\', \'elclarin_register_meta_boxes\');

function elclarin_register_meta_boxes($meta_boxes)
{
    // Better has an underscore as last sign
    $prefix = \'rw_\';

    // meta box - advertising related news
    $meta_boxes[] = array(
        \'id\' => \'advertising_related_news\',
        \'title\' => __(\'Crear espacio publicitario\'),
        \'post_types\' => array(\'esp-publicitarios\'),
        \'fields\' => array(
            array(
                \'name\' => __(\'Choose related news\'),
                \'id\' => "{$prefix}advertising_related_link",
                \'type\' => \'select_advanced\',
                \'options\' => $options,
                \'clone\' => false,
            ),
            array(
                \'name\' => __(\'Choose an image\'),
                \'id\' => "{$prefix}adversiting_image",
                \'type\' => \'image_advanced\',
                \'force_delete\' => true,
                \'max_file_uploads\' => 1,
            ),
            array(
                \'name\' => __(\'Choose a position\'),
                \'id\' => "{$prefix}adversiting_position",
                \'type\' => \'select\',
                \'options\' => array(
                    \'2\' => \'Position 3\',
                    \'5\' => \'Position 6\',
                    \'8\' => \'Position 9\',
                ),
            ),
            array(
                \'name\' => __(\'Position\'),
                \'id\' => "{$prefix}show_at_position",
                \'type\' => \'hidden\',
                \'std\' => 1,
            ),
        ),
        \'validation\' => array(
            \'rules\' => array(
                "{$prefix}advertising_related_link" => array(
                    \'required\' => true,
                ),
            ),
            \'messages\' => array(
                "{$prefix}advertising_related_link" => array(
                    \'required\' => __(\'You must choose a related news\', $prefix),
                ),
            ),
        ),
    );

    return $meta_boxes;
}
这很好用。我需要在给定的位置(如上所示的3、6或9)插入敌手,因此我执行了以下功能:

/**
 * Extracts from an array posts with positional metadata and re-inserts them at the proper
 * indices. See http://wordpress.stackexchange.com/questions/210493
 **/
function wpse_210493_apply_advertising_position(&$posts, $return = false)
{
    $ad_posts = array();
    $content_posts = array();
    $related_post = array();
    // Seperate $posts into "Ads" and "Content" arrays based on whether or not they have \'rw_adversiting_position\' meta-data
    foreach ($posts as $post) {
        $position = get_post_meta($post->ID, \'rw_adversiting_position\', true);
        $post_date = $post->post_date;
        $post_modified = $post->post_modified;
        if (get_post_meta($post->ID, \'rw_advertising_related_link\', true) !== 0) {
            $related_post[] = get_post_meta($post->ID, \'rw_advertising_related_link\', true);
        }
        if (!empty($position)) {
            if (isset($ad_posts[$position])) {
                if ($post_date > $ad_posts[$position]->post_date || $post_modified > $ad_posts[$position]->post_modified) {
                    $ad_posts[$position] = $post;
                }
            } else {
                $ad_posts[$position] = $post;
            }
        } else {
            $content_posts[] = $post;
        }
    }
    // Sort the ads from smallest position index to greatest such that re-insertion properly factors in all ads
    ksort($ad_posts);
    // Add the ads back into the content at their specified positions
    foreach ($ad_posts as $position => $ad) {
        array_splice($content_posts, $position, 0, array($ad));
    }
    foreach ($content_posts as $key => $post) {
        if (in_array($post->ID, $related_post)) {
            unset($content_posts[$key]);
        }
    }

    return $content_posts;
}
接下来,我将在头版中使用上述代码:

$normal_args = array(
    \'order\' => \'desc\',
    \'ignore_sticky_posts\' => 1,
    \'meta_query\' => array(
        array(
            \'key\' => \'rw_show_at_position\',
            \'value\' => \'1\',
            \'compare\' => \'=\',
        ),
    ),
    \'post__not_in\' => $prev_post_ids,
    \'post_status\' => \'publish\',
    \'posts_per_page\' => get_option(\'column_right\'),
    \'post_type\' => array(
        \'opinion\',
        \'especiales\',
        \'clasificados\',
        \'portadadeldia\',
        \'anunciantes\',
        \'post\',
        \'pages\',
        \'esp-publicitarios\',
    ),
);
$normal_query = new WP_Query($normal_args);
$i = 0;
// Espacios publicitarios hook
$filtered_posts = wpse_210493_apply_advertising_position($normal_query->posts);
if (count($filtered_posts)) {
    foreach ($filtered_posts as $post) {
       // ....
    }
}
这“行得通”,但我有一个问题,因为CPT与职位挂钩,所以每当我添加新职位时,它都会离开,我需要保持它的位置不变,换句话说,如果我将CPT设置在位置3上,并将一个职位链接到它,那么CPT和职位应该保持在位置3上,直到我离开那里,我可以从社区获得一些帮助吗?我被困在这一点上了。

1 个回复
SO网友:majick

如果索引$related_posts 按位置以及$ad_posts, 您可能可以在同一位置拼接/插入它们。。。例如:。

function wpse_210493_apply_advertising_position(&$posts, $return = false)
{
    $ad_posts = array();
    $content_posts = array();
    $related_post = array();
    // Seperate $posts into "Ads" and "Content" arrays based on whether or not they have \'rw_adversiting_position\' meta-data
    foreach ($posts as $post) {
        $position = get_post_meta($post->ID, \'rw_adversiting_position\', true);
        $post_date = $post->post_date;
        $post_modified = $post->post_modified;

        // add an index to related_posts inside the position check
        if (!empty($position)) {
            // move this inside position check
            $related_post = get_post_meta($post->ID, \'rw_advertising_related_link\', true);
            if (isset($ad_posts[$position])) {
                if ($post_date > $ad_posts[$position]->post_date || $post_modified > $ad_posts[$position]->post_modified) {
                    $ad_posts[$position] = $post;
                    // add position index to related post
                    if ($related_post) {$related_post[$position] = $related_post;}
                }
            } else {
                $ad_posts[$position] = $post;
                // add position index to related post
                if ($related_post) {$related_post[$position] = $related_post;}
            }
        } else {
            $content_posts[] = $post;
        }

    }

    // Sort the ads from smallest position index to greatest such that re-insertion properly factors in all ads
    ksort($ad_posts);
    // Add the ads back into the content at their specified positions
    foreach ($ad_posts as $position => $ad) {
        array_splice($content_posts, $position, 0, array($ad));
        // add the match related post in
        array_splice($content_posts, $position, 0, array($related_post[$position]));
    }

    // then there is no need for this now
    // foreach ($content_posts as $key => $post) {
    //     if (in_array($post->ID, $related_post)) {
    //        unset($content_posts[$key]);
    //    }
    // }

    return $content_posts;
}

相关推荐

当in_the_loop()为假时,何时以及为什么is_Single(‘my_cpt’)为真?

我正在使用模板系统的示例代码。此地址的页码:http://project.test/my_cpt/hello-post/.无法理解原因is_singular( \'my_cpt\' ) 是true 虽然in_the_loop() 是false.在页面模板中The Loop "E;“工程”:if ( have_posts() ) { while ( have_posts() ) { the_post(); ?>