How to Insert PHP Code in all WordPress Posts - php

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

Related

how to replace excerpt with full posts on WP

First thing first, i'm new with WP and i'm also not very good with PHP.
anyway, i use this plugin http://wordpress.org/plugins/posts-in-page/ to group all the posts that i create inside a specific page and it works good.
Now, i'd like to change the code so that all my posts displayed on the page are not excerpt but the full posts.
I need a full explanation on how to do that cause as i said above i'm new both in wordpress and PHP.
thanks a lot.
There are two ways of doing this. One is to go to the Reading Settings in the Administration interface and select your desired option under For each article in a feed, show….
The other way is to hardcode this in the theme itself. Open the template file (index.php for example) and replace the_content or the_excerpt with the_content.

How do I show only one specific post with comments and author box on the homepage in Wordpress?

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.

Search results page post templates? [wordpress]

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.

Make special pages from WordPress theme?

I'm trying to figure out how a certain WordPress sets things up. I'd like to have a special page where I could make WP calls and interact with the theme, without affecting anything else.
I just making test.php and putting it into my theme's folder, but that doesn't work.
#Eliran provides one possible option, but you could also add a page in the back-end of WP, just make sure it has the slug 'test', and change your 'test.php' filename to 'page-test.php'. If you're worried about the public seeing this, set the page visibility in the admin to 'private'.
Edit:
to move your understanding along a little further also, you should review the way that WordPress determines what file to grab to render a particular URL. This can be pretty confusing to start with, so be patient if you're not familiar with it, but it's at the heart of designing WP themes. I'll link to the examples, and if you scroll down a little there's a diagram that, along with the text, will help you see how WP is 'thinking'.
http://codex.wordpress.org/Template_Hierarchy#Examples
You can see here: Page Templates
all you need to do is create a page named page-{custom-name}.php and add it to the theme folder.
and inside this php file add:
/*
Template Name: My Custom Page
*/
and than to use this page you need to go to the wp-admin, add/edit a page and chose it:
inside the php file everything you do is classic wordpress.
all this is giving you is a custom page tamplate.
Put it in your root folder. When you go to look at it, you'd look at www.mywebsite.com/test.php
It may be other ways to do this, but I rather use the rewrite API and custom query vars, to create custom routes.
A previous answer on the subject can be found here
The basic idea is to add a new url rule, catch the query var with the parse_request filter and maybe do a die or redirect to prevent the default wordpress template from loading.
I prefer this over theme templates, because with templates you need to create a page for each new url, and if that page gets acidentally deleted, that functionality would stop working.
What Pages are Not:
Pages are not Posts, nor are they excerpted from larger works of fiction. They do not cycle through your blog's main page. WordPress Plugins are available to change the defaults if necessary.
Pages cannot be associated with Categories and cannot be assigned Tags. The organizational structure for Pages comes only from their hierarchical interrelationships, and not from Tags or Categories.
Pages are not files. They are stored in your database just like Posts are.
Although you can put Template Tags and PHP code into a Page Template file, you cannot put these into the Page or Post content without a WordPress Plugin like Exec-PHP which Read overwrites the code filtering process.
Pages are not included in your site's feed.
Pages and Posts may attract attention in different ways from humans or search engines.
Pages (or a specific post) can be set as a static front page if desired with a separate Page set for the latest blog posts, typically named "blog."
More About Pages.
In WordPress to add a new page you have to log in to the admin/backend and from the pages menu you can add a new page. In this case, you can select templaes for your page and also you can create a custom page template for that page.
You may read Createing a new page in WordPress. and custom Page template in WordPress.

WordPress - Cannot get custom fields to show on home page

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.

Categories