我使用附件创建了对我的网站的搜索。每个附件都属于“图片”类别,因此我的搜索表单如下所示:
<form method="get" id="searchform" action="<?php bloginfo(\'url\'); ?>">
<input type="text" name="s" id="s" value="Search..." onfocus="if (this.value==\'Search...\') this.value=\'\';" onblur="if (this.value==\'\') this.value=\'Search...\';" />
<?php
$parent = get_cat_ID("Pictures");
?>
<input type="hidden" name="cat" value="<?php echo $parent; ?>" />
<input type="submit" id="searchsubmit" value="" />
</form>
我的搜索结果页面search.php
通过get_posts
查询我遇到的问题是分页。我可以限制页面上的帖子数量,甚至返回分页,但当我单击分页链接时,页面不会显示。我在想,因为这些是附件,页面的设置有点不同,但我不知道url是什么样子。我当前的代码如下所示:<?php
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => \'-1\',
\'category_name\' => get_the_title()
);
$images = get_posts($args);
if (!empty($images)) {
?>
<?php
$limit = 5;
$total = count($images);
$pages = ceil($total / $limit);
$result = ceil($total / $limit);
$current = isset($_GET[\'paged\']) ? $_GET[\'paged\'] : 1;
$next = $current < $pages ? $current + 1 : null;
$previous = $current > 1 ? $current - 1 : null;
$offset = ($current - 1) * $limit;
$images = array_slice($images, $offset, $limit)
?>
<h4><span>Search</span> Results</h4>
<ul class="category">
<?php
foreach ($images as $image) {
$title = $image->post_title;
$description = $image->post_content;
$attachment_link = get_attachment_link( $image->ID );
?>
<li>
<div class="col1">
<a href="<?php echo $attachment_link; ?>"><?php echo wp_get_attachment_image($image->ID, "medium"); ?></a>
</div>
<div class="col2">
<h5><?php echo $title; ?></h5>
<p><?php echo $description; ?></p>
</div>
</li>
<?php } ?>
</ul>
<?php echo "<p>(Page: ". $current . " of " . $result .")</p>"; ?>
<? if($previous): ?>
<a href="<?php bloginfo(\'url\'); ?>?paged<?= $previous ?>">Previous</a>
<? endif ?>
<? if($next) : ?>
<a href="<?php bloginfo(\'url\'); ?>?paged<?= $next ?>">Next</a>
<? endif ?>
<?php } ?>
分页是根据我在上找到的代码集成的:https://erikeldridge.wordpress.com/2009/01/11/simple-php-paging/我离得太近了,可能只是在这里或那里稍作调整,有人能给我指出正确的方向吗?
谢谢,乔希