Hi! First thing I want to say is: thank you for this tutorial series. The second thing is that I accepted the challenge and I created my version of the code. I'm not sure if it's the most effective way, but it works and it's much smaller than the previous piece of code! This is the function I''ve made in the theme-support.php file: function sunset_check_archive_type( $archVal ) { $archive_types = array( 'category', 'tag', 'author' ); $archive_category_name = 'category_name'; foreach ( $archive_types as $archive_type ) { if( in_array( $archive_type, $archVal ) ) { $type = ( $archive_type == 'category' ? $archive_category_name : $archive_type ); $currKey = array_keys( $archVal, $archive_type ); $nextKey = $currKey[0]+1; $value = $archVal[ $nextKey ]; } } $output = array( $type, $value ); return $output; } In Ajax.php I added this piece of code to retrieve the returning values. $archive_value = sunset_check_archive_type( $archVal ); $args[ $archive_value[0] ] = $archive_value[1]; I hope you like it! I would love to get feedback on my code and please tell me if I can optimize the code.
+BJHStyle | Gaming Videos, Ofcourse Hi, thank you so much for following and for your positive feedback. I Love your approach to optimize the code. Really smart use of multiple arrays and the foreach function to avoid code repetition. This is a really good implementation. One problem remain tho, and I will explain better this on the next video I'm publishing tomorrow. The in_array() function is quiet heavy on memory, so using it 3 times is not the best practice. It shouldn't really create any problem considering that our array won't have more than 10 items at most. Your solution is absolutely fine, and you can keep it like that. Check the next video to see the solution I adopted, and let me know what you think. Thank you so much for sharing your code and contributing in improving this channel. You're awesome!
+Alessandro Castellani Thanks for your feedback! I wasn't thinking of memory usage so I will keep that in mind if you would ever make an other challenge ;D Looking forward to tomorrow's video!
Hi Alex, With this lesson I solved my issues from the previous one. Thank you, you are a very good developer, I hope that one day I will reach your level!
+Alessandro Castellani yes please please make a video tutorials on GIT because i have found it very useful for developers but I don't know how to work started with GIT make beginners tutorials plz. I have bunch of my work in my localhost folders but I can't manage it on GIT even I don't have user over GIT thanks!
hi, alex! you may consider using the 'case' instead of 'if' for toner three IF. case1, do that; case2, do that, and case3, do that.. IF not and case, do this
+George Stamate Hi, thanks for watching. Good suggestion, but that doesn't solve the code repetition for the 3 cases. "case switch" and "if" are pretty much identical. :D
Hey man, how come this doesn't seem to work for the Author page for me? No posts ever get ajaxed. Only the initially loaded articles appear. Would be cool if you could help me.
Thank you for your work! Awesome tutorials. I am just curious how to make this part work with other languages. Cause my WP permalinks is in Russian. Is there any way to optimize code for different languages? Or you have to manually write "category"... and other markers in a different language?
Hi Alex, Is it possible to add a filter to the archive page and let visitors filter posts by date or year published. Also, the results have to include only posts from the category that the user is viewing at the time. If there is a solution in one of your tutorials can you direct me to it? Thanks.
Hi, thanks for the question. I didn't touch upon that on any of my tutorials, but it's fairly easy to implement as it's a built-in feature of WordPress. This tutorial will give a pretty good overview on how to achieve that: www.wpbeginner.com/plugins/how-to-let-users-filter-posts-and-pages-in-wordpress/ Cheers
Hi from cold Russia )) I very like those lessons. You're monster of magic of code. No stop! But we have a two trubles: First. in_array( "category", $archVal ) - What about TAG (category, author etc.), which name is 'category' or 'author' or 'tag' etc? Second. If we use some plugins (like "Yoast SEO"), we can remove the word 'category' from URL. What can we do in this case?
+Ignaty A. Kashnitsky Hi, thank you for checking my videos. I don't think I understood your question. In the video I explain how to handle and retrieve categories, tags, and author names, what is not clear? Thank you for flagging the option by Yoast SEP, I completely forgot about it. I will check a solution for this issue. Happy Coding!
+Alessandro Castellani Oh, I'm sorry. It's my mistake in my poor english )) For example. I have archive page - www.blablabla.com/tag/category. Would be script working correctly?
Hi. I have this exact problem but on the homepage. On scroll/after clicking "load more" the url changes to localhost/page/2. I've tried adding the same stuff as you did in archive but it doesn't work for me. The a-tags only have a data-page, data-prev and data-url in the indexpage. Am I missing something? I'm guessning I'm not supposed to add the data-arcive there but I tried it anyways and that didn't work as well. I also have an issue with the media upload library, every one of my galleries contains the post thumbnail (or sets the first img to a thumbnail) and it can't be removed. Some slides have the images I've added + a image that I have no idea where it comes from. Also, I can't make galleries of images that are in the media library, it only works when I upload new images directly, but this is harder to solve by just reading a comment so I would be really happy even if you just answer my first querstion. Thanks.
Hi again! I even downloaded your theme and the same issues remain unfortunately. When I scroll the url changes to localhost (on the homepage) and the gallery only works flawlessly when you upload files directly and not when you pick existing photos. Will you be addressing this in an upcoming tutorial?
Ajax Infinite Scroll work fine But this is can't work in search page, I am try to find solution but i didn't solve this Search pagination are this type : "/page/1?s=markup", "/page/2?s=markup" Can you help us
I'm concerned that, although the attempt to handle a sub-directory or multi-site (using sub-directories), the code is not being tested on such a site. In testing this, I found that the code duplicates portions of the url. Perhaps a better method for: function sunset_grab_current_uri() { $http = ( isset( $_SERVER["HTTPS"] ) ? '' : '' ); $referer = $http . $_SERVER["HTTP_HOST"]; $archive_url = $referer . $_SERVER["REQUEST_URI"];
return $archive_url; } This can be handled by WP functions that nicely wrap up finding the URL of the current page: function sunset_grab_current_uri() { global $wp; $archive_url = home_url( add_query_arg( array(), $wp->request ) ); return esc_url( $archive_url ); } Or, simply reduce to 2 lines by removing the $archive_url var and directly returning the home_url() function call. It also has the added security of WP calling those _SERVER vars inside its home_url() function...
hi alex my url didnt convert to localhost.sunset.com it still localhost/page/2/ and so and so, i used your source code but didn't work, can you help me to solve this problem?
,iam on windows and yes it does ,it LoadModule rewrite_module modules/mod_rewrite.so without#hash sign so i can't know where is problem which show url As it is now
Additionally, the Ajax.php code can be simplified to use your original code, by adding 2 lines and changing the else $page_trail var, like so (this uses var $archParts in place of the original $archVal): $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'paged' => $paged ); $site_url = get_site_url(); if ( $archive != '0' ) { $archSubStr = substr( $archive, strpos($archive, $site_url) + strlen($site_url) ); $archParts = explode( '/', $archSubStr ); $type = ( $archParts[1] == 'category' ? 'category_name' : $archParts[1]); $args[$type] = $archParts[2]; $page_trail = $site_url . '/' . $archParts[1] . '/' . $archParts[2] . '/'; } else { $page_trail = $site_url . '/'; } $query = new WP_Query( $args ); if ( $query->have_posts() ): $pageData = $page_trail."page/$paged/"; echo ''; This uses the WP function get_site_url() which provides the correct URL whether a root site or sub-directory site. One advantage of this is the ability to handle all other post types other than "category". Plus, php strpos is much faster than the array calls.
Also, the line: $archSubStr = substr( $archive, strpos($archive, $site_url) + strlen($site_url) ); can be further shortened to $archSubStr = substr( $archive, strlen($site_url) ); Originally, I was going to verify the strpos before attempting to parse it, but since WP's get_site_url() is reliable, it's not needed...
+Valentin Scarevnea be careful to not get confused. This current series is still ongoing, not finished yet. The video you linked is part of the WordPress 101 for beginners. Don't jump between videos and follow the order numerically. Happy Coding
+Alessandro Castellani I know, but i was fallowing this series, and i didn't see comments, then i remembered that in beginners series the comments are covered ))) thanks ;)
Hi Alex, Thanks for the bug fix video. I'm using windows. So i added some changes and now everything is working for me. In ajax.php => if ( $query->have_posts() ): => $site_url = ($page_trail === '/' ? get_site_url() : '' ); echo '';
Thanks for making the videos coming! I know this is unrelated and I am probably a real pain here but, could you do a tutorial on how to make an accordion menu like this: codyhouse.co/gem/css-multi-level-accordion-menu/ in WordPress? I have been struggling with this for weeks, my gf is not getting enough attention and I have asked on probably all forums I can without giving up. I am sure you can relate. There aren't really any videos on this on youtube so you will probably also get a lot of views/subscribers.
+Arete Thanks for the suggestion. It's actually really easy to achieve and it doesn't require much coding to recreate that structure showed in the tutorial you shared, with the Walker Nav Class. Did you check those couple of videos I uploaded? I create a simple dropdown, but the logic is exactly the same. Anyway, I will do an accordion menu in an upcoming video. Cheers
Thanks. I just watched your video on making a custom walker_nav_menu. At least I am able to create custom styles for the menu. I am still really stuck on the checkboxes and the jQuery though. I look forward to a video on this and I promise to donate (what I can) if you make it. Thanks again!
Hi! First thing I want to say is: thank you for this tutorial series. The second thing is that I accepted the challenge and I created my version of the code. I'm not sure if it's the most effective way, but it works and it's much smaller than the previous piece of code!
This is the function I''ve made in the theme-support.php file:
function sunset_check_archive_type( $archVal ) {
$archive_types = array( 'category', 'tag', 'author' );
$archive_category_name = 'category_name';
foreach ( $archive_types as $archive_type ) {
if( in_array( $archive_type, $archVal ) ) {
$type = ( $archive_type == 'category' ? $archive_category_name : $archive_type );
$currKey = array_keys( $archVal, $archive_type );
$nextKey = $currKey[0]+1;
$value = $archVal[ $nextKey ];
}
}
$output = array( $type, $value );
return $output;
}
In Ajax.php I added this piece of code to retrieve the returning values.
$archive_value = sunset_check_archive_type( $archVal );
$args[ $archive_value[0] ] = $archive_value[1];
I hope you like it!
I would love to get feedback on my code and please tell me if I can optimize the code.
+BJHStyle | Gaming Videos, Ofcourse Hi, thank you so much for following and for your positive feedback.
I Love your approach to optimize the code. Really smart use of multiple arrays and the foreach function to avoid code repetition. This is a really good implementation.
One problem remain tho, and I will explain better this on the next video I'm publishing tomorrow. The in_array() function is quiet heavy on memory, so using it 3 times is not the best practice. It shouldn't really create any problem considering that our array won't have more than 10 items at most.
Your solution is absolutely fine, and you can keep it like that.
Check the next video to see the solution I adopted, and let me know what you think.
Thank you so much for sharing your code and contributing in improving this channel. You're awesome!
+Alessandro Castellani Thanks for your feedback! I wasn't thinking of memory usage so I will keep that in mind if you would ever make an other challenge ;D Looking forward to tomorrow's video!
Hi Alex,
With this lesson I solved my issues from the previous one.
Thank you, you are a very good developer, I hope that one day I will reach your level!
+Victor S you can totally do it, I'm not that good.
You are too modest, of course you are very good, otherwise you won't have so many followers and such good tutorials with so good explanations!
Really nice! It works great!
Thank you so much
I love the challenge! I have created my version. Once I figure out Git I'll get it to you.
+Mark Waters That sounds awesome! I was thinking to do a quick Alecaddd Crash Course on Git, don't know if could be useful.
+Alessandro Castellani that would be pretty great.
+Alessandro Castellani yes please please make a video tutorials on GIT because i have found it very useful for developers but I don't know how to work started with GIT make beginners tutorials plz. I have bunch of my work in my localhost folders but I can't manage it on GIT even I don't have user over GIT thanks!
Ye, 37 another awesome TT. ^_______^
+YA SUO Thank you so much, I'm starting to think that maybe are too many videos :D
hi, alex! you may consider using the 'case' instead of 'if' for toner three IF. case1, do that; case2, do that, and case3, do that.. IF not and case, do this
those, instead of toner. :) story for my t9
+George Stamate Hi, thanks for watching.
Good suggestion, but that doesn't solve the code repetition for the 3 cases. "case switch" and "if" are pretty much identical. :D
+Alessandro Castellani ...if that is the case than do while will fix it maybe... we can try it
Hey man, how come this doesn't seem to work for the Author page for me? No posts ever get ajaxed. Only the initially loaded articles appear.
Would be cool if you could help me.
Thank you for your work! Awesome tutorials. I am just curious how to make this part work with other languages. Cause my WP permalinks is in Russian. Is there any way to optimize code for different languages? Or you have to manually write "category"... and other markers in a different language?
WordPress has the ability to create multi-lingual sections, I will explain this in future videos.
Cheers
Majestic! :-)
Thanks :D
Thank you for your awesome work. Pls, can you tell me which text editor you used for coding?
I use Panic Coda, Sublime Text, and Atom, depending on the project and how I feel :D
Hi Alex, Is it possible to add a filter to the archive page and let visitors filter posts by date or year published. Also, the results have to include only posts from the category that the user is viewing at the time. If there is a solution in one of your tutorials can you direct me to it? Thanks.
Hi, thanks for the question. I didn't touch upon that on any of my tutorials, but it's fairly easy to implement as it's a built-in feature of WordPress. This tutorial will give a pretty good overview on how to achieve that: www.wpbeginner.com/plugins/how-to-let-users-filter-posts-and-pages-in-wordpress/
Cheers
Thanks, Alex. I will check it.
Hi from cold Russia ))
I very like those lessons.
You're monster of magic of code. No stop!
But we have a two trubles:
First.
in_array( "category", $archVal ) - What about TAG (category, author etc.), which name is 'category' or 'author' or 'tag' etc?
Second.
If we use some plugins (like "Yoast SEO"), we can remove the word 'category' from URL. What can we do in this case?
+Ignaty A. Kashnitsky Hi, thank you for checking my videos.
I don't think I understood your question. In the video I explain how to handle and retrieve categories, tags, and author names, what is not clear?
Thank you for flagging the option by Yoast SEP, I completely forgot about it. I will check a solution for this issue.
Happy Coding!
+Alessandro Castellani Oh, I'm sorry. It's my mistake in my poor english ))
For example. I have archive page - www.blablabla.com/tag/category. Would be script working correctly?
nice question!
Hi.
I have this exact problem but on the homepage. On scroll/after clicking "load more" the url changes to localhost/page/2. I've tried adding the same stuff as you did in archive but it doesn't work for me. The a-tags only have a data-page, data-prev and data-url in the indexpage. Am I missing something? I'm guessning I'm not supposed to add the data-arcive there but I tried it anyways and that didn't work as well.
I also have an issue with the media upload library, every one of my galleries contains the post thumbnail (or sets the first img to a thumbnail) and it can't be removed. Some slides have the images I've added + a image that I have no idea where it comes from. Also, I can't make galleries of images that are in the media library, it only works when I upload new images directly, but this is harder to solve by just reading a comment so I would be really happy even if you just answer my first querstion.
Thanks.
Hi again!
I even downloaded your theme and the same issues remain unfortunately. When I scroll the url changes to localhost (on the homepage) and the gallery only works flawlessly when you upload files directly and not when you pick existing photos. Will you be addressing this in an upcoming tutorial?
Ajax Infinite Scroll work fine
But this is can't work in search page, I am try to find solution but i didn't solve this
Search pagination are this type : "/page/1?s=markup", "/page/2?s=markup"
Can you help us
is still continue ?
+Naipo Yes, I'm uploading a new video every week
I'm concerned that, although the attempt to handle a sub-directory or multi-site (using sub-directories), the code is not being tested on such a site. In testing this, I found that the code duplicates portions of the url. Perhaps a better method for:
function sunset_grab_current_uri() {
$http = ( isset( $_SERVER["HTTPS"] ) ? '' : '' );
$referer = $http . $_SERVER["HTTP_HOST"];
$archive_url = $referer . $_SERVER["REQUEST_URI"];
return $archive_url;
}
This can be handled by WP functions that nicely wrap up finding the URL of the current page:
function sunset_grab_current_uri() {
global $wp;
$archive_url = home_url( add_query_arg( array(), $wp->request ) );
return esc_url( $archive_url );
}
Or, simply reduce to 2 lines by removing the $archive_url var and directly returning the home_url() function call.
It also has the added security of WP calling those _SERVER vars inside its home_url() function...
hi alex my url didnt convert to localhost.sunset.com it still localhost/page/2/ and so and so, i used your source code but didn't work, can you help me to solve this problem?
What OS are you using? Are you on windows? Does your local dev setup allow permalinks?
,iam on windows and yes it does ,it LoadModule rewrite_module modules/mod_rewrite.so
without#hash sign so i can't know where is problem which show url As it is now
any idea to help me in this problem?
$site_url = ($page_trail === '/' ? get_site_url() : '' );
echo '';
may be this will solve your issue
Additionally, the Ajax.php code can be simplified to use your original code, by adding 2 lines and changing the else $page_trail var, like so (this uses var $archParts in place of the original $archVal):
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged
);
$site_url = get_site_url();
if ( $archive != '0' ) {
$archSubStr = substr( $archive, strpos($archive, $site_url) + strlen($site_url) );
$archParts = explode( '/', $archSubStr );
$type = ( $archParts[1] == 'category' ? 'category_name' : $archParts[1]);
$args[$type] = $archParts[2];
$page_trail = $site_url . '/' . $archParts[1] . '/' . $archParts[2] . '/';
} else {
$page_trail = $site_url . '/';
}
$query = new WP_Query( $args );
if ( $query->have_posts() ):
$pageData = $page_trail."page/$paged/";
echo '';
This uses the WP function get_site_url() which provides the correct URL whether a root site or sub-directory site. One advantage of this is the ability to handle all other post types other than "category". Plus, php strpos is much faster than the array calls.
Thank you! This is a fantastic approach :)
Also, the line: $archSubStr = substr( $archive, strpos($archive, $site_url) + strlen($site_url) );
can be further shortened to $archSubStr = substr( $archive, strlen($site_url) );
Originally, I was going to verify the strpos before attempting to parse it, but since WP's get_site_url() is reliable, it's not needed...
Sorry, maybe i was not focused, did i skip the single post page - comments area ?
+Valentin Scarevnea or is here - WordPress 101 - Part 11: The single.php file, tags, edit links and comment template
+Valentin Scarevnea be careful to not get confused. This current series is still ongoing, not finished yet.
The video you linked is part of the WordPress 101 for beginners. Don't jump between videos and follow the order numerically.
Happy Coding
+Alessandro Castellani I know, but i was fallowing this series, and i didn't see comments, then i remembered that in beginners series the comments are covered ))) thanks ;)
Hi Alex, Thanks for the bug fix video.
I'm using windows. So i added some changes and now everything is working for me.
In ajax.php => if ( $query->have_posts() ): =>
$site_url = ($page_trail === '/' ? get_site_url() : '' );
echo '';
Awesome, I'm glad is working properly for you :D
Thanks for making the videos coming! I know this is unrelated and I am probably a real pain here but, could you do a tutorial on how to make an accordion menu like this: codyhouse.co/gem/css-multi-level-accordion-menu/ in WordPress? I have been struggling with this for weeks, my gf is not getting enough attention and I have asked on probably all forums I can without giving up. I am sure you can relate. There aren't really any videos on this on youtube so you will probably also get a lot of views/subscribers.
+Arete Thanks for the suggestion. It's actually really easy to achieve and it doesn't require much coding to recreate that structure showed in the tutorial you shared, with the Walker Nav Class.
Did you check those couple of videos I uploaded? I create a simple dropdown, but the logic is exactly the same.
Anyway, I will do an accordion menu in an upcoming video.
Cheers
Thanks. I just watched your video on making a custom walker_nav_menu. At least I am able to create custom styles for the menu. I am still really stuck on the checkboxes and the jQuery though. I look forward to a video on this and I promise to donate (what I can) if you make it. Thanks again!