Declaring and Assigning Values to global variable

I am trying to figure out how to declare a global variable in Wordpress.

I also need to figure out how to give that global variable some values from an array.

I can’t seem to figure it out!

Just declare your variable in functions.php and it should be available everywhere.

But, there’s probably a better way to do what you’re trying to do. What are you trying to do?

Hey thanks for replying mdavis1982,

I am developing a theme, which started out as a hobby and soon became an obsession. I have my main loop in a widget. I also have a feature slider in a widget, with one loop, and another feature widget that has four loops. When any of the feature widgets are displayed on the same page as the main loop widget, I don’t want posts to be repeated.

I actually have posted the problem in another post. I will paste it below:

I have put my main loop into a widget. I also put a feature slider and feature posts displayed in boxes, both with options to decide which category to set as feature. My problem is that when all of these widgets are displayed on the same page, I don’t want the posts to be duplicated in the main loop.

I am aware that I have to declare a global variable and assign post IDs to that variable in an array, but I am not doing it right, apparently.

Here is the code for the feature slider loop:

<?php global $themename_do_not_duplicate;
    $themename_do_not_duplicate = array();
    $arr = array();
    $i=1;
    $width = $instance['ImgWidth'];
    $height = $instance['ImgHeight'];
    $cat_posts = new WP_Query("showposts=" . $instance["featuredNum"] . "&cat=" . $instance["cat"]);
    while ($cat_posts->have_posts()) : $cat_posts->the_post() $themename_do_not_duplicate[] = $posts->ID;?>
                <li><a href="<?php the_permalink();?>">
                <?php
                $post_title = get_the_title();
                $thumbnail = get_thumbnail($width,$height,'thumb',$post_title,$post_title);
                $thumb = $thumbnail["thumb"];
                print_thumbnail($thumb, $thumbnail["use_timthumb"], $post_title, $width, $height, 'thumb'); ?></a>
                <div class="title">
                <h2><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
                </div>
                </li>
        <?php endwhile; wp_reset_query();   ?>

Here is the loop for the feature boxes, which has four loops:


<?php query_posts("showposts=1&cat=".get_catId($instance['catOption4']));

                  while (have_posts()) : the_post(); $themename_do_not_duplicate[] = $post->ID;?>

...stufff

            <?php endwhile; wp_reset_query(); ?>


Do I have to put the global themename_do_not_duplicate before each loop here or can I just put it in the beginning of the widget?

Here is the main loop:


<?php global $themename_do_not_duplicate;

if ($instance['NoRepeatPost'] == 1) {$arrgs=array('posts_per_page'=>$instance['postNum'],'post__not_in' => $themename_do_not_duplicate,'paged'=>$paged,);}

else {$arrgs=array('posts_per_page'=>$instance['postNum'],'paged'=>$paged,);}

query_posts($arrgs);

if(have_posts()) : while (have_posts()) : the_post();$themename_do_not_duplicate[] = $post->ID; //begin the loop ?>

...stuff

<?php endif;wp_reset_query(); //and so ends the loop ?>


In the top of the header.php file I put global themename_do_not_duplicate

I hope I explained my problem thoroughly.