Tagged: blog posts
-
AuthorPosts
-
July 21, 2017 at 1:49 pm #825116
Hello,
I’m using the content element “Blog Posts” and I’d like to filter blog posts according to the year they’ve been posted. The idea is to have a page displaying the posts published within the year and a page “archives” displaying all the posts from previous years.
I’d like it to be automatic so I do not have anything to do each year. I tried to play with the file blog.php (in avia-shortcodes) so I could choose from there to display posts from a custom taxonomy and then pick the years I want. But I can’t figure it out.
For the moment, my posts have a category “2017” or “2016” and they are selected based on that. But it isn’t the best solution (it means the authors have to remember to add the category for each post they write).
Could you help me with this?
Thank you!
July 23, 2017 at 4:18 am #828557July 25, 2017 at 6:10 pm #829730Hi John,
Thank you for your reply. Actually, I wanted to make sure the changes had to be done in the blog.php file.
I am a web developer, just not familiar with your theme and the way it works. I have paid for extra support to contact you so could you help me with this?
Thank you!
July 29, 2017 at 1:07 pm #831469Hi,
Thank you for using our theme.
You have to do the changes in that file (enfold\config-templatebuilder\avia-shortcodes\blog.php), if you are using the ALB element ‘Blog Posts’.
Check function query_entries to adopt your query.
Hope, this will help you.
Best regards,
GünterAugust 8, 2017 at 3:41 pm #835885Hi Günter,
Thank you for your reply. Once the changes have been done, where should I place the file knowing that I use a child-theme?
Should it be wp-content/theme/child-theme/blog.php,
wp-content/theme/child-theme/avia-shortcodes/blog.php
or child-theme\config-templatebuilder\avia-shortcodes\blog.php ?Thank you!
Best regards,
Anne-Laure
August 8, 2017 at 4:14 pm #835901Hey!
Please refer to this post – http://kriesi.at/documentation/enfold/add-new-or-replace-advanced-layout-builder-elements-from-child-theme/ :)
Cheers!
YigitAugust 8, 2017 at 5:47 pm #835943Hi Yigit,
Thanks! It helped a lot!
I wrote the code and it is partly working but I guess there is something missing (the first part works fine but it doesn’t display the year below once the option is chosen). Could you tell me where I’m wrong?function popup_elements() { $this->elements = array( array( "name" => __("Do you want to display blog posts?", 'avia_framework' ), "desc" => __("Do you want to display blog posts or entries from a custom taxonomy?", 'avia_framework' ), "id" => "blog_type", "type" => "select", "std" => "posts", "subtype" => array( __('Display blog posts', 'avia_framework') =>'posts', __('Display entries from a custom taxonomy', 'avia_framework') =>'taxonomy', __('Display entries from a custom date', 'avia_framework') => 'year')), array( "name" => __("Which categories should be used for the blog?", 'avia_framework' ), "desc" => __("You can select multiple categories here. The Page will then show posts from only those categories.", 'avia_framework' ), "id" => "categories", "type" => "select", "multiple" => 6, "required" => array('blog_type', 'equals', 'posts'), "subtype" => "cat"), array( "name" => __("Which Entries?", 'avia_framework' ), "desc" => __("Select which entries should be displayed by selecting a taxonomy", 'avia_framework' ), "id" => "link", "fetchTMPL" => true, "type" => "linkpicker", "subtype" => array( __('Display Entries from:', 'avia_framework' )=>'taxonomy'), "multiple" => 6, "required" => array('blog_type', 'equals', 'taxonomy'), "std" => "category" ), array( "name" => __("Which Year?", 'avia_framework' ), "desc" => __("Select which year should be displayed", 'avia_framework' ), "id" => "year", "type" => "select", "multiple" => 6, "required" => array('blog_type', 'equals', 'year'), ),
Thanks a lot!
Cheers,
Anne-Laure
August 9, 2017 at 9:30 am #836244Hi,
Glad we could help you.
In the code above the content of the select box is missing, should be something like:
array( "name" => __("Which Year?", 'avia_framework' ), "desc" => __("Select which year should be displayed", 'avia_framework' ), "id" => "year", "type" => "select", "multiple" => 6, "subtype" => array( __('2015', 'avia_framework') =>'2015', __('2016', 'avia_framework') =>'2016' ), "required" => array('blog_type', 'equals', 'year'), ),
Best regards,
GünterAugust 9, 2017 at 2:33 pm #836422Hi Günter,
It works fine now. Thanks!
One more thing – if I may – I should define ‘year’ somewhere so the system knows what are ‘2015’ and ‘2016’ (and so it doesn’t considerate those as strings to display).
I would write something like that:
'year' => get_the_date('Y')
But I don’t see where and not sure how to define ‘year’ in the file. Would it be below the following:function query_entries($params) { global $avia_config; $query = array(); if(!empty($params['categories']) && is_string($params['categories'])) { //get the categories $terms = explode(',', $params['categories']); }
?
Thank you very much!
Kind regards,
Anne-Laure
August 9, 2017 at 6:05 pm #836534Hi,
Normally PHP makes type conversions automatically where necessary. And in the database query it is converted to a string anyway.
Best regards,
GünterAugust 10, 2017 at 9:39 am #836853Hi Günter,
Thanks for the explanation!
The issue I have is that I can now select the year. But if I select ‘2016’, I see all published posts. I’m missing something somewhere, but I have no clue what. Do you have any idea?Thank you very much!
Kind regards,
Anne-Laure
August 10, 2017 at 1:46 pm #836972Hi,
In file enfold\config-templatebuilder\avia-shortcodes\blog.php around line 442 you find:
//if we find categories perform complex query, otherwise simple one if(isset($terms[0]) && !empty($terms[0]) && !is_null($terms[0]) && $terms[0] != "null" && !empty($params['taxonomy'])) { $query = array( 'paged' => $page, 'posts_per_page' => $params['items'], 'offset' => $params['offset'], 'post__not_in' => (!empty($no_duplicates)) ? $avia_config['posts_on_current_page'] : array(), 'post_type' => $params['post_type'], 'tax_query' => array( array( 'taxonomy' => $params['taxonomy'], 'field' => 'id', 'terms' => $terms, 'operator' => 'IN')) ); } else { $query = array( 'paged'=> $page, 'posts_per_page' => $params['items'], 'offset' => $params['offset'], 'post__not_in' => (!empty($no_duplicates)) ? $avia_config['posts_on_current_page'] : array(), 'post_type' => $params['post_type']); }
You have to add a date query:
'date_query' => array( array( 'year' => $params['year'] ))
that means replace above with:
//if we find categories perform complex query, otherwise simple one if(isset($terms[0]) && !empty($terms[0]) && !is_null($terms[0]) && $terms[0] != "null" && !empty($params['taxonomy'])) { $query = array( 'paged' => $page, 'posts_per_page' => $params['items'], 'offset' => $params['offset'], 'post__not_in' => (!empty($no_duplicates)) ? $avia_config['posts_on_current_page'] : array(), 'post_type' => $params['post_type'], 'date_query' => array( array( 'year' => $params['year'] )), 'tax_query' => array( array( 'taxonomy' => $params['taxonomy'], 'field' => 'id', 'terms' => $terms, 'operator' => 'IN')) ); } else { $query = array( 'paged'=> $page, 'posts_per_page' => $params['items'], 'offset' => $params['offset'], 'date_query' => array( array( 'year' => $params['year'] )), 'post__not_in' => (!empty($no_duplicates)) ? $avia_config['posts_on_current_page'] : array(), 'post_type' => $params['post_type']); }
And in line 335 you have to add ‘year’ to the default array:
$atts = shortcode_atts(array('blog_style' => '', 'columns' => 3, 'year' => 2016,
Best regards,
Günter- This reply was modified 7 years, 3 months ago by Günter.
August 10, 2017 at 2:45 pm #836997Hi Günter,
Thank you for this!
Unfortunately, it’s still showing all of the published posts.
When you define ‘year’ as being 2016, shouldn’t it limit the year to this particular one? In the example you mentioned where year might be 2015 or 2016, how does it knows what to do if I select 2015?
In line 335, I tried to add$atts = shortcode_atts(array('blog_style' => '', 'columns' => 3, 'year' => get_the_date('Y'), 'blog_type' => 'posts', 'items' => '16', 'paginate' => 'yes', 'categories' => '', 'preview_mode' => 'auto', 'image_size' => 'portfolio', 'taxonomy' => 'category', 'post_type'=> get_post_types(), 'contents' => 'excerpt', 'content_length' => 'content', 'offset' => '0', 'conditional' => '' ), $atts, $this->config['shortcode']);
But nothing is working… :-/
Thank you very much again!
Kind regards,
Anne-Laure
August 11, 2017 at 10:11 am #837365Hi,
Can you give me FTP access and a WP admin account to your server, so I can update your file directly? Would be the easiest way.
You can post it in private content area.
Best regards,
GünterAugust 11, 2017 at 12:10 pm #837401This reply has been marked as private.August 11, 2017 at 3:54 pm #837487Hi,
I found a solution.
I updated the file blog.php and postslider.php (both are in the child theme).
The only restriction by WordPress is, that you can select one year only for filter (or all) – this is the restriction for the date query.
I hope, this helps you.
Best regards,
GünterAugust 11, 2017 at 4:13 pm #837499Hi Günter,
You solved it!
A HUGE thank you!!!You rock!
Have a wonderful weekend!
Anne-Laure
August 11, 2017 at 4:30 pm #837504 -
AuthorPosts
- The topic ‘How to display blog posts from a custom year automatically ?’ is closed to new replies.