Display wordpress search results - php

I'm trying to make a custom search page, by creating all new search.php file, for my wordpress template...so far, so good.
The problem is that when I search for something it doesn't show any results.
I'm guesing that it has something to do with some php script or something I don't know.
How can I fix that ?
P.S The function for the number of the results works fine, but there isn't any results.
Here is the content of search.php
<?php
get_header();
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1>Search Results</h1>
<?php endwhile; ?>
<?php else : ?>
<?php _e( 'Nothing Found' ); ?>
<?php endif; ?>
<?php
get_footer();
?>

The problem is that you don't have anything in your loop to print the results, i.e.
<?php while (have_posts()) : the_post(); ?>
<h1>Search Results</h1>
<!-- Needs something here -->
<?php endwhile; ?>
To fix the problem, simple replace <!-- Needs something here --> with the following
<a href="<?php the_permalink() ?>">
<h2><?php the_title(); ?></h2>
</a>
<p><?php the_excerpt(); ?></p>
You also need to move <h1>Search Results</h1> to above the loop to stop it from displaying multiple times. It may be best to move it above the if statement if you don't intend on adding it to your else statement as well.

Related

clicking on a post link leads to a blank page in php wordpress

I have been trying to learn PHP, and I am stuck at one point. I created some posts and made them as links, but on clicking them, they lead me to a blank page.
Here is the index.php code
<?php
while(have_posts()){
the_post();?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php
}
?>
Here is the single.php code
<?php
while(have_posts()){
the_post();?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php
}
?>
I changed the href value to another working link, and it was working pretty well, the problem maybe with permalink. I went to WordPress dashboard->permalink and tried all kinds of common settings, but the problem still exists, could someone help me out?
You have to use PHP functions always wrapped inside <?php - ?>
<?php
while(have_posts()){
the_post();?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php
}
?>

Wordpress Custom Theme: Plugins Won't Load or Display Only Code

This is my first attempt at creating a theme purely from scratch. Before this I just used underscores_me and made most of my changes to style.css and left the majority of PHP alone, because I'm a novice with it.
My issue is that plugins are not working. None of the ones I have installed work. At this point I have been trying plugins that create an event calendar, but I'm assuming that any and all plugins will have issues. In the areas that are meant to display the calendar, there are two things I'm seeing. Plugin-generated pages show nothing at all (aside from the theme visuals) and plugins that are inserted into an admin-created page display plugin-generated code.
I am using WampServer. I have wp_footer(); and wp_head(); in the correct places. My functions.php file was created from the example found at https://scanwp.net/blog/create-a-wordpress-starter-theme-from-scratch/ and the only adjustment I have made to it so far is to remove the fontawesome line of code.
My index.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "&#9755 "; ?><?php the_title(); ?></h1>
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display post content
endwhile;
endif;
?>
<?php get_footer(); ?>
And my page.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "&#9755 "; ?><?php the_title(); ?></h1>
<?= get_post_field('post_content', $post->ID) ?>
<?php get_footer(); ?>
First of all, you have to understand that in your displayed files, there are no functions that recall objects that will later show the page content.
Then, in your index.php file there is an error, besides what was said above, because you're calling the_title () (function) that extrapolates the title of a post or page via the post object, which in this case should be extracted within the while loop contained within the if condition.
Then try editing the files as follows.
index.php
<?php
get_header(); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if( is_singular() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else: ?>
<h1>No posts to display</h1>
<?php endif; ?>
<?php get_footer(); ?>
and page.php
<?php
get_header(); ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
<?php get_footer(); ?>
However, within the wordpress codex you will find all the guides well written on any type of function, look no further.
source: https://codex.wordpress.org/

HTML only print once in Wordpress Loop

I have a code chunk that is inside the_content(); I'm using acf repeater as well. So when I post a blog, I'll either use the_content(); or the acf field. I have h2 tag ( latest articles ) that I only want printed one time, but it's printing everytime I make a post.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="container">
<div class="row">
<div class="col-md-4 sidebar-r">
<?php echo the_content(); ?>
</div><!-- end sidebar-r -->
<?php
$i = $wp_query->post_count;
if($i <=1) {
echo '<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>';
}else{
echo '';
}
?>
<div class="col-md-8 links-wrap">
<?php if(have_rows('daily_links')): ?>
<?php while(have_rows('daily_links')): the_row(); ?>
<a href="<?php the_sub_field('link_url'); ?>" target="_blank">
<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>
<h3 class="link-source">
<?php the_sub_field('link_source'); ?>
</h3>
</a>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- end links wrap -->
</div><!-- end row -->
</div><!-- end container -->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
You'll see I tried using php to count the posts and if more than one post, don't print the tag, but couldn't figure out the exact logic and syntax.
I am honestly struggling a bit to understand exactly what you are trying to do and since I do not even have the posts and other key pieces of information so that I can properly replicate your issue so that I can help you better, this is a little bit challenging. That being said, looking into some ideas I came across another stackoverflow question/answer that might be relevant for you in catching the first post and does something to it. The answer to the referenced question instance was this:
<?php if (have_posts()) : $postCount = 1; while (have_posts()) : $postCount++; ?>
<?php if($postCount == 2) { ?>
// SOMETHING TO DO WITH FIRST POST
<?php } else { ?>
// SOMETHING TO DO WITH ALL OTHER POSTS
<?php } ?>
This was suggested by user Bora in this answer from 2013.
Let me know if that helped!

if statement inside of wordpress post loop

I've tried to add an if statement inside of the wordpress loop to not show anything inside of a category, but the posts are still showing up on the front-end, I've got a feeling its something to do with the colon on the end of as if I change it to a semi colon I get an error (500) but I'm just not sure how to fix it. Does anyone have any idea how to get an if statement into the wordpress post loop?
<div class="row page-wrapper">
<?php if ( have_posts() ): the_post(); ?>
<?php if ( !in_category('65') ): ?>
<?php WpvTemplates::left_sidebar() ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(WpvTemplates::get_layout()); ?>>
<?php
global $wpv_has_header_sidebars;
if( $wpv_has_header_sidebars) {
WpvTemplates::header_sidebars();
}
?>
<div class="page-content">
<?php
rewind_posts();
get_template_part('loop', 'category');
?>
</div>
</article>
<?php WpvTemplates::right_sidebar() ?>
<?php endif ?>
<?php else: ?>
<article>
<h1 style="text-align: center;margin-top: 35px;"><?php _e('Sorry, nothing found', 'church-event') ?></h1>
</article>
<?php endif ?>
This is the code I've tried to add seen on line 3 and line 20.
I'm just looking to hide all posts from that category from showing on the search page in this case.
Any help would be greatly appreciated, if I can gain an understanding of what exactly the colon does that would be great too.
Thank you

featured post same as post content

I have a wordpress site I am creating.
on the post pages, I have text widget with PHP allowed, where I have a custom loop:
<?php $my_query2 = new WP_Query("showposts=1&cat=9,10,11,18,19&orderby=rand"); ?>
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>">
<div class="home-widget-thumb">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('home-thumb');
}
?>
</div>
<h2><?php the_title(); ?></h2></a>
<div class="body">
<?php echo get_the_excerpt(); ?>
</div><!--body-->
</br>
<span class="more-link">
[more]
</span>
<?php endwhile; ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
For some reason, whatever blog post you are on is the same as in the loop I created.
See an example here:
http://counselingandtraining.com/play-therapy/
The loop for the post is not modified.
Can anyone tell me why this is happening?
Let me know if I can provide further info.
Thanks in advance for your time.
Chris
It looks to me like the problem is that you are still using the base query.
You can change this by adding your variable $my_query2 in the code like this:
<?php if ($my_query2->have_posts()) : ?><?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
This will make all the functions like the_title(), the_content(), etc work as intended since they will be set to $my_query2.

Categories