I created a search for my site using attachments. Each attachment is in category "Pictures", so my search form looks like:
<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>
My search results page search.php
returns those results through a get_posts
query. The problem I am having is with the pagination. I have been able to limit the number of posts on the page and even return the pagination, but when I click on the link for the pagination, the page does not display. I am thinking because these are attachments the pages are setup a little different, but I don't know what the url looks like. My current code looks like:
<?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 } ?>
The paging was integrated from the code I found on: https://erikeldridge.wordpress.com/2009/01/11/simple-php-paging/
I'm so close, probably just a tweak here or there, can anyone point me in the right direction?
Thanks,
Josh