• Resolved davidmaxwaterman

    (@davidmaxwaterman)


    Hi,

    Disclaimer: I’m no expert on ‘driving’ wordpress via the UI, but I’ve been tasked to make changes to the UI and a few other bits and pieces, and I’ve done that mostly by hacking away at the code, and that seems to work ok.

    My latest task is to change something on the project pages. Currently, the ‘all’ page allows the user to select ‘projects’, and when one is selected, it takes me to a URL like this :

    https://domain/?project=project-space

    and this page shows the stuff that’s been loaded for that project with a side bar that shows a ‘tag cloud’ widget, but it doesn’t show tags actually – instead it shows the customer taxonomies I’ve hacked in with all the terms associated with each.

    The change they want me to make is to either *only* show the terms that have been ‘assigned’ to the current page’s project, *or* somehow highlight the ones that are ‘assigned’.

    I see on the ‘projects’ page on the admin pages that this is done in a table format, so it must be possible somehow, so I wonder if there’s a nice simple function like ‘get_terms’ that I can use … I see ‘get_objects_in_term’? Can that do it?

    Apologies if I’m not using the correct ‘lingo’ – I’m really not familiar with wordpress.

    Any pointers appreciated..

    Thanks!

    Max.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter davidmaxwaterman

    (@davidmaxwaterman)

    OK, I made it work, so I’m recording my solution for prosperity. Here’s the diff :

    diff --git a/wp-includes/default-widgets.php b/wp-includes/default-widgets.php
    index e48cc01..e72268c 100644
    --- a/wp-includes/default-widgets.php
    +++ b/wp-includes/default-widgets.php
    @@ -1241,6 +1241,13 @@ class WP_Widget_Tag_Cloud extends WP_Widget {
                            echo $before_title . $title . $after_title;
                    echo '<div class="tagcloud">';
    
    +               // get terms for this taxonomy
    +               $termsInThisTaxonomy=wp_get_object_terms(array(get_the_ID()), array($current_taxonomy));
    +               $include_terms = array();
    +               foreach ($termsInThisTaxonomy as $term) {
    +                        array_push($include_terms, $term->{'term_id'});
    +               }
    +
                    /**
                     * Filter the taxonomy used in the Tag Cloud widget.
                     *
    @@ -1251,9 +1258,31 @@ class WP_Widget_Tag_Cloud extends WP_Widget {
                     *
                     * @param array $current_taxonomy The taxonomy to use in the tag cloud. Default 'tags'.
                     */
    -               wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
    -                       'taxonomy' => $current_taxonomy
    -               ) ) );
    +               if (count($include_terms)==0) {
    +                       // including no terms so exclude all terms
    +                       $args = array(
    +                               'project_collection',
    +                               'project_market_segment',
    +                               'project_platform',
    +                               'project_phase',
    +                               'project_tag',
    +                       );
    +                       $all_terms = get_terms( $args );
    +
    +                       $exclude_terms = array();
    +                       foreach ($all_terms as $term) {
    +                                array_push($exclude_terms, $term->{'term_id'});
    +                       }
    +                       wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
    +                               'taxonomy' => $current_taxonomy,
    +                               'exclude' => $exclude_terms
    +                       ) ) );
    +               } else {
    +                       wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
    +                               'taxonomy' => $current_taxonomy,
    +                               'include' => $include_terms
    +                       ) ) );
    +               }
    
                    echo "</div>\n";
                    echo $after_widget;
    @@ -1410,3 +1439,4 @@ function wp_widgets_init() {
     }
    
     add_action('init', 'wp_widgets_init', 1);
    +

    It does what I want it to, so I’m happy, but, as I mention, I’m no wordpress expert and the above is quite likely a ‘hack’ in this context (ie there is a ‘proper way’). Comments welcome.

    Max.

    Thread Starter davidmaxwaterman

    (@davidmaxwaterman)

    I should note that I am using an older version of php and so array_column wasn’t available, which would have made the code quite a bit simpler.

    Also note I pass an array of my custom taxonomies into get_terms. I guess there’s a way to not have to hard-code those…I manually registered them elsewhere in the code.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘hacking tags cloud to only show terms assigned to this project.’ is closed to new replies.