Show Wordpress posts in more than one page - php

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.

Related

How can I set my wordpress post listing to show excerpt from the post content?

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.

How to Insert PHP Code in all WordPress Posts

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

Wordpress Loop does not work properly in category.php when using query_posts

I am developing a wordpress theme. I am new in this field, and it's not very easy.
I wrote the code for the loop, and everything is working perfectly. I am working on category.php page. Without the query posts, the category is correctly showing the posts from that specific category.
But, I want to add pagination, and limit the posts per page to maybe 10. But, when I add this code before the loop:
<?php query_posts( 'posts_per_page=10' ); ?>
It doesn't work. Now, it outputs all the posts, from every category in the website, not only from that category.
Can anybody tell me what am I doing wrong?
Thanks.
http://codex.wordpress.org/Function_Reference/query_posts
query_posts( 'cat=3&year=2004' );
You need to add the category to the query_posts; otherwise wordpress won't know you need a category in your custom query.
What you'll need to do is set how many posts you want to display on the page via Settings -> Reading and WP should take care of the pagination.
If you theme framework doesn't have a pagination function, I would recommend something like:
http://wordpress.org/plugins/wp-pagenavi/
http://wordpress.org/plugins/wp-visualpagination/
Those give you a simple PHP code to put into your theme wherever you want the pagination to show up. The code will have to be put between the endwhile; and endif; inside the loop.
You'll need to dynamically pass the current category's ID into the query:
query_posts('posts_per_page=10&cat='.get_query_var('cat'));

WordPress loop to include pages content instead of posts

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().

Wordpress single page theme - how to get the ids of the pages from the menu

I have one problem, I have a one-page wordpress theme and I'd like to implement WP Menus inside it. I created a custom walker so I could create links like #post-slug or #page-slug instead of redirecting the user to another page, but the issue comes when I need to show the posts from the menu on the homepage.
Obviously I can't show both pages and posts in one query so the only solution would be to create two queries, but still, how can I get the ids of the posts or pages that were added in Appearance > Menus? I need just the pages or the posts and not custom links or categories/etc.
Any help is greatly appreciated.
Try this
<?php the_ID();
Reference: http://codex.wordpress.org/Template_Tags/the_ID
This function displays the ID of the post, to return the ID use get_the_ID().
EDIT
I'm fairly sure you need the wp_get_nav_menu_items( $menu, $args ); function.
Reference: http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items

Categories