Display list of posts on each post page - wordpress

I would like my blog post pages (single.php) to include the normal blog page (index.php) below each post. Basically when you're in the archive and click on a blog post, it should appear as if the post simply gets inserted above all the posts:
index.php
1. Blog post 1
2. Blog post 2
3. Blog post 3
single.php
title
content
1. Blog post 1
2. Blog post 2
3. Blog post 3
I'm aware I can do my own get_posts() call and simply foreach all the posts, but seeing as I've already got my index.php styled and working fine, and I want that exact same template below every single post, it would be much easier for me if there was some sort of way to replicate "the loop" used on the archive page. I would also need it to work with the normal pagination (though it would point back to the archive).
<iframe>ing the index.php file would actually be exactly what I want - only you know, I obviously don't want to do that :P
Any ideas?

Unfortunately it isn't possible to do it the way you're thinking, because de WP main query is very different in these two cases (index and single). However you can use get_template_part function, that works just like an include, and put inside it the query that you want to replicate. Then you can call it in single.php, index.php, archive.php and so on.
It would be something like:
get_template_part('loop');
And inside loop.php you call a WP_Query with whatever you want.
Hope it helps!

Related

How to access an ACF field on a category from within pre_get_posts in functions.php to modify archive query

Long title, but what I'm trying to accomplish is
1) Adding a Post Object field to the Add/Edit Category page to specify a single post to use as a featured/sticky post for that category. As far as I can tell there's no way to do this (featured category post). Have used https://wordpress.org/plugins/category-sticky-post/ in the past but seems dead and starting to act buggy.
2) Display that post on top of the respective archive page 1, and then start with the normal loop for that category. This is working perfectly.
3) Where I'm stuck is how to exclude that featured post from the query so we don't get duplicates. I'm trying to do this in a pre_get_posts function in functions.php to modify the query so it functions like a normal archive page otherwise.
If you know of an easier way to simply set a featured article for a category I'm open to suggestions. I know sticky for main homepage loop but nothing for archive pages.
You may need more info but that should be a good starting point. Using ACF Pro (latest)
Thanks for the help!
You can use the ignore_sticky_posts property while querying posts using WP_Query to exclude the sticky/featured posts.
Reference: WP_Query

Wordpress posts with Spotify <iframe> in excerpt/description

I need to display the spotify preview along with the post thumbnail on the archive page.
It doesn't necessarily have to embedded on the Excerpt, but I'm wondering how is it possible to create a iframe per post which is easy to mantain (using a simple posting system)
2 things I can think about - 1st you can actually create your iFrame inside the archive.php and the excerpt field to be just the url, so inside the file you can have something like:
//the loop
...//some content
<iframe src="<?=get_the_excerpt()?>"></iframe>
...//more content
//end loop
2nd thing I can think about is too use some pluging to add extra fields to your posts which you can call. I personally like CMB2 they have a really good documentation and all sorts of custom fields you can add to your posts.

Trying to understand how to add a separate blog to a word press site

This may seem more like informational question rather than a coding question but I may be wrong.
Let's say I have a site, www.xyz.com. This site is built in wordpress from a custom template. (That I will put together).
The site will showcase products with each product being input through individual posts. SO techincally speaking, It's already using the "blog" feature of wordpress.
Now I want to add a blog into my site. Let's say www.xyz.com/blog/
I could create a custom page in my theme folder called
page-blog.php
in which that would work fine.
But here is were im stumped. I want to show blog posts here that are not found on the original xyz.com page. If I use the wordpress loop on the blog page, it will show all my posts that are on the xyz page. I want to show separate posts on my blog page then the ones I show on my main page.
So my main page would have lets say, a 1000 posts of products, each product being a different post.
For my blog, I want to run the loop but not show the products posts, but show regular "blog" posts, not products. Like information, etc, etc...
I'm stumped on how I can run the loop on the blog page and have it pull in different posts that are not on my main page. Would I have to install a separate wordpress installation? I believe this would be a major hit on site performance, and also caching would be more difficult i think.
So you have 'products' and you have 'posts'. Two different post types. See: https://wordpress.org/plugins/custom-post-type-ui/
Create a new Post Type for products.
Enhance your WordPress theme so it displays products differently. Note that some themes come equipped to handle custom post types automatically, while others may require you to make some minor adjustments or add additional template files.
Refer to the article at WordPress regarding Template Hierarchy.
See also: The Complete Guide to Custom Post Types
I understood your concern and I can say no need for extra WordPress setup and it is easily achived by creating category for example you can create category blog or product.
Now let's start with main site , if you want to show all post then nothing to do on that.
Secondly on blog you just need to get post having blog category, and this can be achieved easily by wp_query if you are writing your custom loop otherwise best way to use hook. You can use pre get post action as below,
function exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-1,-1347' ); } }
add_action( 'pre_get_posts', 'exclude_category' );

WordPress Recent Post should be 5 with continue reading link

I set my front page as recent posts. But It display all the posts at front page.
I need to display only 5 recent posts at first page.
I like to have that "Continue reading" after 10 lines of the each post content.
Q1 - Controlling Amount Of Posts Displayed
Dashboard -> Settings -> Reading -> Blog pages show at most -> change to 5 -> Save Changes -> test
If that didn't work you may be using a custom theme which controls posts per page with a function. Check functions.php for something like if ( $query->is_front_page() and in that if statement may be something similar to $query->set( 'posts_per_page', '10' ); You will want to change 10 to 5.
Q2 - Controlling Post Excerpt Length
The easiest way if one has no experience working with wordpress template files is to add the <!--more--> tag in your post editor and wordpress will cut the post at exactly that spot. Here's some info about using it.
You will see on the above linked page the option to customize it with a function. It requires messing around with the template files a little. Leave a comment if you want to know more about that and I can try to help you with a better explanation.

Limit wordpress posts loop to continue onto another page

Theres a site i did that on the home page, i created kind of like a news box type
of thing which is what the client wanted...
the posts bit im using to limit the posts on the index page is
query_posts('posts_per_page=4');
Now, this limits my posts to "4" or whatever number i want etc, when the user clicks on the
"news" page button, it takes them to a page which has the full posts loop without the
query_posts('posts_per_page=4'); which essentially shows them all the posts.
Now the problem is that, this person is posting A LOT now and the page now has a bunch of posts one after the other.
Is there a way to limit these to any number (that i choose) and show the rest of the posts on another page???
So essentially something that says
filter out 20 messages/excerpts for example, BUT after 20 show a "next" page or something like that?
is that possible? Because as it stands now, by years end this ONE page will have hundreds of posts.
ive looked around but not sure exactly what im looking for. This one came close (here on SO)
Wordpress loop show limit posts
And although its similar, i need a way to limit the posts on the page AND continue them on another page so that one page doesnt hold ALL the posts.
Thanks in advanced.
The homepage is controlled by index.php or a template file used by a page which you can set up to be your homepage at Settings > Reading.
You must check your index.php file or the template file used the page from the homepage.
If your queries in the code are not including any "posts per page" arguments, you can control this numbers from Settings > Reading.
There is also a plugin which can help you:
http://wordpress.org/extend/plugins/custom-post-limits/
Now, clicking on news page, you must check if that is a page or a category listing?
If it's a page, then it is handled by page.php or an attached/used template for it. Again, the code is important, how the queries are written. It it's a category listing, then the category.php is handling that page.
Pay attention for the template files are using each every page/post and learn this: http://codex.wordpress.org/Class_Reference/WP_Query
Good luck! :)
http://codex.wordpress.org/Settings_Reading_Screen
In the settings you can set a default max limit of how many posts per page.

Resources