Part 37 - WordPress Theme Development - Dynamic Archive Page Improvements

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ธ.ค. 2024

ความคิดเห็น • 55

  • @BJHStyle
    @BJHStyle 8 ปีที่แล้ว +1

    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.

    • @alecaddd
      @alecaddd  8 ปีที่แล้ว +1

      +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!

    • @BJHStyle
      @BJHStyle 8 ปีที่แล้ว +1

      +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!

  • @metalvocal
    @metalvocal 8 ปีที่แล้ว +1

    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!

    • @alecaddd
      @alecaddd  8 ปีที่แล้ว

      +Victor S you can totally do it, I'm not that good.

    • @metalvocal
      @metalvocal 8 ปีที่แล้ว

      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!

  • @jellycoding
    @jellycoding 8 ปีที่แล้ว +1

    Really nice! It works great!

    • @alecaddd
      @alecaddd  8 ปีที่แล้ว

      Thank you so much

  • @markwaters5964
    @markwaters5964 8 ปีที่แล้ว +1

    I love the challenge! I have created my version. Once I figure out Git I'll get it to you.

    • @alecaddd
      @alecaddd  8 ปีที่แล้ว

      +Mark Waters That sounds awesome! I was thinking to do a quick Alecaddd Crash Course on Git, don't know if could be useful.

    • @markwaters5964
      @markwaters5964 8 ปีที่แล้ว +1

      +Alessandro Castellani that would be pretty great.

    • @TahirAfridi
      @TahirAfridi 8 ปีที่แล้ว

      +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!

  • @iyasuo
    @iyasuo 8 ปีที่แล้ว +1

    Ye, 37 another awesome TT. ^_______^

    • @alecaddd
      @alecaddd  8 ปีที่แล้ว

      +YA SUO Thank you so much, I'm starting to think that maybe are too many videos :D

  • @gxgl
    @gxgl 8 ปีที่แล้ว +1

    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

    • @gxgl
      @gxgl 8 ปีที่แล้ว

      those, instead of toner. :) story for my t9

    • @alecaddd
      @alecaddd  8 ปีที่แล้ว

      +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

    • @gxgl
      @gxgl 8 ปีที่แล้ว

      +Alessandro Castellani ...if that is the case than do while will fix it maybe... we can try it

  • @bendyamin86
    @bendyamin86 7 ปีที่แล้ว

    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.

  • @oksanaromaniv7102
    @oksanaromaniv7102 7 ปีที่แล้ว +1

    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?

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว +2

      WordPress has the ability to create multi-lingual sections, I will explain this in future videos.
      Cheers

  • @JDdueck
    @JDdueck 6 ปีที่แล้ว +1

    Majestic! :-)

    • @alecaddd
      @alecaddd  6 ปีที่แล้ว +1

      Thanks :D

  • @mdbenjirahmed3294
    @mdbenjirahmed3294 7 ปีที่แล้ว

    Thank you for your awesome work. Pls, can you tell me which text editor you used for coding?

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      I use Panic Coda, Sublime Text, and Atom, depending on the project and how I feel :D

  • @samahmohamed9574
    @samahmohamed9574 6 ปีที่แล้ว +1

    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.

    • @alecaddd
      @alecaddd  6 ปีที่แล้ว

      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

    • @samahmohamed9574
      @samahmohamed9574 6 ปีที่แล้ว +1

      Thanks, Alex. I will check it.

  • @kiadev
    @kiadev 8 ปีที่แล้ว +2

    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?

    • @alecaddd
      @alecaddd  8 ปีที่แล้ว +1

      +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!

    • @kiadev
      @kiadev 8 ปีที่แล้ว +1

      +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?

    • @raulterraferrao696
      @raulterraferrao696 7 ปีที่แล้ว

      nice question!

  • @hontogmittnamnigen
    @hontogmittnamnigen 8 ปีที่แล้ว

    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.

    • @hontogmittnamnigen
      @hontogmittnamnigen 8 ปีที่แล้ว

      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?

  • @jillurrahman5281
    @jillurrahman5281 7 ปีที่แล้ว

    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

  • @bantubikindotcom7303
    @bantubikindotcom7303 8 ปีที่แล้ว +1

    is still continue ?

    • @alecaddd
      @alecaddd  8 ปีที่แล้ว

      +Naipo Yes, I'm uploading a new video every week

  • @robstarr8055
    @robstarr8055 6 ปีที่แล้ว

    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...

  • @mahmoudkhdre
    @mahmoudkhdre 7 ปีที่แล้ว

    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?

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      What OS are you using? Are you on windows? Does your local dev setup allow permalinks?

    • @mahmoudkhdre
      @mahmoudkhdre 7 ปีที่แล้ว

      ,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

    • @mahmoudkhdre
      @mahmoudkhdre 7 ปีที่แล้ว

      any idea to help me in this problem?

    • @abhishekbaliyan5891
      @abhishekbaliyan5891 6 ปีที่แล้ว

      $site_url = ($page_trail === '/' ? get_site_url() : '' );
      echo '';
      may be this will solve your issue

  • @robstarr8055
    @robstarr8055 6 ปีที่แล้ว

    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.

    • @kevinmackey3166
      @kevinmackey3166 5 ปีที่แล้ว

      Thank you! This is a fantastic approach :)

  • @robstarr8055
    @robstarr8055 6 ปีที่แล้ว

    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...

  • @ValentinScarevnea
    @ValentinScarevnea 8 ปีที่แล้ว

    Sorry, maybe i was not focused, did i skip the single post page - comments area ?

    • @ValentinScarevnea
      @ValentinScarevnea 8 ปีที่แล้ว

      +Valentin Scarevnea or is here - WordPress 101 - Part 11: The single.php file, tags, edit links and comment template

    • @alecaddd
      @alecaddd  8 ปีที่แล้ว +1

      +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

    • @ValentinScarevnea
      @ValentinScarevnea 8 ปีที่แล้ว +1

      +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 ;)

  • @abhishekbaliyan5891
    @abhishekbaliyan5891 6 ปีที่แล้ว +1

    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 '';

    • @alecaddd
      @alecaddd  6 ปีที่แล้ว

      Awesome, I'm glad is working properly for you :D

  • @arete_
    @arete_ 8 ปีที่แล้ว +1

    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.

    • @alecaddd
      @alecaddd  8 ปีที่แล้ว +1

      +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

    • @arete_
      @arete_ 8 ปีที่แล้ว

      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!