我的情况:我有一个frontpage,它从24个缩略图中筛选出12个缩略图(每个缩略图代表一篇文章的特色图像),并显示它们。用户可以在frontpage中隐藏任何他想要的帖子。
假设用户选择隐藏拇指#4,这意味着我们现在有拇指1、2、3、5、6、7、8、9、10、11、12。也就是说,我们有11个缩略图。因此,我们错过了最后一个“缩略图空间”,它应该被即将发布的帖子填满;在本例中,拇指为#13。
简而言之,frontpage应该查询帖子,以便当一个或多个缩略图被隐藏时(导致一个或多个空格),它会通过推入即将出现的缩略图自动“重新填充”空格。
我的首页有以下查询:
<?php
global $wp_query;
// First row of images
if(!empty($options[\'home_thumbs\'])) {
$page_items = $options[\'home_thumbs\'];
} else {
$page_items = 18;
}
$portCat = get_category_id($options[\'portfoliocat\']);
// get the desired sort order of portfolio items
if($options[\'homepage_sort\'] == \'DESC\') { $order = \'DESC\'; } else { $order = \'ASC\'; }
$hideFromHome = get_post_meta($wp_query->post->ID, \'pr_hidehome\', true);
query_posts(\'posts_per_page=\' . $page_items . \'&orderby=title&order=\' . $order . \'&cat=\' . $portCat . \'&meta_key=\' . $hideFromHome .\'&meta_value=\' . true);
// arrays to detect first and last columns
$firsts = array(6,12,18,24,30,36);
$lasts = array(5,11,17,23,29,35);
if (have_posts()) :
$count = 0;
echo \'<ul class="imageRows rowOne">\';
while (have_posts()) : the_post();
$custom_meta = get_post_custom($post->ID);
if (has_post_thumbnail() && $custom_meta[\'pr_hidehome\'][0] != true) { ?>
<li class="<?php if (in_array($count, $firsts) ) { echo \' columnFirst\'; } if (in_array($count, $lasts) ) { echo \' columnLast\'; } ?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
$atts = array(
\'class\' => "attachment-$size imageBlock",
\'alt\' => get_the_title(),
);
the_post_thumbnail(\'portfolioSmall\', $atts);
?>
</a>
</li>
<?php
$count++;
}
endwhile;
echo \'</ul> <!-- First row -->\';
else :
echo \'<h3>Oops, something went wrong.</h3>\';
endif;
?>
pr\\u hidehome是一个选项,启用后会隐藏相应的帖子(但会留下需要用一个或多个即将出现的缩略图填充的空白)。你也可以看到我是如何尝试查询帖子的。然而on this article, 我了解到,通过自定义字段查询帖子的最佳方法是使用一个包含“meta\\u query”的数组。以下是我尝试的内容: <?php
global $wp_query;
// First row of images
if(!empty($options[\'home_thumbs\'])) {
$page_items = $options[\'home_thumbs\'];
} else {
$page_items = 18;
}
$portCat = get_category_id($options[\'portfoliocat\']);
// get the desired sort order of portfolio items
if($options[\'homepage_sort\'] == \'DESC\') { $order = \'DESC\'; } else { $order = \'ASC\'; }
$args = array(
\'posts_per_page\' => $page_items,
\'orderby\' => \'title\',
\'order\' => $order,
\'cat\' => $portCat,
\'meta_query\' => array(
array(
\'key\' => \'pr_hidehome\',
\'value\' => \'the_value_you_want\',
\'compare\' => \'LIKE\'
)
)
);
query_posts($args);
// arrays to detect first and last columns
$firsts = array(6,12,18,24,30,36);
$lasts = array(5,11,17,23,29,35);
if (have_posts()) :
$count = 0;
echo \'<ul class="imageRows rowOne">\';
while (have_posts()) : the_post();
$custom_meta = get_post_custom($post->ID);
if (has_post_thumbnail() && $custom_meta[\'pr_hidehome\'][0] != true) { ?>
<li class="<?php if (in_array($count, $firsts) ) { echo \' columnFirst\'; } if (in_array($count, $lasts) ) { echo \' columnLast\'; } ?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
$atts = array(
\'class\' => "attachment-$size imageBlock",
\'alt\' => get_the_title(),
);
the_post_thumbnail(\'portfolioSmall\', $atts);
?>
</a>
</li>
<?php
$count++;
}
endwhile;
echo \'</ul> <!-- First row -->\';
else :
echo \'<h3>Oops, something went wrong.</h3>\';
endif;
?>
遗憾的是,这给了我一个回退错误“哎呀,出了点问题”。就是这样。我希望我解释得足够好。如果没有,我会尝试重新措辞,以便你能帮助我!
非常感谢你。非常感谢您的帮助;)