我的情况:我有一个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;
?>
遗憾的是,这给了我一个回退错误“哎呀,出了点问题”。
就是这样。我希望我解释得足够好。如果没有,我会尝试重新措辞,以便你能帮助我!
非常感谢你。非常感谢您的帮助;)
最合适的回答,由SO网友:cr0z3r 整理而成
可以。只是有点复杂,但这里是这样的:
<?php
/**
* WP Practica
* Home Page Template
*/
get_header(); ?>
<?php $options = get_option( \'practica_theme_settings\' ); ?>
<div id="main"> <!-- start div#main -->
<!-- Optional 3D style --><div class="dimWrap_content"></div>
<div id="content" > <!-- start div#content -->
<?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=-1&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 ( $options[\'nothumb_content\'] == true ) { ?>
<div id="page-content">
<?php the_content(); ?>
</div>
<?php } else {
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\'; } ?>">
<?php if ($custom_meta[\'pr_openpretty_home\'][0] != true) { ?>
<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>
<?php } else {
if ($custom_meta[\'pr_openpretty_video\'][0] == \'Your URL\' || $custom_meta[\'pr_openpretty_video\'][0] == \'\') { ?>
<a href="<?php $image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,’large’, true);
echo $image_url[0]; ?>" title="<?php the_title_attribute(); ?>" rel="prettyPhoto<?php
if(!empty($custom_meta[\'pr_openpretty_slideshow\'][0])) {
echo \'[\';
echo $custom_meta[\'pr_openpretty_slideshow\'][0];
echo \']\';
} ?>">
<?php
$atts = array(
\'class\' => "attachment-$size imageBlock",
\'alt\' => get_the_title(),
);
the_post_thumbnail(\'portfolioSmall\', $atts);
?>
</a>
<?php } else { ?>
<a class="videoBlock" href="<?php echo $custom_meta[\'pr_openpretty_video\'][0]; ?>?width=100%&height=100%" title="<?php the_title_attribute(); ?>" rel="prettyPhoto<?php
if(!empty($custom_meta[\'pr_openpretty_slideshow\'][0])) {
echo \'[\';
echo $custom_meta[\'pr_openpretty_slideshow\'][0];
echo \']\';
} ?>">
<?php
$atts = array(
\'class\' => "attachment-$size imageBlock",
\'alt\' => get_the_title(),
);
the_post_thumbnail(\'portfolioSmall\', $atts);
?>
</a>
<?php } ?>
<?php } ?>
</li>
<?php
$count++;
if($count == $page_items) { break; }
}
endwhile;
echo \'</ul> <!-- First row -->\';
else :
echo \'<h3>Oops, something went wrong.</h3>\';
endif;
}
?>
</div> <!-- end div#content -->
<?php get_footer(); ?>