Help in inserting a line of code in search.php

1 replies
Hi

First of all, I have been looking for a good free plugin to track shipments (Not woocommerce) on a wordpress site. Where the site admin can update the location and status and location of the parcel and it will be displayed when the tracking code is entered on the site. The only few very solutions i found are paid. Please if you know any way plugin or ways to do it, kindly recommend.

My solution
I decided to use the search function of wordpress and alter the results to reflect a specific page that i can update.
A plugin that does this easily is 'curated search'. It has advantages amongs others to limit the number of search results, redirect the search result to a specific page, specify the search terms and a dedicated section called special searches which creates posts that will appear on top of search results on your site. https://wordpress.org/plugins/curated-search/
Now all this is good.
However, they provided a piece of code to be inserted Between Header and Loop of search.php
If you would like to display content from the WYSIWYG editor of a Special Search at the top of the search results page, paste <?php do_action('cs_search_after_title'); ?> into the search.php file of your theme below the header and above The Loop.
In their video tutorial, they pasted the code in search.php of wordpress 2015 default theme and it was all easy.
I am using BeTheme and can't seem to find a place to paste this code in my search.php that you can see below.
As you can imagine, i create posts in the "special searches" area of the plugin and the preview shows "Ooops... Error 404. We are sorry, but the page you are looking for does not exist." while searches show "Ooops...No results found for:..."

Here is my search.php
Can a warrior help me with some good advice for an alternative solution or where to add the php code to this search.php file of BeTheme
PHP Code:
<?php
/**
 * The search template file.
 *
 * @package Betheme
 * @author Muffin group
 * @link http://muffingroup.com
 */

get_header();

$translate['search-title'] = mfn_opts_get('translate') ? mfn_opts_get('translate-search-title','Ooops...') : __('Ooops...','betheme');
$translate['search-subtitle'] = mfn_opts_get('translate') ? mfn_opts_get('translate-search-subtitle','No results found for:') : __('No results found for:','betheme');

$translate['published']     = mfn_opts_get('translate') ? mfn_opts_get('translate-published','Published by') : __('Published by','betheme');
$translate['at']             = mfn_opts_get('translate') ? mfn_opts_get('translate-at','at') : __('at','betheme');
$translate['readmore']         = mfn_opts_get('translate') ? mfn_opts_get('translate-readmore','Read more') : __('Read more','betheme');
?>

<div id="Content">
    <div class="content_wrapper clearfix">

        <!-- .sections_group -->
        <div class="sections_group" style="width:100% !important;">
        
            <div class="section">
                <div class="section_wrapper clearfix">
                
                    <?php if( have_posts() ): ?>
                    
                        <div class="column one column_blog">    
                            <div class="blog_wrapper isotope_wrapper">
                
                                <div class="posts_group classic">
                                    <?php
                                        
while ( have_posts() ):
                                            
the_post();
                                            
?>
                                            <div id="post-<?php the_ID(); ?><?php post_class( array('post-item''clearfix''no-img') ); ?>>
                                                
                                                <div class="post-desc-wrapper">
                                                    <div class="post-desc">
                                                    
                                                        <?php if( mfn_opts_get'blog-meta' ) ): ?>
                                                            <div class="post-meta clearfix">
                                                                <div class="author-date">
                                                                    <span class="author"><span><?php echo $translate['published']; ?> </span><i class="icon-user"></i> <a href="<?php echo get_author_posts_urlget_the_author_meta'ID' ) ); ?>"><?php echo get_the_author_meta'display_name' ); ?></a></span>
                                                                    <span class="date"><span><?php echo $translate['at']; ?> </span><i class="icon-clock"></i> <?php echo get_the_date(); ?></span>
                                                                </div>
                                                            </div>
                                                        <?php  endif; ?>
                                                        
                                                    
                                                        <div class="post-title">
                                                            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                                                        </div>
                                                        
                                                        <div class="post-excerpt">
                                                            <?php the_excerpt(); ?>
                                                        </div>
        
                                                        <div class="post-footer">
                                                            <div class="post-links">
                                                                <i class="icon-doc-text"></i> <a href="<?php the_permalink(); ?>" class="post-more"><?php echo $translate['readmore']; ?></a>
                                                            </div>
                                                        </div>
                            
                                                    </div>
                                                </div>
                                            </div>
                                            <?php
                                        
endwhile;
                                    
?>
                                </div>
                        
                                <?php    
                                    
// pagination
                                    
if(function_exists'mfn_pagination' )):
                                        echo 
mfn_pagination();
                                    else:
                                        
?>
                                            <div class="nav-next"><?php next_posts_link(__('&larr; Older Entries''betheme')) ?></div>
                                            <div class="nav-previous"><?php previous_posts_link(__('Newer Entries &rarr;''betheme')) ?></div>
                                        <?php
                                    
endif;
                                
?>
                        
                            </div>
                        </div>
                        
                    <?php else: ?>
                    
                        <div class="column one search-not-found">
                        
                            <div class="snf-pic">
                                <i class="themecolor <?php mfn_opts_show'error404-icon''icon-traffic-cone' ); ?>"></i>
                            </div>
                            
                            <div class="snf-desc">
                                <h2><?php echo $translate['search-title']; ?></h2>
                                <h4><?php echo $translate['search-subtitle'] .' 'esc_html$_GET['s'] ); ?></h4>
                            </div>    
                                        
                        </div>    
                        
                    <?php endif; ?>
                    
                </div>
            </div>
            
        </div>

    </div>
</div>

<?php get_footer(); ?>
#code #inserting #line #searchphp
  • Profile picture of the author noah.whitmore
    Sounds like you have found a solution. Very crafty. But, as in so many situations, your solution has created additional problems - don't you hate that ?? So, let's take a look at the issue that your solution created . The plugin instructions say to place the code below the header and above the loop.

    So, let's find those two spots in your search.php code. First, the header is usually relatively simple to find, since WordPress usually calls the header with a special function of get_header()...

    PHP Code:
    <?php 
    /** 
     * The search template file. 
     * 
     * @package Betheme 
     * @author Muffin group 
     * @link http://muffingroup.com 
     */ 

    get_header(); // <---- THIS IS YOUR HEADER

    $translate['search-title'] = mfn_opts_get('translate') ? mfn_opts_get('translate-search-title','Ooops...') : __('Ooops...','betheme'); 
    $translate['search-subtitle'] = mfn_opts_get('translate') ? mfn_opts_get('translate-search-subtitle','No results found for:') : __('No results found for:','betheme');
    So, we need to go below that. It also says to put it above the loop. The loop should stand out because it uses the PHP function 'while'...

    PHP Code:
             
                <div class="section"> 
                    <div class="section_wrapper clearfix"> 
                     
                        <?php if( have_posts() ): ?> 
                         
                            <div class="column one column_blog">     
                                <div class="blog_wrapper isotope_wrapper"> 
                     
                                    <div class="posts_group classic"> 
                                        <?php 
                                            
    while ( have_posts() ): // <--- THIS IS THE START OF YOUR LOOP
                                                
    the_post(); 
                                                
    ?> 
                                                <div id="post-<?php the_ID(); ?><?php post_class( array('post-item''clearfix''no-img') ); ?>
                                                     
                                                    <div class="post-desc-wrapper">
    So, we could put the code right above the loop. But, you need to be careful of your <?php and ?> tags. These tags allow us to go 'out' of HTML and into PHP. If you are already within PHP code (meaning that you are already in between the <?php and ?>) then you can't put another <?php tag or it will cause an error.

    So, don't do this...

    PHP Code:
                                   <div class="posts_group classic"> 
                                        <?php 
                                            
    <?php do_action('cs_search_after_title'); ?>
                                            while ( have_posts() ): 
                                                the_post(); 
                                                ?> 
                                                <div id="post-<?php the_ID(); ?><?php post_class( array('post-item''clearfix''no-img') ); ?>
                                                     
                                                    <div class="post-desc-wrapper"> 
                                                        <div class="post-desc">
    See how I've got a <?php tag and then another? That will cause an error. Instead, you'll want to do one of two things...

    Option 1
    Just put this new code before the <?php that is used for the loop. Here I'm putting it on the second line in the code snippet
    PHP Code:
                                   <div class="posts_group classic"> 
                                       <?php do_action('cs_search_after_title'); ?>
                                        <?php 
                                            
    while ( have_posts() ): 
                                                
    the_post(); 
                                                
    ?> 
                                                <div id="post-<?php the_ID(); ?><?php post_class( array('post-item''clearfix''no-img') ); ?>
                                                     
                                                    <div class="post-desc-wrapper"> 
                                                        <div class="post-desc">
    Option 2
    Remove the <?php and ?> from the code that you are inserting
    PHP Code:
                                   <div class="posts_group classic"> 
                                        <?php 
                                            do_action
    ('cs_search_after_title');
                                            while ( 
    have_posts() ): 
                                                
    the_post(); 
                                                
    ?> 
                                                <div id="post-<?php the_ID(); ?><?php post_class( array('post-item''clearfix''no-img') ); ?>
                                                     
                                                    <div class="post-desc-wrapper"> 
                                                        <div class="post-desc">
    Both of these options will do the same exact thing. Let me know if that takes care of it or not - I'd be happy to trouble shoot with you!
    Signature
    No Pitch For The Moment - Just A Nice Hello.
    So... 'Hello'
    Feel free to PM me if you have any questions about my posts. I'd like to hear from you!
    {{ DiscussionBoard.errors[10601027].message }}

Trending Topics