treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Search vs. Homepage

  • I have the home page http://www.faberunashop.com posts set up so that when you click on each individual post you get redirected to another site. I just noticed that when you search or use the categories, the posts ignore my custom code and let you enter the post instead. How do I fix that?
  • How does your loop code look like? I can see you have the images wrapped in a anchor tag linking to the appropriate website. However, the link on your categories/searches lead to the post indeed. My guess is your loop code on your home page is different than the loop code on your categories page, so try checking that or post them here.
  • Bob - where would I find the loop code? I will check and post what I find.
  • In the index.php file
  • Actually I think you can find it in the loop-page.php file.
  • Here's the index.php:
    <?php get_header(); ?> 

    <ul class="mcol">
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <li class="article" id="post-<?php the_ID(); ?>">

    <?php
    if ( has_post_thumbnail() ) { ?>
    <?php
    $imgsrcparam = array(
    'alt' => trim(strip_tags( $post->post_excerpt )),
    'title' => trim(strip_tags( $post->post_title )),
    );
    $thumbID = get_the_post_thumbnail( $post->ID, 'background', $imgsrcparam ); ?>
    <div class="preview"><a href="<?php echo get_post_meta($post->ID, 'url', true); ?>"><?php echo "$thumbID"; ?></a></div>

    <?php } else {?>
    <div class="preview"><a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_url'); ?>/images/default-thumbnail.jpg" alt="<?php the_title(); ?>" /></a></div>
    <?php } ?>

    <div class="article-over">
    <?php print_post_title() ?>
    <?php the_excerpt(); ?>
    <div class="postmetadata">
    Posted: <?php the_time(__('F jS, Y', 'kubrick')) ?>&nbsp;&#721;&nbsp;
    <?php comments_popup_link(__('No Comments'), __('1 Comment'), __('% Comments'), '', __('Comments Closed') ); ?><br />
    <?php printf(__('Filled under: %s'), get_the_category_list(', ')); ?>
    </div>
    </div>
    </li> <?php ?>
    <?php endwhile; ?>
    <?php else : ?>

    <?php endif; ?>
    </ul>

    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <?php endwhile; ?>
    <?php else : ?>
    <h1 id="error"><?php _e("Sorry, but you are looking for something that isn&#8217;t here."); ?></h1>
    <?php endif; ?>

    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    <?php get_footer(); ?>
  • Here's the archive.php:
    <?php get_header(); ?> 
    <?php /* If this is a category archive */ if (is_category()) { ?>
    <h1><?php printf(__('Archive for the &#8216;%s&#8217; Category'), single_cat_title('', false)); ?></h1>
    <?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
    <h1"><?php printf(__('Posts Tagged &#8216;%s&#8217;'), single_tag_title('', false) ); ?></h1>
    <?php /* If this is a daily archive */ } elseif (is_day()) { ?>
    <h1><?php printf(_c('Archive for %s|Daily archive page'), get_the_time(__('F jS, Y'))); ?></h1>
    <?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
    <h1><?php printf(_c('Archive for %s|Monthly archive page'), get_the_time(__('F, Y'))); ?></h1>
    <?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
    <h1><?php printf(_c('Archive for %s|Yearly archive page'), get_the_time(__('Y'))); ?></h1>
    <?php /* If this is an author archive */ } elseif (is_author()) { ?>
    <h1><?php _e('Author Archive'); ?></h1>
    <?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
    <h1><?php _e('Blog Archives'); ?></h1>
    <?php } ?>

    <ul class="mcol">
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <li class="article" id="post-<?php the_ID(); ?>">

    <?php
    if ( has_post_thumbnail() ) { ?>
    <?php
    $imgsrcparam = array(
    'alt' => trim(strip_tags( $post->post_excerpt )),
    'title' => trim(strip_tags( $post->post_title )),
    );
    $thumbID = get_the_post_thumbnail( $post->ID, 'background', $imgsrcparam ); ?>
    <div class="preview"><a href="<?php the_permalink() ?>"><?php echo "$thumbID"; ?></a></div>


    <?php } else {?>
    <div class="preview"><a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_url'); ?>/images/default-thumbnail.jpg" alt="<?php the_title(); ?>" /></a></div>
    <?php } ?>

    <div class="article-over">
    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
    <div class="postmetadata">
    Posted: <?php the_time(__('F jS, Y', 'kubrick')) ?>&nbsp;&#721;&nbsp;
    <?php comments_popup_link(__('No Comments'), __('1 Comment'), __('% Comments'), '', __('Comments Closed') ); ?><br />
    <?php printf(__('Filled under: %s'), get_the_category_list(', ')); ?>
    </div>
    </div>
    </li> <?php ?>
    <?php endwhile; ?>
    <?php else : ?>


    <?php endif; ?>
    </ul>

    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <?php endwhile; ?>
    <?php else : ?>
    <h1 id="error"><?php _e("Sorry, but you are looking for something that isn&#8217;t here."); ?></h1>
    <?php endif; ?>


    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    <?php get_footer(); ?>
  • Phew, please put it between code tags :)
    Edit your post, select the code and press the 'Code' button (4th button from the left on this comment box).
  • And search.php:
    <?php get_header(); ?> 
    <h1>Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('&#8220;'); echo $key; _e('&#8221;'); _e(' &mdash; '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h1>


    <ul class="mcol">
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <li class="article" id="post-<?php the_ID(); ?>">

    <?php
    if ( has_post_thumbnail() ) { ?>
    <?php
    $imgsrcparam = array(
    'alt' => trim(strip_tags( $post->post_excerpt )),
    'title' => trim(strip_tags( $post->post_title )),
    );
    $thumbID = get_the_post_thumbnail( $post->ID, 'background', $imgsrcparam ); ?>
    <div class="preview"><a href="<?php the_permalink() ?>"><?php echo "$thumbID"; ?></a></div>

    <?php } else {?>
    <div class="preview"><a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_url'); ?>/images/default-thumbnail.jpg" alt="<?php the_title(); ?>" /></a></div>
    <?php } ?>

    <div class="article-over">
    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
    <div class="postmetadata">
    Posted: <?php the_time(__('F jS, Y', 'kubrick')) ?>&nbsp;&#721;&nbsp;
    <?php comments_popup_link(__('No Comments'), __('1 Comment'), __('% Comments'), '', __('Comments Closed') ); ?><br />
    <?php printf(__('Filled under: %s'), get_the_category_list(', ')); ?>
    </div>
    </div>
    </li> <?php ?>
    <?php endwhile; ?>
    <?php else : ?>


    <?php endif; ?>
    </ul>

    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <?php endwhile; ?>
    <?php else : ?>
    <h1 id="error"><?php _e("Sorry, but you are looking for something that isn&#8217;t here."); ?></h1>
    <?php endif; ?>

    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>

    <?php get_footer(); ?>

  • You forgot 404.php
  • Sorry about the code tag omission! I don't see the 404.php. Does it go by another name?
  • I was kidding. Did you significantly alter your search.php and what theme are you using?
  • LOL! Don't mess with a Monday morning brain. I am using the Imbalance theme from www.wpshower.com and I only changed the text in the search.php, that is all.
  • What exactly do you mean, "I only changed the text"?

    I think the problem resolves in the search.php template and I'm not sure why you have your h2 like this:
    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>


  • I only changed the Search form but that was in the searchform.php
  • I know the solution is probably simple but I really just want my search and category section to behave like my homepage: click on the post image and be taken to another site.
  • I can't even enter the site without being redirected.
  • Is my site http//:www.faberunashop.com redirecting you?
  • Sorry about that. It's been hacked. UGGG!
  • After 20 seconds or so, yes.
  • Sorry about that. It's been hacked. UGGG!
  • Fixed. Now, as you were saying...
  • Ah ok, so I think to resolve this, you would need to add a code in your functions.php file to only allow the URL specified in your search results. @TheDoc knows more about this than I do.
  • Cool. Thanks for your help and patience!
  • This is the code that I added to functions.php to get the post images to redirect:

    function print_post_title() {
    global $post;
    $thePostID = $post->ID;
    $post_id = get_post($thePostID);
    $title = $post_id->post_title;
    $perm = get_permalink($post_id);
    $post_keys = array(); $post_val = array();
    $post_keys = get_post_custom_keys($thePostID);

    if (!empty($post_keys)) {
    foreach ($post_keys as $pkey) {
    if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') {
    $post_val = get_post_custom_values($pkey);
    }
    }
    if (empty($post_val)) {
    $link = $perm;
    } else {
    $link = $post_val[0];
    }
    } else {
    $link = $perm;
    }
    echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';
  • If I can just get that code to place in my functions.php, I'd be a happy little camper!
  • I'm not entirely sure how to resolve this. I know a minimum of php and Wordpress. You could try either stackoverflow.com or the Wordpress forums. Hopefully someone else here can help.
  • Ok I only quickly scanned the code you posted, no idea if I have the right thing here.. but in your index.php file, right after your
    you are calling that function you posted here above: <?php print_post_title() ?>.In your archive.php and your search.php file, there is no such line after your <div class="article-over">,instead it immediately has a <h2>with the_permalink.Perhaps try adding in that same line of code like you have on your index.php page?

    I think this might solve it, but I'm not 100% sure.. because even though the whole image is clickable on your index.php page, the print_post_titlefunction only echo's a <h2>with the_permalink,which doesn't wrap around the image.. Weird, but this might be a start?
  • Thanks. I will give it a try. If you'd like backdoor access to my site, let me know.
  • Holy hell this is one heck of a thread. I think I see the problem. I'm swamped at the moment, but I'll get back to you in a couple hours!
  • Thanks Doc! {And everyone else. You guys make this neophyte's little heart happy!}
  • So, basically, anywhere you see "the_permalink", it's going to be linking to the post in question, and not your redirect. If you just comb through your search.php file you'll see what I'm talking about. But here's an example:
    <div class="article-over">
    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
  • OK, so what do I change?
  • Do I put the custom field information?
  • Yup. Treat it just like any other loop!
  • Just to be sure, here is the code on my search.php

    <?php get_header(); ?> 
    <h1>Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('&#8220;'); echo $key; _e('&#8221;'); _e(' &mdash; '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h1>


    <ul class="mcol">
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <li class="article" id="post-<?php the_ID(); ?>">

    <?php
    if ( has_post_thumbnail() ) { ?>
    <?php
    $imgsrcparam = array(
    'alt' => trim(strip_tags( $post->post_excerpt )),
    'title' => trim(strip_tags( $post->post_title )),
    );
    $thumbID = get_the_post_thumbnail( $post->ID, 'background', $imgsrcparam ); ?>
    <div class="preview"><a href="<?php the_permalink() ?>"><?php echo "$thumbID"; ?></a></div>

    <?php } else {?>
    <div class="preview"><a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_url'); ?>/images/default-thumbnail.jpg" alt="<?php the_title(); ?>" /></a></div>
    <?php } ?>

    <div class="article-over">
    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
    <div class="postmetadata">
    Posted: <?php the_time(__('F jS, Y', 'kubrick')) ?>&nbsp;&#721;&nbsp;
    <?php comments_popup_link(__('No Comments'), __('1 Comment'), __('% Comments'), '', __('Comments Closed') ); ?><br />
    <?php printf(__('Filled under: %s'), get_the_category_list(', ')); ?>
    </div>
    </div>
    </li> <?php ?>
    <?php endwhile; ?>
    <?php else : ?>


    <?php endif; ?>
    </ul>

    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <?php endwhile; ?>
    <?php else : ?>
    <h1 id="error"><?php _e("Sorry, but you are looking for something that isn&#8217;t here."); ?></h1>
    <?php endif; ?>

    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>

    <?php get_footer(); ?>


    What do i replace?
  • In this line:
    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    You'll see the_permalink. This is what links to your post. If you don't want it to link to your post, you'll have to change that.

    Anywhere you see:
    <a href="<?php the_permalink() ?>">
    It will link to the post.
  • [SOLVED] Fixed it. Instead of changing the code, which I have NO idea how to (I just tinker) I found a plugin that does it just as well. Thanks for your help!