Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How would you pass a variable from a foreach loop, through a link? I have tabs that display correctly. The jQuery works. But when I call the variable $category down where the content sorts, $category doesn't have a value.

<ul id="tabs">

    <li id="tab-content-all" class="active-tab">
        <a href="javascript:viewTab('content-all');">ALL</a>
    </li>

<?php 
    $categories=  get_categories('child_of=104'); 
    foreach ($categories as $category) {
    $option = '<li id="tab-content-';
        $option .= $category->cat_name;
        $option .= '"><a href="javascript:viewTab(\'content-';
        $option .= $category->cat_name;
        $option .='\');">';
        $option .= $category->cat_name;
        $option .= '</a></li>';
        echo $option;
    }
?>


</ul><!--end of tabs-->
<div id="contents-container">

    <div id="content-all">
        <ul class="gallery">

        <?php
            /**Get the posts by a category*/
            $portfolio_posts = new WP_Query('category_name=portfolio');
            /** Initiate the output*/
            $output = '';
            /** Loop through the post */
            while ($portfolio_posts->have_posts()): $portfolio_posts-> the_post();

                /** Add Thumbnail to the anchor if present     */
                $output .= '<li class="g_image"><a href="' . get_permalink($post->ID) . '">';
                $size='medium';
                if(has_post_thumbnail($post->ID))
                    $output .= get_the_post_thumbnail($post->ID,$size);   
                /** Close the widget*/
                $output .= '</a></li>';

            endwhile;
            echo $output;
        ?>  
        </ul>  <!--end of gallery--> 
    </div><!--end of ALL -->  


    <?php
        $output = '';
        $output = '<div id="content-'.$category->cat_name.'">';
    ?>
    <ul class="gallery">"

        <?php

            $category_name = strtolower($category->cat_name);


            /**Get the posts by a category*/
            $portfolio_posts = new WP_Query('category_name='.$category_name);
            /** Initiate the output*/

            $output = '';
            /** Loop through the post */
            while ($portfolio_posts->have_posts()): $portfolio_posts-> the_post();

                /** Add Thumbnail to the anchor if present     */
                $output = $category_name;
                $output .= '<li class="g_image"><a href="' . get_permalink($post->ID) . '">';

                $size='medium';
                if(has_post_thumbnail($post->ID))
                    $output .= get_the_post_thumbnail($post->ID,$size);   
                /** Close the widget*/
                $output .= '</a></li>';

            endwhile;
            echo $output;

            rewind_posts();
        ?>  
    </ul>  <!--end of gallery--> 
</div><!--end of ALL -->  

The jQuery tabbing works. Thanks.

share|improve this question
    
Close-voted as off-topic. This is a PHP question, not a WordPress question. –  Chip Bennett Jun 7 '12 at 21:48
add comment

1 Answer

This is more of a basic PHP question, but still it's somewhat important. Inside your foreach loop, $category is a locally-defined variable. That's to say, it only exists inside the foreach loop.

So if you have

foreach ( $categories as $category ) {
    // You can use $category all you want in here
}

// Out here $category ceases to exist

This is just the way iterative loops work ... Even if the variable still existed outside of the foreach you have no way to know which $category you're referencing.

From the looks of things, though the $category you want is your portfolio category (I'm guessing it has a category ID of 104 based on your earlier code).

You can still fetch this category based on its ID, just be sure to give it a unique name rather than $category:

$parent_category = get_category( 104 );
$parent_category_name = $parent_category->name;

You can then chug merrily along.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.