• Resolved ejm

    (@llizard)


    I am using the Asteroid Child theme and would like to customize the search results without adding a search.php page. Is there a way to do that in the theme’s functions.php?

    This is as far as I’ve gotten to find the following coding in index.php of the Asteroid theme…

    <!-- Post Not Found -->
    		<div class="wrap-404-box">
    			<h2><?php _e('Search Results: Nothing Found', 'asteroid'); ?></h2>
    			<p><?php _e('Try a new keyword.', 'asteroid'); ?></p>
    			<?php get_search_form(); ?>
    		</div>

    E. Morris, who knows just enough about coding to get in trouble

Viewing 10 replies - 1 through 10 (of 10 total)
  • Theme Author ronangelo

    (@ronangelo)

    @llizard

    I am using the Asteroid Child theme and would like to customize the search results without adding a search.php page.

    What do you mean by “customize”? Modifying the results or the appearance? In what way?

    Thread Starter ejm

    (@llizard)

    Thank you for your reply, ronangelo.

    What do you mean by “customize”? Modifying the results or the appearance? In what way?

    I would like to modify the words in the heading of the wrap-404-box “Search Results: Nothing Found” AND the words in the <p> “Try a new keyword. I’m guessing by the presence of “asteroid” in that there might be a function I can use. I just don’t quite know where to look for that function.

    <?php _e('Search Results: Nothing Found', 'asteroid'); ?>

    Again, I’m very much hoping not to have to create a search.php for the Asteroid Child but to make the change in the theme’s functions.php

    edit: I tried adding the following to functions.php, but of course, even though it doesn’t throw any errors, it didn’t work.

    if ( is_search() ) {
        // add external search form (Google, Yahoo, Bing...)
    echo $wp_query->found_posts .' search results for [' . attribute_escape($s) .']';
    }

    Theme Author ronangelo

    (@ronangelo)

    You can use functions.php to translate/change those words.
    https://ronangelo.com/change-or-translate-text-on-a-wordpress-theme/

    Thread Starter ejm

    (@llizard)

    Fantastic! I’ll give that a shot. Many thanks, ronangelo!

    Thread Starter ejm

    (@llizard)

    The translate/change words coding works wonderfully. Thank you again.

    My struggle continues: how can I add the following to the top of the search results page? Must I create a search.php?

    <?php $wp_query->found_posts .'results for [' . get_search_query() .']'

    Also, I have been unable to find where the coding is in the Asteroid files that produces the search result. I was hoping to show search results with title and the date only. Something along this line:

    <ul>
    <?php if [SOMETHING GOES HERE BUT WHAT??] : foreach [SOMETHING GOES HERE TOO BUT WHAT???]; ?>
            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a> (<?php the_date(); ?>)</li>
        <?php endforeach; else: ?>
        <li><?php _e('Alas, nothing matched your criteria; please use the search form at the top of the page'); ?></li>
        <?php endif; ?>
        </ul>

    I finally found that .blog-view is how the results are styled.

    Theme Author ronangelo

    (@ronangelo)

    @llizard
    My other newer theme Frontier outputs the search terms. I actually just forgot to add this functionality on Asteroid theme. You can however do something similar by adding this on your functions.php

    function display_search_terms() {
    	if ( is_search() ) {
    		echo '<div id="my-search-terms">';
    		echo 'Results for [ ' . get_search_query() . ' ]';
    		echo '</div>';
    	}
    }
    add_action( 'ast_hook_before_content', 'display_search_terms' );

    You can then use the id “my-search-terms” to style that div.

    I was hoping to show search results with title and the date only.

    Easiest way I could think of is to just hide through css.

    body.search-results article * {display: none;}
    body.search-results article .entry-header,
    body.search-results article .entry-header *,
    body.search-results article .entry-date {display: inline;}

    Thread Starter ejm

    (@llizard)

    Thank you, once again, ronangelo. This display_search_terms() is most helpful.

    But I’m not sure that using “display:none” for not showing the content is the way to go here. I worry that it might flag the site as an undesirable for the big search engines. (I get the impression they aren’t too keen on “display:none” when there is content there to be seen if the styles are disabled.)

    I know the following must be wrong wrong wrong. But this is as far as I can guess from staring at https://codex.www.ads-software.com/Class_Reference/WP_Query

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    		<?php if ( $wp_query->found_posts == !is_singular() ) : ?>
    	<ul>
    <?php  if ( $the_query->have_posts() ) {
    	while ( $the_query->have_posts() ) {
    		$the_query->the_post();
    		echo '<li>' . get_the_title() . '</li>';
    	} ?>
    </ul>
    <?php } else {
    	// no posts found
    }?>
    Theme Author ronangelo

    (@ronangelo)

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<div>
    		<h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h2>
    		<div class="entry-date"><?php the_time(get_option('date_format')); ?></div>
    	</div>
    <?php endwhile; else: ?>
    	<p>No Search Results</p>
    <?php endif; ?>
    Thread Starter ejm

    (@llizard)

    Thank you once more, ronangelo.

    Thread Starter ejm

    (@llizard)

    I really am most appreciative for all the help, ronangelo. But after all that you have done, I have decided to go with adding a search.php in the child folder:

    <?php
    /*
    Template Name: Search Page
    */
    
    get_header(); ?>
    
    <div id="content">
    
    <p><strong>WordPress found <?php echo $wp_query->found_posts; ?> search results for [<?php echo attribute_escape($s); ?>].</strong></p>
    
    <ul class="search-results-list">
        <?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
            <li><h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a> (<?php the_date(); ?>)</h3> <?php the_excerpt(); ?></li>
    
        <?php endforeach; else: ?>
    </ul>
        <p><?php _e('Please try again using different search term(s).'); ?></p>
    
     <?php get_search_form(); ?>
        <?php endif; ?>
    
    </div>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    The other function fixes have been most informative though. Thank you again.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘how to customize search results in Asteroid Child’ is closed to new replies.