WordPress Search results Programmatically - php

I want to include an automatic search results output on my 404 page based on the url that the user has type in.
The problem is that on the usual search.php file, wordpress gets the value from the URL parameter and uses the regular loop like so:
<?php while (have_posts()) : the_post(); ?>
<h1>Search Results</h1>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
How do I get it so that I can manually enter the search term in the code?
Much appreciated.

You want to automatically search with the value from url?
maybe you can show your similiar post with that value, or show them a recent post with search form , using search for your site and google.com.
may be this link could help Built Effective 404 Error Pages
Hope this help,

Related

Home.php using for blog content can't retrieve page title - Wordpress

Hi I want to do the blog part in my wordpress theme, I set up using static front page et blog page to display latest post.
So in my home.php I have :
<?php get_header(); ?>
<section class="header--page">
<div class="header--img">
<?php the_post_thumbnail() ?>
</div>
<div class="container">
<h1><?php the_title() ?></h1>
</div>
</section>
<?php get_footer(); ?>
But instead of getting title from page and featured image from page, It display those from the first posts, any idea ?
I try on my page.php and front-page.php, both are returning correct title and image
So things like the Posts page function a little differently than a standard page. Most likely, your the_title() function is returning the title of the first post to be displayed.
What you'll need to do is this instead:
<h1><?php echo get_the_title(get_option('page_for_posts')) ?></h1>
What needs to happen is you need to grab the ID of the Posts page, and use that ID to find its title.
So you'll want to use get_the_title() instead, because the_title() doesn't allow you to pass an ID of a page or post. get_the_title() accepts an ID as a parameter.
The page's ID that you specified as your Posts Page in the WP Settings > Reading can be grabbed by using get_option('page_for_posts').
So putting it all together, the code snippet above will get the title of the Posts Page, and echo it on-screen. (get_the_title() doesn't automatically echo it's returned value like the_title() does, hence the addition of the echo.

Wordpress permalink redirection

I am trying to create a theme for Wordpress where I have
0. landing page with static info and 6 latest posts. When user clicks on post title it is supposed to open it
1. all articles page with the same clickable function
What I've done so far
0. Pages index.php and page.php(this is the articles list page). In Wordpress settings those pages are added to "Pages" and page.php is child of index.php
1. This is my code in both files.
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<article class="post">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</article>
endwhile;
else :
echo '<p>No content found</p>';
endif;
?>
The problem:
It redirects me to the homepage again, just adding post ID to the link. The post IS actually displayed at the bottom of the page, but the previous content is still showing up (I mean, homepage is still there, just with the post at the bottom).
So, what may be wrong?
P.S. I don't have any active plugins and I have default .htaccess
Your problem is hard to understand, have you tried saving your Permalinks again and reproducing the problem in a private browser screen? (301 redirects get be cached as long as possible in your browser, so that might be a reason you (still) get redirected)

How do you include the Post ID within the_content() field in PHP?

Please bear with me on this question. I understand that it may not be as clear but and suggestions are helpful.
So I created a template page and within that template page I have two tags.
One field is the default:
<?php the_content(); ?>
and the other one is
<?php the_field('test_advertisement_two', 25017); ?>
that I created using ACF (Advanced Custom Fields) which includes the Post id. How can I include the post id in the <?php the_content(); ?> field?
I think I maybe confused how <?php the_content(); ?> works. Is there a way to assign a name to it a name to it?
This is what I am doing:
I am calling the template page (test-template.php) that contains:
<?php the_content(); ?>
<?php the_field('test_advertisement_two', 25017); ?>
from a different location by using
The post content in <?php the_field('test_advertisement_two', 25017); ?> displays but the content from <?php the_content(); ?> does not display. That's why I figure that I either need to include a post Id in <?php the_content(); ?>
The post content in displays but the content from does not display. That's why I figure that I either need to include a post Id in
Essentially.
the_content only functions right within The Loop, one of the most important concepts in WordPress. The the_field call is working because it's being explicitly passed a post ID in the optional second parameter.
You'll need to access it like this:
$page = get_post(25017);
echo $page->post_content;
(or get_page if you're on a page instead of a post)
Hi if you need post content using post id then below is the code for that.
echo get_post_field('post_content', $post_id);

How do I display one (featured) post from Wordpress on my static html website?

We have a Wordpress News Blog on our website (http://www.litepanels.com/news/) that is separate from the rest of the static html website. I just want to take the latest story (the featured post) and display it on our home page. The Blog and our website are on the same server. I found I can grab post titles like this:
<?php
require('../news/wp-blog-header.php');
?>
<?php query_posts('showposts=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?><br />
<?php endwhile;?>
but I do not know PHP well, how do I grab just the featured post (which has a title, image and text as you can see in the link above)
I am testing it here: http://www.litepanels.com/newwebsite/blog_test2.php
If that works for you, it means you've got all you need except for some way to display your information. For this, Wordpress provides some template tags that can be used within the while loop. the_content is the one you need. Others can be found in the Codex.

Wordpress - duplicate search results listed?

The theme used is CookingPress v1.2. - http://bit.ly/zgCtE0
Problem:
If "test-blog-post" has 5 tags and is returned in search results, then it will be listed 5 times in the results list.
Question:
Is there something I can add to the code below to correct the search results from being duplicated? If not, is there any other code that might cause this?
Below is the code that renders the search results (from loop-search.php).
<?php if (!have_posts()) : ?>
// No Results
<?php endif; ?>
<?php while (have_posts()) : the_post(); ?>
// Show Posts
<?php endwhile; ?>
That is not the complete code. You have cut out the important stuff after "//Show Posts". Please post that code and please post the code that queries the database. I ask because I don't think that it is a WordPress native query. That is, I don't think vanilla Wordpress searches for tags at all. That means that this is a custom query or the developer has hooked into the WordPress query and altered it. Your question is not very answerable without that information.

Categories