Help with PHP code

Party Monger

Skilled
Need some help guys,
My wordpress theme shows the following error
Warning: Invalid argument supplied for foreach() in /home/partymon/public_html/blog/wp-content/themes/xxxxx/header.php on line 309

Warning: array_slice() expects parameter 1 to be array, string given in /home/partymon/public_html/blog/wp-content/themes/xxxxx/header.php on line 314

Warning: Invalid argument supplied for foreach() in /home/partymon/public_html/blog/wp-content/themes/xxxxx/header.php on line 316

Following is the content of php file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>

<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />

<title><?php bloginfo('name'); ?> <?php wp_title(); ?></title>

<meta name="generator" content="WordPress <?php bloginfo('version'); ?>" />

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<!--[if IE 6]>
<style type="text/css">
body {
behavior:url("<?php bloginfo('template_url'); ?>/scripts/csshover2.htc");
}
</style>
<![endif]-->
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="<?php bloginfo('rss2_url'); ?>" />
<link rel="alternate" type="text/xml" title="RSS .92" href="<?php bloginfo('rss_url'); ?>" />
<link rel="alternate" type="application/atom+xml" title="Atom 0.3" href="<?php bloginfo('atom_url'); ?>" />

<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />

<?php $cp_iIcon = get_settings( "cp_favICON" );
if( $cp_iIcon != "" ) {
?>
<link rel="icon" href="<?php bloginfo('template_url'); ?>/images/icons/<?php echo $cp_iIcon; ?>" />
<link rel="shortcut icon" href="<?php bloginfo('template_url'); ?>/images/icons/<?php echo $cp_iIcon; ?>" /><link rel="Shortcut Icon" href="http://www.ecoinscollector.com/favicon.ico" type="image/x-icon" /><?php } ?>
<style type="text/css">
<?php for( $cp_i = 1; $cp_i <= 5; $cp_i ++ ) {
$cp_iCol = get_settings( "cp_hexColor_" . $cp_i );
if( $cp_iCol != "" ) { ?>
#cat-<?php echo get_settings( "cp_colorCategory_" . $cp_i ); ?> { border-top:8px solid <?php echo $cp_iCol ?>; }
#cat-<?php echo get_settings( "cp_colorCategory_" . $cp_i ); ?>:hover, #cat-<?php echo get_settings( "cp_colorCategory_" . $cp_i ); ?> a:hover {background:<?php echo $cp_iCol ?>; color:#333; }
#sidebar h3.catt-<?php echo get_settings( "cp_colorCategory_" . $cp_i ); ?> {background:<?php echo $cp_iCol ?>; }

<?php } } ?>
</style>

<?php wp_head(); ?>

</head>

<body>
<div id="head" class="clearfloat">

<div class="clearfloat">
<div id="logo" class="left">
<?php $cp_iLogo = get_settings( "cp_logo" );
if( $cp_iLogo != "" ) {
?>
<a href="<?php echo get_option('home'); ?>">
</a>
<?php } ?>

</div>

<div class="right">
<?php $cp_i = 1; $cp_iAd = get_settings( "cp_adImage_" . $cp_i ); ?>
<?php if(($cp_iAd != "") && ($cp_iAd != "Adsense")) { ?>
<a href="<?php echo get_settings( "cp_adURL_" . $cp_i ); ?>">
</a>
<?php } else { ?>

<?php if( $cp_iAd != "") { ?>
<?php $cp_iAdcode = get_settings( "cp_adAdsenseCode_" . $cp_i );
if( $cp_iAdcode != "" ) {
?>
<script type="text/javascript"><!--
google_ad_client = "<?php echo get_settings( "cp_adGoogleID" ); ?>";
google_ad_slot = "<?php echo get_settings( "cp_adAdsenseCode_" . $cp_i ); ?>";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<?php } } ?>
<?php } ?>

</div>

</div>

</div>

<div id="navbar" class="clearfloat">

<ul id="page-bar" class="left clearfloat">

[*]Home

<?php wp_list_pages('sort_column=menu_order&title_li='); ?>

[/list]

<?php include (TEMPLATEPATH . '/searchform.php'); ?>

</div>

<div id="top" class="clearfloat">
<?php if (is_home() && !is_paged()) { ?>
<div id="headline">

<?php
//Get value from Admin Panel
$cp_categories = get_categories('hide_empty=0');
$ar_headline = get_settings( "ar_headline" );
if( $ar_headline == 0 ) { $ar_headline = $cp_categories[0]->cat_ID; }
query_posts( 'showposts=1&cat=' . $ar_headline );
?>

<div class="label"><?php single_cat_title(); ?> »</div>

<?php while (have_posts()) : the_post(); ?>

<div class="clearfloat">
<?php $width = get_settings ( "cp_thumbWidth_Headline" );
$height = get_settings ( "cp_thumbHeight_Headline" );
if ( $width == 0 ) { $width = 200; }
if ( $height == 0 ) { $height = 225; }
?>

<?php $status = get_settings ( "cp_thumbAuto" );
if ( $status != "first" ) { ?>
<?php $values = get_post_custom_values("Image");
if (isset($values[0])) { ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo get_option('home'); ?>/<?php
$values = get_post_custom_values("Image"); echo $values[0]; ?>&w=<?php echo $width; ?>&h=<?php echo $height; ?>&zc=1&q=100"
alt="<?php the_title(); ?>" class="left" width="<?php echo $width; ?>px" height="<?php echo $height; ?>px" /></a>
<?php } ?>
<?php } else { ?>

<?php $id =$post->ID;
$the_content =$wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
$pattern = '!


<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
" class="left" width="<?php echo $width; ?>px" height="<?php echo $height; ?>px" /></a>

<?php } ?>
<div class="title"><?php the_title(); ?></div>
<div class="meta"><?php the_time('D, j/m/y') ?> – <?php the_time('G:i') ?> | <?php comments_popup_link('No Comment', 'One Comment', '% Comments');?></div>
<?php the_excerpt() ?>
">Read the full story »
</div>
<?php endwhile; ?>

</div>

<div id="featured">
<?php
//Get value from Admin Panel
$cp_categories = get_categories('hide_empty=0');
$ar_featured = get_settings( "ar_featured" );
if( $ar_featured == 0 ) { $ar_featured = $cp_categories[0]->cat_ID; }
$num = get_settings( "cp_numFeatured" );
if( $num == 0 ) { $num = 3; }
query_posts( 'showposts=' . $num . '&cat=' . $ar_featured );
?>
<div class="label"><?php single_cat_title(); ?> »</div>
<?php while (have_posts()) : the_post(); ?>
<div class="clearfloat">

<?php $status = get_settings ( "cp_thumbAuto" );
if ( $status != "first" ) { ?>

<?php $values = get_post_custom_values("Image");
if (isset($values[0])) { ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo get_option('home'); ?>/<?php
$values = get_post_custom_values("Image"); echo $values[0]; ?>&w=100&h=65&zc=1&q=100"
alt="<?php the_title(); ?>" class="left" width="100px" height="65px" /></a><?php } ?>

<?php } else { ?>

<?php $id =$post->ID;
$the_content =$wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
$pattern = '!


<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
" class="left" width="100px" height="65px" /></a>

<?php } ?>

<div class="info"><?php the_title(); ?>
<div class="meta"><?php the_time('D, j/m/y') ?> – <?php the_time('G:i') ?> | <?php comments_popup_link('No Comment', 'One Comment', '% Comments');?></div>

</div>

</div>
<?php endwhile; ?>

<?php wp_reset_query(); ?>

</div>

</div>

<div id="mores" class="clearfloat">

<div id="more-head">More posts from this section »</div>
<div id="more-feat">More posts from this section »</div>

</div>
<?php } else { ?>

<?php $showheadline1 = get_settings ( "cp_showpostheadline" );
$showheadline2 = get_settings ( "cp_showarchiveheadline" );
if ( (is_home() && is_paged()) || (is_search() && $showheadline2 != "no") || (is_archive() && $showheadline2 != "no") || (is_single() && $showheadline1 != "no") ) {

query_posts("showposts=1&category_name=Headline"); ?>

<?php while (have_posts()) : the_post(); ?>
<div id="single_head">

<?php $status = get_settings ( "cp_thumbAuto" );
if ( $status != "first" ) { ?>

<?php $values = get_post_custom_values("Image");
if (isset($values[0])) { ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo get_option('home'); ?>/<?php
$values = get_post_custom_values("Image"); echo $values[0]; ?>&w=175&h=125&zc=1&q=100"
alt="<?php the_title(); ?>" class="left" width="175px" height="125px" /></a>
<?php } ?>
<?php } else { ?>

<?php $id =$post->ID;
$the_content =$wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
$pattern = '!


<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
" class="left" width="175px" height="125px" /></a>

<?php } ?>
</div>

<div id="single_desc">
<div class="label">headline »</div>
<div class="title"><?php the_title(); ?></div>
<div class="meta"><?php the_time('D, j/m/y') ?> – <?php the_time('G:i') ?> | <?php comments_popup_link('No Comment', 'One Comment', '% Comments');?></div>
<?php the_excerpt(); ?>
">Read the full story »
</div>
<?php endwhile; ?>

<?php wp_reset_query(); ?>
<?php } ?>
</div>
</div>

<?php } ?>




<?php $showcatbar1 = get_settings ( "cp_showpostcatbar" );
$showcatbar2 = get_settings ( "cp_showarchivecatbar" );
if ( (is_home()) || (is_search() && $showheadline2 != "no") || (is_archive() && $showcatbar2 != "no") || (is_single() && $showcatbar1 != "no") ) { ?>

<div id="middle" class="clearfloat">

<?php $postcat = get_settings( "ar_categories" );
if( $ar_categories == 0 ) { $ar_categories= $cp_categories->cat_ID; }

if( ! is_array( $postcat ) ) {
foreach ( $cp_categories as $b ) {
$postcat[] = $b->cat_ID;
}
}
$postcat = array_slice($postcat, 0, 5);

foreach ($postcat as $cp_pC ) { ?>
<?php query_posts("showposts=1&cat=$cp_pC"); ?>
<div id="cat-<?php echo $cp_pC; ?>" class="category">
<span class="cat_title"><a href="<?php echo get_category_link($cp_pC);?>">
<?php single_cat_title(); ?></a></span>


<?php echo category_description($cp_pC); ?></p>
</div>
<?php } ?>

<?php wp_reset_query(); ?>
</div>

<?php } ?>



<div id="page" class="clearfloat">
Can anyone correct the code please?
The problem part is in bold..if i dont use this part of the code, everything works fine..:mad:
 
I maybe wrong, but try initializing $postcat = array() before it is used. also replace
if( ! is_array( $postcat ) ) {

foreach ( $cp_categories as $b ) {

$postcat[] = $b->cat_ID;

}

}

with
if( ! is_array( $postcat ) ) {

foreach ( $cp_categories as $b ) {

$postcat = $b->cat_ID;

}

}

Edit: If that doesn't work, try to supress the warnings with a @ in front of the line. If the code is correct, it might still work
 
Insert <?php $postcat = array() ?> before the line where it is used.

Also
if( ! is_array( $postcat ) ) {

@foreach ( $cp_categories as $b ) {

$postcat = $b->cat_ID;

}

}

If you get warnings, and if your script does function properly, use the @ symbol before the function where it shows warning. For example if a warning is displayed before array_slice() in a particular line, just use @array_slice()
 
Tried..Still the same thing:(

BTW, i was searching and similar errors were cause because the array had value 0 or something..does that help?
 
If the array is null, then the functions array_slice() or foreach() won't work. That is because they need data in the array. try print_r(arrayname); to display the contents of the array in a readable form. And see if anything is displayed.
 
Back
Top