我已经创建了一个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上,直到我离开那里,我可以从社区获得一些帮助吗?我被困在这一点上了。