I am developing a one page WordPress theme, there is no need to display any posts, it's just informative page, I was thinking to use static content, but it turned out that I'll need to use dynamic one in order for things like search to work. In "pages" section of WordPres I created a new page which contains all the content I need, I was trying to figure out a loop to include contents of that page, but failed. After doing a research I was only able to find loops used to display content of posts. So is there same loop, but to display contents of page? Also How would I than include those contents in a page e.g. <?php something();?>
Just specify the post_type inside of the query_posts() parameter. For example, if you wanted to output the content of each page, you could do this:
query_posts(array('post_type' => 'page'));
while(have_posts())
{
echo the_content();
}
For reference, you might want to check out the API Reference Docs for query_posts().
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'm using WordPress 4.8.1 and have some familiarity with how WP works. I have a page that lists all my posts and it shows Title and Date for the posts in a listing, then the user can click on a post title to go to the detail page showing that posts content.
Question I have is how can I modify the PHP page that displays my posts ( ) so that under the TITLE for the post it shows a brief bit of the posts content?
I've read a couple of similar questions on StackOverflow but nothing seems to have the answer.
I can see that the page that displays my posts appears to be page.php and the template parts it uses are /frameworks/header/header-v1.php and /frameworks/template/blog/content-page.php
None of my attempts to make edits to any of these pages show up though. I tried adding a class to what I thought was the line that renders the link to the article (the hyperlinked title) but no luck. Thanks for any light anyone can shed on this.
Use the_excerpt() function in your template file to display the excerpt in posts loop.
The page.php is the template that shows the individual page, so this is not the correct file. If you are referring to the home page which lists the all of your posts, then you need to edit index.php file.
inside it, locate the_title() which renders the title and after that, add the the_excerpt() function:
<?php the_excerpt() ;?>
You can also use <!--more--> quicktag in posts itself to specify, where is the cutoff point. In this case, use the_content() function instead:
<?php the_content(); ?>
Just be aware that in this case if the post doesn't have the <!--more--> quicktag, it will display the whole post.
I'm new to Wordpress and creating a Wordpress template from scratch. I have been grinding my gears for a while and can't find a solution for the following problem.
In my front page, among with some other static content, I want to show a preview of my blog posts in a sidebar like layout. I wrote the following function in my functions.php file:
function show_forum_posts($preview = false) {
if (have_posts()) : while (have_posts()) : the_post();
get_template_part($preview ? 'forum-preview-content' : 'forum-content', get_post_format());
endwhile; endif;
}
This works as expected and I can see the posts by calling this function in my index.php
The problem is that I want to have another page where I want to list all the complete blog posts.
From what I have been reading Wordpress Pages only allow static content.
After banging my head on the wall for quite a few time I created and gave all the posts a category called "Forum", and then created the category-forum.php file, in which I used the function above and successfully listed all the posts (I also tried with Pages but didn't worked out).
The problem is the link to this new page is mywebsite.com/category/forum/ and I want it to be mywebsite.com/forum/.
Although I made it work it got me thinking that this probably is not the best solution, but it was the only one I could came up with.
Any ideas on how to accomplish what I'm looking for?
Thanks in advance!
WP_Query function is what you might want to use in this case.
You can pass the category parameter to your query and loop through it. You could also use 'posts_per_page' value to limit the number of posts you want to display on a page. Hope that helps.
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 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.