viernes, 19 de junio de 2015

Add Search to bbPress WordPress Plugin


When working with bbPress I found struggles to get search to work properly, and hopefully after this article you wont have the same struggles I had. I didn’t see any point in building what is already built so I decided to first search the WordPress plugin directory to see what was currently available. After a few different plugins I settled on using Search bbPress from Stephen Carroll. Search bbPress works by extending the default WordPress search to include the bbPress custom post types. Now you know the basics, lets get started below.
This tutorial assumes you want all your theme search results being displayed in a table like your forums are structured.

Step 1:

Download and install the Search bbPress plugin. You can either search for the plugin from your WordPress dashboard or simply go here and download the plugin. Once you have installed the plugin go ahead and activate it. Nothing fancy or exciting will happen (that you can see ;) ) but it’s working.

Step 2:

Go to your widget section under Appearance -> Widgets. Drag the default Search widget to the sidebar that you would like to display it in give it a title and that is it for step 2.

Step 3:

This step is where things could get a little tricky. Since bbPress forums are displayed using tables most likely the results look a little weird since your theme’s search.php doesn’t have the results being displayed in tables. If you think the results are just fine then you are good to go from here.
In my case, I wanted to have my results displayed in tables so I had to open the search.php and style.css files in my editor and make some changes.
Results in a table
First you need to find the loop in the page. The loop should look something like this:
view sourceprint?
001<!--?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?-->
002 
003// Content inside the loop
004 
005<!--?php endwhile; else: ?-->
006 
007<!--?php _e('Sorry, no posts matched your criteria.'); ?-->
008 
009<!--?php endif; ?-->
Since we want each result to be inside a table row, we need to add the following inside our loop.
view sourceprint?
001<tr>
002    <td><a href="<?php the_permalink() ?>><?php the_title(); ?></a></td>
003 
004    <td><?php the_time('l jS F, Y - g:ia') ?></td>
005</tr>
If you are not familiar with tables the tr stands for table row and the td stands for table data. So what this is doing is adding a row with the title and the time inside of the row. Now, we need to finish up the table information that will go outside the loop.
If there are results that are found, we want to display the table and give the table columns a headline. I am going to break up the loop and add a title to the results stating the results for what I searched for.
view sourceprint?
001<?php if (have_posts()) : ?>
002 
003<h1><?php printf( __( 'Search Results for: %s' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
004 
005<table>
006    <thead>
007        <tr>
008            <th>Title</th>
009            <th>Date</th>
010        </tr>
011    </thead>
012 
013    <tbody>
014 
015<?php while (have_posts()) : the_post(); ?>
016 
017<tr>
018    <td><a href="<?php the_permalink() ?>><?php the_title(); ?></a></td>
019 
020    <td><?php the_time('l jS F, Y - g:ia') ?></td>
021</tr>
022 
023<?php endwhile; ?>
024 
025<?php endif; ?>
026 
027    </tbody>
028 
029</table>
Now you should see your results being displayed just like your forum tables are displayed. Something I would recommend adding to your pages is a simple line of code to limit the number of results on your page.
view sourceprint?
001<?php $posts=query_posts($query_string . '&posts_per_page=20'); ?>
You can change the 20 to the number of results you would like to display.
Here is the final code to display your search results.
view sourceprint?
001<div class="search-results-wrap">
002 
003<?php $posts=query_posts($query_string . '&posts_per_page=20'); ?>
004 
005<?php if (have_posts()) : ?>
006 
007<h1><?php printf( __( 'Search Results for: %s' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
008 
009<table>
010    <thead>
011        <tr>
012            <th>Title</th>
013            <th>Date</th>
014        </tr>
015    </thead>
016 
017    <tbody>
018 
019<?php while (have_posts()) : the_post(); ?>
020 
021<tr>
022    <td><a href="<?php the_permalink() ?>><?php the_title(); ?></a></td>
023 
024    <td><?php the_time('l jS F, Y - g:ia') ?></td>
025</tr>
026 
027<?php endwhile; ?>
028 
029<?php endif; ?>
030 
031    </tbody>
032 
033</table>
034 
035</div>
If you have other questions regarding how to display your forums or how to use bbPress shortcodes, see the following posts:
  • How to setup and install bbPress forum
  • bbPress Shortcodes

No hay comentarios:

Publicar un comentario