• Resolved feisar

    (@feisar)


    I’m moving to this plugin from an old abandoned one and liking everything about it so far. Thanks for developing this!

    Is there a way to output a HTML list <ul> of links from a particular download category? Or a HTML <select> menu? For example, to show the latest 5 downloads from the ‘newsletters’ category?

    Any hooks I could use to create custom outputs myself?

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jeff Starr

    (@specialk)

    Glad to help. Currently the plugin does not output any lists of anything. But I like the ideas presented here and have added to the suggestion list. If nothing else, I will add a category attribute to the display-count shortcode. That would make it possible to manually create lists from specific categories, etc. Thank you for the feedback, @feisar. Feel free to post again with any further ideas, questions, etc.

    Thread Starter feisar

    (@feisar)

    Thanks Jeff. I had a bit of time today to attempt something myself, and came up with the below shortcode function. The coding isn’t up to your standards, but it works ok for my purposes until an official solution:

    add_shortcode('sdc_list', 'sdc_download_list');
    
    function sdc_download_list($atts) {
        $a = shortcode_atts( [
            'cat'           => '',
            'num'           => 5,   /* num of downloads to show in list */
            'extra'         => 'no',    /* show select menu for more downloads yes/no */
            'select_text'   => 'Select more...'
        ], $atts );
    
        if (empty($cat)) {
    
            return __('No Download Category', 'simple-download-counter');
    
        }
    
        $args = [
            'post_type'        => 'sdc_download',
            'post_status'      => 'publish',
            'tax_query'        => [
                [
                    'taxonomy' => 'sdc_download_category',
                    'field'    => 'slug',
                    'terms'    => $a['cat'],
                ],
            ],
            'order'            => 'DESC',
            'orderby'		   => 'date',
            'posts_per_page'   => -1,
        ];
    
        $my_query  = new WP_query( $args );
        $downloads = $my_query->posts;
    
        $output = '<ul class="sdc_list">';
    
        foreach ( array_slice( $downloads, 0, $a['num'] ) as $item ) {
            $url = simple_download_counter_download_url($item->ID);
            $hash = get_post_meta($item->ID, 'sdc_download_hash', true);
            if ($hash) {
                 $url = add_query_arg('key', $hash, $url);
            }
    
            $output .= '<li><a href="' . esc_url($url) . '">' . esc_html($item->post_title) . '</a></li>';
        }
        $output .= '</ul>';
    
        if ( $a['extra'] == 'yes' ) {
            $output .= '<p><select onchange="if (this.value) window.location.href=this.value">';
            $output .= '<option disabled selected>' . $a['select_text'] .'.</option>';
    
            foreach ( array_slice( $downloads, $a['num'], 999 ) as $item ) {
                $url = simple_download_counter_download_url($item->ID);
                $hash = get_post_meta($item->ID, 'sdc_download_hash', true);
                if ($hash) {
                     $url = add_query_arg('key', $hash, $url);
                }
                $output .= '<option value="' . esc_url($url) .'">' . esc_html($item->post_title) . '</option>';
            }
            $output .= '</select></p>';
        }
    
        return $output;
    }
    Plugin Author Jeff Starr

    (@specialk)

    Thanks @feisar, I will take a closer look at this for the next plugin update.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Output list of links from category of downloads’ is closed to new replies.