Archived topic
how to create related posts with generateblocks?
27 replies · Started by eran on July 17, 2022
Show me a screenshot ?
and it doesnt show on all pages
So the options available.
So whats not working ? The Parent - > Post Parent option only works on Pages ( or post types that have hierarchy support ) and those pages must be parents with child pages.
I have this pages in my menu but it doesnt work on them
Hi there,
Can you make sure the parent page is set correctly?
https://wordpress.com/support/pages/page-options/#parent-page
The URL now changed and became longer.
it can affect SEO right?
I should do it or find another way to use the related posts sidebar?
it can affect SEO right?
I'm not an expert on SEO, you might need to do some google research on that.
I should do it or find another way to use the related posts sidebar?
It depends on you :)
It still don't work for me.
If im in "camera page" i want the related posts to show all the pages that related to camera (all the pages under "camera page" in the menu).
and If im in a "camera for XXX page" (page under "camera page" in the menu) I want the related posts to show all the other pages realted to camera plus the "camera page".
can i achieve this result?
1. You need to understand the menu structure doesn't really build any relationship between pages, you'll need to use the method introduced from this link: https://wordpress.com/support/pages/page-options/#parent-page
2. Make sure the pages you are talking about are pages, not posts, as there's no parent/children posts relationship in WP.
3. There's no easy way to do this, you'll need to create multiple block elements. For example:
1). Create a block element - hookfor the camera page for XXX pages (children), insert 2 query loop blocks:
The 1st query loop: add the parameter Parent, choose the camera page (parent page) for the 1st Query loop block.
https://www.screencast.com/t/vQ2qM3YPI
The 2nd query loop: add include posts parameter, add the camera page(parent)page:
https://www.screencast.com/t/ruuFMugK3J
2). Selec the hook as element type, select after_do_template_part as the hook name, choose all the child pages under camera page.
3). To exclude the current page, add no-current-post class to the Grid block under the 1st query loop block:(If you have GB pro, you don't need to do step 3 and step 4, you can directly enable the exclude current post option)
https://www.screencast.com/t/HkWhjs0iub
4). Add this PHP snippet:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'no-current-post' ) !== false ) {
$query_args['post__not_in'] = [get_the_ID()];
}
return $query_args;
}, 10, 2 );
4). Create another block element - hook for the Camera page (parent), insert 1 query loop block, the setting would be the same as the 1st one in child pages, except you don't need to addno-current-postclass to this one.
https://www.screencast.com/t/7uHNLUtNkjx4
There's no way to do it without parent page?
To do it without Page Hierarchy ( Parent / Child pages ) or Post Taxonomies would require Custom development.
When you create a sub-menu the relationship is formed in the Menu post type. Not between the pages.
To to do that you would need to:
a. get the current page menu item.
b. loop through the child pages of its submenu and get the individual post IDs
c. stores those post IDs in an Array
d. create a custom query parameter to display ONLY those post IDs.
But you would need to custom develop that.
I dont know how make it that way.
I just don't want to create parent page since the child pages url will be longer and hurt my seo.
But i guess this is the only way no? Or i can manually create related post for every page?
If the relatioship is hierarchical then the above is the only way.
If they were Posts you could related them by Category.
Or yes, you can simply add a Query Loop to each page and set which posts you want displayed in each query loop