wordpress custom theme search has quantity but displays no results - php

Trying my best to wrap my head around wordpress search process and results.
Been reading and researching online for about 8 hours straight at this stage and have gotten almost nowhere.
I have a completely custom theme, designed by a friend for a client. I have a search box in a nav bar at the top of the page, inside the header. I'd like the user to be able to search the site's content from that search box in the nav bar at any time. I've found plenty of examples of this being accomplished on other pages, though they often seem to be using a ready-made theme.
Eventually I'd like to be able to understand what's going in the search.php file well enough to edit it along with the searchform.php file so that the search box and results appear unified with the rest of the theme.
At the moment the closest I've gotten to having anything functioning at all is the inclusion of a quantity of search results showing the appropriate number of hits expected, but I still can not figure out how to display a result.
I've been combing through others' page sources and every hit I can google about customising or just explaining the search.php file, and yet all I keep finding is "read the Creating a Search Page" and "it must be a theme issue." This is not gainful information.
My understanding of what's being taught in the "Creating a Search Page" info is that it describes the steps for creating a custom search page that one can hyperlink / permalink to, but which is unrelated to the default search function within the wordpress kit. Indeed, I've successfully created a custom search page, but it behaves simply as a stand-alone page, and is not related to a customised search results page. To customise the look of default search function results, I understand the search.php file must be edited. So far the "what to edit" within that file escapes me, but for now I'd just like to understand why I am seeing the correct number of results, but no actual results.
My search.php file looks like this:
<?php get_header(); ?>
THIS IS THE TOP OF SEARCH.PHP
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="search-title">
<?php echo $wp_query->found_posts; ?> <?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>"
</h1>
</header><!-- .page-header -->
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
/*
// Previous/next post navigation.
twentyfourteen_paging_nav();*/
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
</div><!-- #content -->
</section><!-- #primary -->
THIS IS AFTER THE SECTION OF SEARCH.PHP
<?php
// get_sidebar( 'content' );
// get_sidebar();
get_footer();
*Note: The notes in CAPS are to help me understand what content is coming from where as I try to figure out what each phpiece of the puzzle is doing.
Also, the twentyfourteen_paging_nav line is commented out as it kept spitting back an error. The original search.php was from a (default theme?) folder called "twentyfourteen". My current folder is called "ewgi2014" however, replacing twentyfourteen_paging_nav with ewgi2014_paging_nav spat back the same error, and I can't find any more useful information on the topic. Also, when looking at other sample search.php files I didn't see the paging_nav call, so I tried commenting it out, and was able to at least avoid the error, though I still don't understand it.
Sample output on the page after searching for the term, say, "program" is:
8 Search Results Found For: "program"
...with nothing else written beyond that.
Not entirely relevant, I believe, but my searchform.php looks like this:
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<input type="search" class="search-field" placeholder="search …" value="" name="s" title="Search for:" />
</label>
<input type="submit" class="search-submit" value="Search" />
</form>
I've read and re-read the "Creating a Search Page" page from the Codex so many times I could probably recite it by heart, but haven't found any solutions within.
I've read the stackoverflow hit about "How to display Wordpress search results?" and have tried the code from ThemeShaper, but that throws an error on the shape_content_nav line(s).
I've read this and ... and apparently I can't link to all the other things I've read as this is my first post, but I have been reading a lot, and fiddling, and making very little headway.
Apologies for the long-winded post. Just trying to be thorough and show my research and current point of understanding.

What happens if you put
the_title();
Directly after your while statement?
Each time you loop within the while loop, you are processing each post. Currently, on each loop, you are entering a content-*.php file, where * depends on the post format. Does that file exist?

Related

How to use wp loop in search.php

I want to get the result in search.php include posts and products only, and the posts list will be display in POST tab, Products list in PRODUCT tab.
I tried to use this code for loop but it get all posts and products:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile;?>
<?php endif; ?>
You may want to try adding a post_type argument to the URL.
www.example.com?s=test&post_type=product (shows products)
www.example.com?s=test&post_type=page (shows pages)
This is not exactly what you are asking because this is 2 separate pages (not 1 page with 2 tabs). You can add a hidden input field to a form in order to have the browser automatically append the post type to the URL. Ie.
<form method="get" action="<?= get_bloginfo( 'url' ); ?>">
<input type="hidden" name="post_type" value="product">
<input type="text" name="s" value="<?= get_search_query(1); ?>" placeholder="Search Products...">
<button type="submit">Go</button>
</form>
To show 2 tabs on the same page, you can use search.php and ignore the main query, and simply write two of your own queries by accessing $_GET['s'] (the function, get_search_term(), also accesses $_GET['s'] but can also sanitize it). Alternatively, you could use a page template to write your own queries, showing search results on a different URL (you would have to modify all search forms to go here instead).
When you visit your websites home URL and add ?s= to the URL, WordPress knows to serve search.php and run a "main query" (using WP_Query) behind the scenes. The main query is what you are looping through when you call while( have_posts() ) etc.
Writing your own queries to show search results may make your code less compatible with plugins that modify the main query to show more meaningful search results. It depends on how they do the logic of whether or not to modify a specific query. For example, they may check that the query is a main query and that you are on the search.php page. When you write your own queries using WP_Query or get_posts(), it's not a main query anymore.
WP_Query (or get_posts() which calls WP_Query) should have an argument where you can specify the search term(s) and it may take care of parsing out all the different words and turning it into (somewhat) useful SQL. You should read the docs on WP_Query for more info.
Sorry this is not a full answer but perhaps it will lead you in the right direction.

My wordpress custom post displays the content rather than the excerpt

I've made a custom post type called 'news', plus an index page called 'archive-news'. In the archive page the excerpt is replaced with the content of the post. Can't work out how to change it.
I've been using HTML for over a year, familiar with the idea of Wordpress PHP but probably not deep enough yet. Had a look for similar problems. The input area for the excerpt has not been left blank (when you edit a custom post). I've heard that if it doesn't then it defaults to use content instead of excerpt. Cleared the cache many times and given it time to refresh.
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="postExcerpt">
<p><?php the_excerpt(); ?></p>
</div>
<?php endwhile; wp_reset_query(); ?>
should show the excerpt and not the content in the archive-news

Changes to page.php and single.php not applying

I'm having quite a weird issue with my WordPress site. I am building a theme from scratch and I'm trying to make change to the containers on my blog page (page.php and single.php). Weirdly enough, I have made no major changes to the code other than add a wrapper, but the changes aren't showing up.
I've made many themes without this issue so I'm unsure what's going wrong this time. I have set the blog page to be the posts page in the settings so I can't understand why the template won't update. The code I have is below, and is just the standard WordPress code with the exception of 'grid wrapper' and attempting to remove the sidebar which also shows.
I have done quite a bit of research on this to see what the problem could be, but sadly the only post similar was in 2014 with no answer.
<div class="container main-content">
<div class="grid wrapper">
<section class="the-content">
<main>
<h1><?php the_title(); ?></h1>
<?php while ( have_posts() ): the_post(); ?>
<?php get_template_part('template-parts/content', 'page'); ?>
<?php endwhile; // End of the loop. ?>
</main>
</section>
</div>
</div>
Long story short, my content shows just fine. I just can't edit the wrapper which contains that content.
I would change parameters in get_template_part()
This is how it supposed to be used: Understanding get_template_part
So if you have blank Wordpress file structure, I'm not sure if you have template-parts for sure. You can have them only if you intentionally made them.
Use wordpress Wp-Super_cache Plugin
I had the same issue. Most likely index.php is overriding your file as in my case. It depends how your theme handles the loop so check out on your index.php and change it there.
If i remember well, the blog page set in the back-office reference first at home.php and then index.php. Page and single aren't call.

Adding content to wordpress page

So, i finally got my css and js scripts to load on WP
Now there is but one thing i need to get done.
i have my own theme, including header.php, footer.php, page.php
header.php and footer.php is working just fine, loading scripts and showing properly, but now i need to add all the other content.
my page.php is currently:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<?php get_footer(); ?>
I would need to somehow add html content to pages, i have about 20 ready made .php pages which needs to be transfered to WP.
Soooo how do i go about making new page (pages -> add new) and using page template while getting the html content to show up?
I've tried to just make new page and in text mode add up all the html into page, but it only shows empty page with header and footer, so the problem is most likely the page.php and i have no idea how to get it to work.
You can do look like this:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile;
?>
<?php get_footer(); ?>
You are on the good way. While developing a custom theme from scratch is a great challenge it's not too hard.
I could recommend to take it easy and follow this tutorial I found really helpful some time ago, I learned a lot there:
Developing a WordPress Theme from Scratch
You must have the official source documentation always in your mind:
Theme Development
Do some reading and you will see that making themes is really fun and gratifying :)
EDIT:
I would recommend picking a good starter theme or framework and work using child themes. You have plenty of them to pick.
To get started adding a new page to your WordPress site, find the Pages menu in the WordPress Dashboard Navigation menu. Click Add new.
The WordPress page editor looks nearly identical to the post editor, except for a few different boxes located on the right side of the screen.
Add the title of the page, like About. Note: If you have pretty permalinks set up, the title of your page will also be the URL slug.
Next, add some content.
The Publish section of the page editor is exactly the same as for writing posts. When you’re ready to publish, you can either publish immediately, save this or a draft, or schedule the page to be published later.
The Page Attributes section applies a parent page and template to your new page. For the Parent section, you can arrange your pages into hierarchies. For example, you could create this new page with additional pages under it. There are no limits to how many levels you can nest pages.
Some WordPress themes have custom page templates, so the next Template section allows you to apply a template to your new page.
The Order box allows you to order your page numerically. Pages are usually ordered alphabetically, but you can choose your own order by entering a number in this field.
Preview the page one last time, then click Publish. You’ve added a new page to your WordPress site.
This is how your
your index.php should look like :
<?php
get_header();?>
<div class="YourContainer">
<div class="Whatever">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="ContentSectionDiv">
<?php the_content();?>
</div>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
</div>
</div>
<?php get_footer();?>
you can make also a custom loop
<?php
$arg = array("post_type" => "services",
"posts_per_page" => 9,
"order_by" => "date",
"order" => "ASC",
);
$services = new WP_Query($arg);
if ($services->have_posts()):;
while ($services->have_posts()):$services->the_post();
the_content();
endwhile;
endif;
?>

Replace word in template page with title of page in php

I am now starting to realize the infinite ways a person can utilize php.
I have created a template page called new-member.php that calls in the content from another page so that when I add a new member, it is fast and easy.
The path I use now is:
I add a new page with the company name as the title.
I choose the new member template.
I Click publish. This creates a "new member coming soon" page that is up in seconds. Perfect!
But I would like to take it to a higher level and have a php function replace "New Member" with the page title (The company name) so that when I add a new page, not only does it have the new members url, but instead of saying "New Member coming soon" it says "XYZ Company Coming Soon!" which is a temporary page until the official one is built.
Could someone please show me the correct code and where to place that code? I am using wordpress. I have no clue how to write this code. Does the code go into the new member.php template? The header? I have no idea but want to learn!
Please help!
What you need to do there (if I were in your position I would have put together a plugin or do some functions.php hacking) is capture the output of the included page and replace your specific tag "New Member" with what you need. In the below example i'm going to use %NEW_MEMBER_NAME% instead of "New Member", which makes your replacement string more unique, so that in your placeholder wp page you will write
"%NEW_MEMBER_NAME% comig soon".
<?php /* Template Name: New Member */ ?>
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
the_post();
// i don't think you need this anymore
// get_template_part( 'content', 'page' );
?>
<div class="entry-content">
<?php if(function_exists('iinclude_page')) {
ob_start();
iinclude_page(112);
$content = ob_get_clean();
echo str_replace('%NEW_MEMBER_NAME%',the_title('','',false),$content);
} ?>
</div>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Remember that it's never a good ideea to hardcode things in your templates, as you are doing here with "112", the id of your placeholder wp page, and presumably the replacement tag %NEW_MEMBER_NAME%.
You should fork the theme, and create an admin options page for these two.
Hope this hepls you in some way.
Edit your newmember.php template file. Find 'The Loop'. This is where your post content is shown. Echo the title out.
http://codex.wordpress.org/Function_Reference/the_title
//syntax
<?php the_title( $before, $after, $echo ); ?>
//add this to your newmember.php template
<?php the_title('<h3>', '</h3>', true); ?>
If you post what is in your newmember.php template file then a more specific answer can be provided. But this is simple enough, I think you can probably figure it out on your own.
Not sure If I understood your question correct.
But here is what I think you may have to do.
Just replace
New Member coming soon
with
<?php the_title();?> coming soon
Now you will get the page title instead of "New Member".

Categories