i was wondering if you need or can make post templates for search results page only on wordpress. I'm developing my own child theme using underscores.me framework on localhost and have come across an issue which has lead me to ask this question. T
he issue itself is that the post content for post's (for both standard and custom post types) gets changed on the search results page to the url of the post permalink -
e.g. if i search for post titled movie3 it will return a result on the search results page, but the post movie3 will have the post body removed, including the excerpt and read more etc and instead have the title and date followed by localhost/?movies=movie3 instead of the excerpt or post body (I'm using optional excerpts), followed by tags etc.
This is the first time I'm developing with wordpress and was also wondering if a plug in like relevanssi would alleviate my issues, especially since I've read that wordpress search is supposed to be terrible?
If you are building your own theme I would not suggest using a plugin for this. The underscores theme should have an search.php page included in the wp-content/themes/yourtheme directory. This is the default template for displaying search results.
The default search.php template for underscore should have "the loop" included:
<?php while ( have_posts() ) : the_post(); ?>
You can call WordPress functions such as the_excerpt(); inside "the loop" to get the excerpt of the returned post. See http://codex.wordpress.org/Function_Reference/the_excerpt for more information on this.
Related
I have a Wordpress site with three custom taxonomies. They're configured and working, with some posts marked with each, and want to run code related to them. As per the documentation, if the custom taxonomies are cats, dogs and trees, and I want to run php about cats, after lots of trying and going up and down over the documentation, I think I should create a template and call it "taxonomy-cats.php":
taxonomy-{taxonomy}.php: For example, if the taxonomy is named “sometax,” WordPress would look for a file named taxonomy-sometax.php
But I still don't get something: the docs say "wordpres would look for". Why would WP look for it and not other? I suppose I at least have to tell it what or when to search for it. How or where do I tell WP to look for it?
<?php /* Template Name: Taxonomy cats */
get_header(); ?>
<h1>Hello world</h1>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
As a template, I've found I can add it in the administration as an attribute to a post, but then when I enter that page if I try to get the id of the taxonomy, I get the id of the post.
Wordpress uses a loop to display posts. Wherever you are, most of time, the loop is being used.
The Loop is PHP code used by WordPress to display posts. Using The
Loop, WordPress processes each post to be displayed on the current
page, and formats it according to how it matches specified criteria
within The Loop tags. Any HTML or PHP code in the Loop will be
processed on each post.
Source # https://codex.wordpress.org/The_Loop
Wordpress is also based on a template hierarchy system. With the exception of the basic index.php template file, you can choose whether you want to implement a particular template file or not.
WordPress uses the query string to decide which template or set of
templates should be used to display the page. The query string is
information that is contained in the link to each part of your
website.
You can take a look at this image to have a visual understanding of the Wordpress templates hierarchy.
Source # https://developer.wordpress.org/themes/basics/template-hierarchy/
In your case when displaying the dogs taxonomy, Wordpress look for the following files in that order: taxonomy-pets-dogs.phpif this template doesn't exist it move on to look for pets taxonomy-pets.php, if it doesn't exist it moves on to taxonomy.php... etc
(Where pets is the name of your taxonomy and dogs is a term inside that taxonomy)
Specifying taxonomy-pets-dogs.php enables you to create a custom template for that specific term. cats wont have the same template as dogs exept if both are using taxonomy.php or taxonomy-pets.php and if no specific templates exist.
But in any case taxonomy-{taxonomy}.php or taxonomy-{taxonomy}-{term}.php will always use the loop to display posts and terms. Like any other template.
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();
echo the_title().'<br/>';
endwhile; else :
echo 'No posts found!';
endif; ?>
I need to add my own custom PHP script to all Wordpress pages of my blog.
I am not referring to adding PHP onto a single page (which can be done with a plugin).
Essentially, this is what I need to do:
Add a snippet of code directly to Wordpress script that handles
display of posts.
The snippet needs to be able to grab a ID of the post.
Can someone show me the best way to do this?
This is what I need to figure out:
Which of the Wordpress php files handles the display of the Wordpress
posts (is it post-template.php by any chance?)
How to grab the Post ID using PHP? I found the page which says this could be the possible way, is that correct way of getting the Post ID?
$id = get_the_ID();
for single post, it single.php and to get post ID
$post = get_post();
$id = $post->ID;
It depends on the theme. Very simple themes might use index.php for all pages, others use single.php for the display of single posts, and there are a lot more possibiites, also secondary templates (template parts) which are included in the primary templates and contain part of the post. Have a look in the theme folder.
Inside all of these, the posts are always inside "the loop". This page has the explanation and some useful examples: https://codex.wordpress.org/the_loop
HTH
I want to show the full post with comments and author box on the home page just as if the actual link to the post was clicked. I will set the canonical on the actual post to point to the home page for seo purposes.
I've seen some code out there, but so far nothing that matches my exact needs.
I only want to display 1 WordPress post on the homepage with the comments and author box below the post. And I want to be able to specify which post will be the one that gets displayed on the homepage. (which post id#)
To add to this, I also need the rel="author" to be visible in the header section. I'm using WordPress SEO by yoast and have selected to not show rel="author" for pages. And all code out there I've tried that shows a single post on the home page treats it as if it is a page instead of a post, therefore removing the rel="author" from the head area.
Please do not reply with how to set the number of posts in the Reading tab or how to set a static page. I already know that. That won't work for my needs.
In summary, when I go to my home page I will see the article I wrote along with the comments and author box below it and rel="author" in the header.
Your best bet would be to set your homepage as a static page, and build a custom template for that page. In the editor, create a custom field that allows your to select a blog post to feature (look into Advanced Custom Fields plugin for easy custom fields), grab that custom field value, and use that to build either a WP_Query, or just call the various bits of information using the ID of that post. The trickiest part is getting the_content() by ID. Here's how you can do that:
Add this function to the functions.php file:
function get_the_content_by_id($post_id) {
$page_data = get_page($post_id);
if ($page_data) {
return $page_data->post_content;
}else{
return false;
}
}
Call your function in your page template like this:
echo get_the_content_by_id([INSERT YOUR DESIRED POST ID HERE]);
If you make a php file in your Theme directory called front-page.php, that will automatically appear as the template for your static home page.
Now as far as the rel="author" thing, I don't know anything about that, so I unfortunately cannot help with that.
Also, I'm unsure why my code is losing it's formatting here.
I am trying to add the custom field "market" into the a href as links, which is working great on pages, however is not working on my home page.
The PHP is identical for both.
For the home page, the php is in the functions.php file, and for the theme-by-series page the PHP is in the page's .php file.
The page works, so it doesn't seem to be anything wrong with the PHP, seems more like it for some reason is not targeting the post id as it should.
Anyone know how to solve this?
Custom fields are saved per post/page, and this is likely why it's not loading on your index.php page. You need to replace $post->ID with the ID of the post/page you want to show your custom field for - the $post object is available on the page.php and single.php templates, which is why it works there. On the index page, it would only be available inside the loop.
Look up the WP Docs on how to Reset the main WP Loop Query:
http://codex.wordpress.org/Function_Reference/wp_reset_query
If you're getting different results in functions.php vs a page template, there's a good chance its because your page template has already started a query loop and its conflicting somehow. This is just off the cuff without looking at the code. I'm going by your description of the difference betweeen working and non-working models.
I have created a wordpress.org blog on my website but I do not use the actual blog page to display the blog posts. Initially I tried to style the wordpress blog to match my site, but since wordpress' theme is too constricted I decided to use "the loop" to gather the_author_posts_link, the_title, the_content, etc and display them on my main page. (reference: http://www.corvidworks.com/articles/wordpress-content-on-other-pages). The problem I am running into is the comment section. No matter what, I cannot seem to find a way to add comments to my front page using php. I know I can get my post content to display by calling the_content(), but I cannot seem to find a similar function for comments. I'm open to any solution that will allow my pseudo-blog to display comments under each post.
Thank you!
Should be <?php comments_template(); ?>. Look in your theme files for that template tag. But it working depends on if you've correctly included the blog header on your non-WP pages.