Okay
  Public Ticket #3480375
Portfolio
Closed

Comments

  •  1
    sofiane started the conversation

    Hello,


    I have a masonry portfolio on my homepage, in which portfolio items are ordered by date of creation (the latest are the highest). I'd like portfolio items to be displayed in a random order, so that it is ordered differently each time I refresh my homepage.

    Could you tell me if there's an option for that in the Laborator settings, or tell me what code I can paste in my PHP or JS to set this up ?

    Thanks in advance, have a nice day :)

  •  15
    hahni replied

    For my understanding: You have portfolio items on your homepage as an "eyecatcher" and on your site a normal portfolio page too (in which the items are ordered normally). On the homepage there should be a "random part" of your portfolio items? Am i right?

  •  1
    sofiane replied
    Sorry, I could've explained it better haha
    Here is what my homepage looks like: it's both the homepage and the portfolio page. I just want the items to be displayed in a random order.

    Thanks!
    2980209230.png
  •  15
    hahni replied

    If your homepage / landing page is made with WP bakery pagebuilder and NOT the portfolio post type archive page then you could change the query in the Portfolio AddOn, if the page is made with the portfolio page template then you have to tweak it by your functions.php and add:

    function your_function_name( $query ) {
        $posttype = $query->get( 'post_type' );
        if ( is_page( 'your_page_slug OR is_home()' && 'portfolio' == $posttype ) {
            $query->set( 'orderby', 'rand' );
        }
    }
    add_action( 'pre_get_posts', 'your_function_name' );
    


    This should work - untested. I do it similar but i track the shown items and show only "unviewed" items on next reload. So i had to shorten my code a lot.


    Try it und perhaps give a feedback.


  •  1
    sofiane replied

    Thank you for your answer!

    To be honest, my skills don't go beyond using a theme template and pasting custom CSS. 

    I used the "Freelancer" child theme so I think it is made with the portfolio page template you mentioned.

    I tried to put your code in the functions.php and it says there is a syntax error with the ";" on line 53. 

    Could you tell me what I did wrong?

    Thanks in advance :)

    6400682526.png
  •  15
    hahni replied

    sorry i forgot one closing bracket:


    function your_function_name( $query ) {
        $posttype = $query->get( 'post_type' );
        if ( is_page( 'your_page_slug OR is_home()' && 'portfolio' == $posttype ) ) {
            $query->set( 'orderby', 'rand' );
        }
    }
    add_action( 'pre_get_posts', 'your_function_name' );


    and i'am only a user too - only for your information.

    Regards André


  •  1
    sofiane replied

    The "is_home" didn't work for me but the page slug did!!
    You helped me to get rid of the biggest issue I had with my website, I'm really grateful.

    Thank you very much André, have a nice day :)

  •  15
    hahni replied

    You are right: is_home() only work for blog page as home. Sorry - i tried to answer quickly - but with the slug is more safe too. Nevertheless your using of is_home() was wrong. But for your case the slug is the right solution.

    If you have a lot of portfolio items, there exist the possibility to "track" the shown items and to show new ones on the next reload. But this is a little bit more tricky but works very nice. I use this on many positions on my new page.

  • [deleted] replied

    Hi sofiane,

    To make your portfolio on your homepage to show different projects every time you enter the page you just need to navigate to Portfolio Items element setting and change the Order by > Random.

    See video: https://d.pr/v/UcZ9bc

    Have a nice day!

    P.S. If you use a caching plugin this will not work as it should because caching plugins cache your pages and don't refresh them on each page load.

  •  1
    sofiane replied

    Hi Ylli,

    I was surprised not to find a built-in option for that, turns out I just didn't search enough 😅
    As I used the Portfolio page template, the "Custom Query" button is in Parameters and Options, but that's the only difference.

    8774032080.png

    Thank  you for your time, have a nice day as well!