mardi 3 octobre 2023

php to generate random links not working - WordPress

Let me explain - I made a PHP that SORTA works, but it could be better - basically, the PHP is assigned to an image module on WordPress that when clicked; is supposed to take the user to a random PROJECT on the site. The problem is; while the function works, I keep getting the same projects repeated over and over; we have probably about 12 projects and I can't click the module more than 4 times before getting the same project that I saw previously. I need to generate a php that takes the user to a portfolio PROJECT without there being an overlap (i.e. being able to rifle randomly all 12 without overlapping). See below for the code

THANK YOU VERY MUCH, CHEERS!

<?php
class ICE_CREAM_CONE extends ET_Builder_Module {
    // Module slug (also used as shortcode tag)
    public $slug = 'ice_cream_cone';


    // Full Visual Builder support
    public $vb_support = 'on';


    /**
     * Module properties initialization
     *
     * @since 1.0.0
     */
    function init() {
        $this->name = esc_html__( 'Ice Cream Cone', 'ak-text-domain' );
    }


    /**
     * Module's specific fields
     *
     * @since 1.0.0
     *
     * @return array
     */
    function get_fields() {
        return array(
            'image' => array(
                'label'             => esc_html__( 'Image', 'ak-text-domain' ),
                'type'              => 'upload',
                'toggle_slug'       => 'ice_cream_cone'
            )
        );
    }


    function get_settings_modal_toggles() {
        return array(
            'content' => array(
                'toggles' => array(
                    'ice_cream_cone' => array(
                        'priority' => 0,
                        'title' => 'Ice Cream Cone'
                    )
                )
            )
        );
    }


    function getProjects() {
        $output = '';
        $currentPost = get_post();


        $args = array(
            'post_type' => 'project',
            'orderby' => 'rand',
            'posts_per_page' => 1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'project_category',
                    'field' => 'slug',
                    'terms' => 'ice_cream_cone'
                )
            )
        );


        if ( is_singular( 'project' ) ) {
            $args['post__not_in'] = array( $currentPost->ID );
        }


        $query = new WP_Query($args);


        if ( $query->posts ) {
            foreach( $query->posts as $p ) {
                $output = get_permalink( $p->ID );
            }
        }


        return $output;
        $can_visit_pages = array_diff($all_page_links,$visited_page_links);




    }
    


    /**
     * Render module output
     *
     * @since 1.0.0
     *
     * @param array  $attrs       List of unprocessed attributes
     * @param string $content     Content being processed
     * @param string $render_slug Slug of module that is used for rendering output
     *
     * @return string module's rendered output
     */
    function render( $attrs, $content = null, $render_slug ) {
        // Module specific props added on $this->get_fields()
        $image = $this->props['image'];


        // Render module content
        $output = sprintf(
            '<div class="ice-cream-cone-container"><a href="%2$s"><img src="%1$s" /></a></div>',
            esc_html( $image ),
            $this->getProjects()
        );


        // Render wrapper
        // 3rd party module with no full VB support has to wrap its render output with $this->_render_module_wrapper().
        // This method will automatically add module attributes and proper structure for parallax image/video background
        return $this->_render_module_wrapper( $output, $render_slug );
    }
}


new Ice_Cream_Cone;```


Crafting a code to generate random projects - the projects genreate but overlap frequently



Aucun commentaire:

Enregistrer un commentaire