Support

Account

Home Forums General Issues Category Page

Solved

Category Page

  • I am using the Advanced Custom Fields plugin to pull data into specific spots in my template and allow the client to change this info from the dashboard. I am running into an issue in my category.php page template for categories. I have a banner file that contains the following code:

    <?php $posts = get_posts(array(
                	'post_type' => 'siteinfo',
                    'posts_per_page' => -1,
                    'orderby'=> 'ID',
                    'order' => 'ASC'
                  ));
    			  		  
                  if($posts) { 
                  	foreach($posts as $post)
                      setup_postdata($post);
                      { 
    				  $phone = get_field('phone_number');
    			  ?>			
            <a class="phone-btn"  href="tel:1-<?php echo $phone; ?>"><img class="phone-icon" src="<?php bloginfo('template_directory'); ?>/images/white-phone.png" alt="menu" /><?php echo $phone; ?></a>
            </div>
            <div class="mobile-menu visible-phone visible-tablet"><a id="responsive-menu-button" href="#sidr-main"><img class="menu-icon" src="<?php bloginfo('template_directory'); ?>/images/menu-icon.png" alt="menu" />Main Menu</a></div>
     </div>
    
    </div><!--end.container-->
    <div class="mobile-back"></div>
    
    </header>
    
    <div class="phone-google">
    <div class="container">
    
    				<div class="phone">
    				    <p><img class="phone-img" src="<?php bloginfo('template_directory'); ?>/images/phone.png" alt="<?php bloginfo('name'); ?>" />
    	                
    	                <span><?php echo $phone; ?></span></p>
    	            </div><!--end.phone-->
    	      	
               <?php } wp_reset_postdata();
    			}?>

    This pulls info from a custom post type into my template and works great. Problem is when I have this piece of code in the template my code that pulls in all posts from a specific category doesn’t work.

    Here is my code that pulls in the posts from a category (this code is after the above code):

    <section id="content" role="main">
                       
                    
                    
                    
                    
                    
                    
                    
                       <h1>Category: <?php single_cat_title() ?></h1>
    
                       <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                           <div class="entry-content">                               
                              <div class="title">
                                  <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                  <p class="written"><?php the_time('l, F, jS, Y'); ?> at <?php the_time(); ?> by <?php the_author(); ?></p>
                              </div><!-- end title -->
                              <div class="like-btns">
                                  <iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&layout=button_count&show_faces=false&width=450&action=like&font=arial&colorscheme=light&height=21" style="border:none; overflow:hidden; background-color:transparent; width:80px; height:21px;"> </iframe>
                                  
                                  <a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
                                  
                                  <!-- Place this tag where you want the +1 button to render. -->
                                  <div class="g-plusone" data-size="medium"></div>
                                  
                                  <!-- Place this tag after the last +1 button tag. -->
                                  <script type="text/javascript">
                                    (function() {
                                      var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
                                      po.src = 'https://apis.google.com/js/plusone.js';
                                      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
                                    })();
                                  </script>
                              </div><!-- end like-btns -->
    
                          <p class="cat-tag-links"><span class="cat-tag">Categories:</span> <?php the_category(', '); ?> <?php the_tags('<span class="cat-tag">Tags: </span>',', '); ?></p>
                          
                          <?php css_excerpt('css_excerptlength_blog', 'css_excerptmore'); ?>
                          
                          </div><!-- end entry-content -->
                          <div class="divider"></div>
                          
                          <?php endwhile; else: ?>
              
                          <?php endif; ?>
                          
                          <?php //Reset Query
                          wp_reset_query(); ?>
                      		<div class="clearboth"></div>
                          <?php 
                          if (function_exists('my_pagination')) {
                              my_pagination();
                          }
                      ?>
                    
                </section><!-- end #content -->

    Here is a category page: http://senroctech.com/category/categorytest/

    When I comment out the code that pulls in the info from the “Site info” custom post type the page works fine.

    Does anyone know what I am doing wrong and how I can fix this?

    Thanks!

  • Hi @mccawphoto

    The issue may be related to using the setup_postdata($post); function.

    The best way to fix your issue is to reduce your code to the bare skeleton. Simply load and loop over the data (no setup_postdata, etc). Does the page work if this is all you do?

    Thanks
    E

  • I tried a couple of things but neither worked. First I just removed the setup_postdata($post); from the code but in doing that the page looked the same.

    Next I tried the following code:

    <header>
    
    <!--logo/nav/social-->
    <div class="container">
    
    <!--logo-->
    	<a class="logo" href="<?php bloginfo('url'); ?>">
    		<img class="logo" src="<?php bloginfo('template_directory'); ?>/images/logo.png" alt="<?php bloginfo('name'); ?>" />
    	</a>
    	
            
     <?php include(TEMPLATEPATH . '/nav.php'); ?>
     
     <div class="social">
         <a class="facebook" href="#" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/facebook.png" alt="<?php bloginfo('name'); ?>" /></a>
    	 <a class="twitter" href="#" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/twitter.png" alt="<?php bloginfo('name'); ?>" /></a>
    	 <a class="youtube" href="#" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/youtube.png" alt="<?php bloginfo('name'); ?>" /></a>
         <a class="linkedin" href="#" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/linkedin.png" alt="<?php bloginfo('name'); ?>" /></a>
     </div><!--end.social-->
     
    <div class="clear"></div>
    
     <!--mobile header-->
     <div class="phone-nav visible-phone visible-tablet">
            <div class="mobile-phone visible-phone visible-tablet">
            
            <?php $posts = get_posts(array(
                	'post_type' => 'siteinfo',
                    'posts_per_page' => -1,
                    'orderby'=> 'ID',
                    'order' => 'ASC'
                  ));
    			  		
    	if ( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post();  
                         
    	$phone = get_field('phone_number'); ?>
    			
            <a class="phone-btn"  href="tel:1-<?php echo $phone; ?>"><img class="phone-icon" src="<?php bloginfo('template_directory'); ?>/images/white-phone.png" alt="menu" /><?php echo $phone; ?></a>
            </div>
            <div class="mobile-menu visible-phone visible-tablet"><a id="responsive-menu-button" href="#sidr-main"><img class="menu-icon" src="<?php bloginfo('template_directory'); ?>/images/menu-icon.png" alt="menu" />Main Menu</a></div>
     </div>
    
    </div><!--end.container-->
    <div class="mobile-back"></div>
    
    </header>
    
    <div class="phone-google">
    <div class="container">
    
    				<div class="phone">
    				    <p><img class="phone-img" src="<?php bloginfo('template_directory'); ?>/images/phone.png" alt="<?php bloginfo('name'); ?>" />
    	                
    	                <span><?php echo $phone; ?></span></p>
    	            </div><!--end.phone-->
                
       <?php endwhile; // end of the loop. 
    endif; ?>
                    
                    <div class="g-plusone" data-size="large"></div>
                                        
    <!-- Place this tag after the last +1 button tag. -->
    <script type="text/javascript">
    (function() {
       var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
       po.src = 'https://apis.google.com/js/plusone.js';
       var s = document.getElementsByTagName('script')[0];   s.parentNode.insertBefore(po, s);
    })();
    </script>
                                        
               
                    
    </div><!--end.container-->
    </div><!--end.phone-google-->

    But that made everything below the navigation disappear.

    Can you see anything else I may be doing wrong?

    Thanks!

  • Hi @mccawphoto

    When you say But that made everything below the navigation disappear. do you mean that the page broke? This is most likely a PHP error.

    You can debug this error by turning on DEBUG_MODE in your wp-config.php file.

    You are posting too much code above for me to see any potential issues. Please reduce your code to the bare skeleton to determine why any errors are happening.

    Use a simple debugging technique like:

    <?php 
    
    echo '<pre>';
    	print_r($variable);
    echo '</pre>';
    die; ?>

    on each line to view variable data and see visually that your code made it to that line without breaking.

    Thanks
    E

  • Yes, the page breaks after the navigation at the top of the page.

    Now I have this error showing up where the page breaks:

    “Fatal error: Call to a member function have_posts() on a non-object in /home/senroctech/public_html/wp-content/themes/Senroc/banner-cat.php on line 66”

    Here is my code where the page breaks:

    <?php $posts = get_posts(array(
                	'post_type' => 'siteinfo',
                    'posts_per_page' => -1
                  ));
    	$phone = get_field('phone_number');	  
    if ( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post();		  
    
    <div class="phone">
       <p><img class="phone-img" src="<?php bloginfo('template_directory'); ?>/images/phone.png" alt="<?php bloginfo('name'); ?>" /><span><?php echo $phone; ?></span></p>
    </div><!--end.phone-->
                
       <?php endwhile;  
    endif; ?>
  • The end of php ?> after your loop does not appear in your code…

  • I fixed that but I am still getting this error:
    Fatal error: Call to a member function have_posts() on a non-object in /home/senroctech/public_html/wp-content/themes/Senroc/banner-cat.php on line 66

    line 66 is this line of code:
    if ( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post(); ?>

  • Hi @mccawphoto

    The code:

    <?php $posts = get_posts(array(
                	'post_type' => 'siteinfo',
                    'posts_per_page' => -1
                  ));
    	$phone = get_field('phone_number');	  
    if ( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post();	

    Is not valid.

    Please read the get_posts documenation to understand how to loop over the posts.
    Perhaps you intended to use WP_Query instead? Please research this as well.

    Thanks
    E

  • I was able to fix it!

    I changed my code to:

    <?php $args = array( 'posts_per_page' => -1, 'post_type' => 'siteinfo' );
                $myposts = get_posts( $args );
                foreach ( $myposts as $post ) : setup_postdata( $post ); 
                    $phone = get_field('phone_number'); ?>
    <div class="phone">
         <p><img class="phone-img" src="<?php bloginfo('template_directory'); ?>/images/phone.png" alt="<?php bloginfo('name'); ?>" /><span><?php echo $phone; ?></span></p>
    </div><!--end.phone-->
    <?php endforeach; 
    wp_reset_postdata();?>

    Thanks for your help!

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Category Page’ is closed to new replies.