I’m a beginner with wordpress development and would like to have a suggestion about the more correct way to solve a couple of problems:
1) I have a blog with posts and pages and although the posts will appear in the home page they are often related to some of the other pages of the blog, so i want also them to appear in a news box at the end of those pages. How is the best way to accomplish this? please be detailed!
2) I want to create a page that display more pages divided by category, displaying category name e the pages inside. Any suggestion? again please don’t leave details unspoken 🙂
Thanks a lot!
Well, lets actually answer the question since it is so straightforward.
the wordpress codex you need is probably here:
https://codex.wordpress.org/Template_Tags/get_posts
you want $posts=get_posts(array(‘category’=>’xxx’)); with the ‘xxx’ the main category of the page you are on.
If you have multiple categories, you can either do multiple queries and merge them somehow (and look for duplicates by using the postID, for instance) or you can just go with the first… A lot depends on the content and the user base.
Once you have the results, you can just loop through them. Do take care that this is NOT the same as the main loop, because that depends on the global post variables.
If you know enough PHP to have gotten this far, pulling the values from the results array should be very easy:
e.g.
echo $posts[post_number][‘post_title’];