Site logo

Archived topic

How to show Recent Posts and Recent Pages on homepage?

13 replies · Started by Vl on December 16, 2020

Viewing posts 1–14 of 14

Hello!
On my site, I use both posts and pages to add new content. But the homepage only shows Recent Posts.
What code should I add to show Recent Posts and Recent Pages on the homepage?
Thank you.

Thanks for the answer!
As I understand it, the plugin can display posts or pages. But I need to show posts and pages together sorted by date.
Is it possible to do this without a plugin?

Hi,

As I understand it, the plugin can display posts or pages. But I need to show posts and pages together sorted by date.

To clarify: You mean both the post and pages are within the same list? or do you mean they are separated into 2 list within a page? Let us know.

Is it possible to do this without a plugin?

It is possible by writing your on wp_query but it'll be waaaay more tedious to do than simply inserting a shortcode from a plugin.

Yes, I need both the posts and pages are within the same list.

Sorry, I don’t know PHP.
I have changed line 383 in the “wp-show-posts.php” file accoding this instraction: https://github.com/tomusborne/wp-show-posts/commit/286caf1164db8b6b6f38b85d3a011b519a27f4de

We highly discourage editing of plugin files. If you must change something in how the plugin works, use filters instead.

But where should I paste this code:
add_filter( ‘wp_show_posts_shortcode_args’, function( $args, $settings ) {
if ( 123 === $settings[‘list_id’] ) {
$args[‘post_type’] = array( ‘post’, ‘another-type’, ‘one-more’ );
}

return $args;
}, 10, 2 );

for this particular snippet, you'll have to change 123 to the ID of your WPSP list. You'll also have to change/add/remove things on this line $args[‘post_type’] = array( ‘post’, ‘another-type’, ‘one-more’ ); to only $args['post_type'] = array( 'post', 'page' ); since these are the only ones you want to include on the list.

Note: Be mindful of and within your code. These will cause issues. Use ' instead.

As for how to add PHP snippets to your site, check this brief documentation:
https://docs.generatepress.com/article/adding-php/

Thanks, it works!

If you must change something in how the plugin works, use filters instead.

Can you explain how to use filters?

Filters are basically snippets of code that change the output of core functions in the theme without having to actually edit the theme itself (which is never a good idea).

Sorry I do not understand.
You do not recommend editing the "wp-show-posts.php" file, but how can I replace this code:

$args[‘post_type’] = array( ‘post’, ‘another-type’, ‘one-more’ );
$args['post_type'] = array( 'post', 'page' );

Do I need to use filters? But how to do that?

Thank you very much!

No problem :)

This archived topic is closed to new replies.