-
AuthorPosts
-
September 6, 2021 at 12:36 am #1319810
Hello,
I have read the thread https://kriesi.at/support/topic/sticky-posts-in-magazine-element/
and copied the snippet in functions.php, but I don’t understand how to use it.
I have several magazine elements and each one should have its specific sticky post. How can I set it?
Also, that code breaks the visualizations of the magazine elements: some magazines were displaying their content and some other just the title
Thank you
MauroSeptember 9, 2021 at 2:44 am #1320151Hey profumopuntoit,
Thank you for the inquiry.
Are you going to add magazine elements on different pages? If that is the case, you can replace the function that retrieves the sticky post with the get_post function and use the is_page conditional function to check for the current page. Look for this line..
$sticky = get_option( 'sticky_posts' );
.., and replace with:
$sticky = get_option( 'sticky_posts' ); if(is_page(11)) { $sticky = get_post(123); }
The code above will check if the page has the ID 11 and assign the post with the ID 123 as sticky post.
Best regards,
IsmaelSeptember 9, 2021 at 11:29 pm #1320308Thank you
but the original code in thread https://kriesi.at/support/topic/sticky-posts-in-magazine-element/ seems that it is not working any more. Please see screenshot.
I tried it also in a blank functions.php with only this code
Also, what if in one page there are more than one magazine? Please see our page in private content
Thank you
MauroSeptember 13, 2021 at 1:56 am #1320611Hi,
Thank you for the info.
We may have to access the site and test the modification directly. Please provide the WP and FTP login details in the private field.
Best regards,
IsmaelSeptember 13, 2021 at 11:12 am #1320699Please see private content
Thank you
MauroSeptember 14, 2021 at 7:20 am #1320847Hi,
Thank you for the info.
We adjusted the filter in the functions.php file a bit. The post items from the selected category are now displaying properly in the magazine element with the selected or sticky post above.
This is the updated code.
/* https://kriesi.at/support/topic/sticky-posts-in-magazine-element/ */ add_filter('avf_magazine_entries_query', 'avf_magazine_entries_query_sticky', 10, 2); function avf_magazine_entries_query_sticky($query, $params) { $include = array(); $sticky = get_option( 'sticky_posts' ); if(is_single(10137)) { $sticky = array(get_post(4649)->ID); } $query['post__not_in'] = $sticky; $posts = get_posts( $query ); foreach($posts as $post) { $include[] = $post->ID; } $include = array_merge($sticky, $include); $query['post__in'] = $include; $query['orderby'] = 'post__in'; // sort items based on the post__in value return $query; }
We replace is_page with the is_single function because the magazine element is inside a post.
if(is_single(10137)) { $sticky = array(get_post(4649)->ID); }
We also placed the post with the ID 4649 inside an array. If you need to display a different sticky post on a different page, just duplicate the if statement above and replace the post IDs.
if(is_single(113)) { $sticky = array(get_post(235)->ID); }
The changes above will display the post with the ID 235 as a sticky item inside a post with the ID 113.
Best regards,
IsmaelSeptember 18, 2021 at 1:53 am #1321416Thank you Ismael,
the code is working correctly in the simple test page,
but what about pages like the other real ones (Please see links in Private Content) where there are more than one Magazine elements in the page?
Mauro- This reply was modified 3 years, 2 months ago by profumopuntoit.
September 20, 2021 at 8:54 am #1321581Hi,
If there are multiple magazine elements in a single page, we could check for the class attribute value or names of each magazine element and assign a sticky post accordingly. Example:
/* https://kriesi.at/support/topic/sticky-posts-in-magazine-element/ */ function avf_magazine_entries_query_sticky($query, $params) { $include = array(); $sticky = get_option( 'sticky_posts' ); if(is_single(10137)) { // first magazine element if(true == strpos($params["class"], "avia-builder-el-0")) { $sticky_id = 4649; } // second magazine element if(true == strpos($params["class"], "avia-builder-el-1")) { $sticky_id = 2301; } // third magazine element if(true == strpos($params["class"], "avia-builder-el-2")) { $sticky_id = 1236; } $sticky = array(get_post($sticky_id)->ID); } $query['post__not_in'] = $sticky; $posts = get_posts( $query ); foreach($posts as $post) { $include[] = $post->ID; } $include = array_merge($sticky, $include); $query['post__in'] = $include; $query['orderby'] = 'post__in'; // sort items based on the post__in value return $query; } add_filter('avf_magazine_entries_query', 'avf_magazine_entries_query_sticky', 10, 2);
This part of the code checks for the class name of the first, second and third magazine element and return the corresponding sticky post.
// first magazine element if(true == strpos($params["class"], "avia-builder-el-0")) { $sticky_id = 4649; } // second magazine element if(true == strpos($params["class"], "avia-builder-el-1")) { $sticky_id = 2301; } // third magazine element if(true == strpos($params["class"], "avia-builder-el-2")) { $sticky_id = 1236; }
You can combine these conditions with the is_page function to display a specific sticky post for different magazine element within a specific page.
Best regards,
Ismael -
AuthorPosts
- You must be logged in to reply to this topic.