Search code examples
phpwordpressechocategoriessidebar

php inside echo tags


I am running a wordpress website and I am trying to echo out different sidebars according to different categories but I can't seem to put php code inside the echo tags. The code is working fine to echo out different content, in the sidebar, on different category pages but I just can't add php code inside echo ' ' as it's returning an error. This is my code:

<?php  if ( is_active_sidebar( "main_sidebar" ) ) :  ?>

     <div id="main_sidebar" class="widget-area">
               <?php
               if ( is_category( '7' )) {
               echo 'Sidebar to be echo'd on category page with ID=7';
               }
               else 
               {
               echo 'Sidebar to be echo'd on all the other category pages';
               }   
               ?>
     </div><!-- #first_sidebar .widget-area -->

<?php endif; ?>

and this is how I want it to function:

<?php  if ( is_active_sidebar( "main_sidebar" ) ) :  ?>

     <div id="main_sidebar" class="widget-area">
               <?php
               if ( is_category( '7' )) {
               echo '<?php dynamic_sidebar( 'category_fashion' ); ?>';
               }
               else 
               {
               echo '<?php dynamic_sidebar( 'main_sidebar' ); ?>';
               }   
               ?>
     </div><!-- #first_sidebar .widget-area -->

<?php endif; ?>

I am getting this error when using the above code:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/ignoremu/public_html/wp-content/themes/.../sidebar.php on line 6

Please help me out. Thanks.


Solution

  • The best method for you is to skip HTML in your echo completely and just output the result with the echo as such:

    <?php  if ( is_active_sidebar( "main_sidebar" ) ) :  ?>
    
         <div id="main_sidebar" class="widget-area">
                   <?php
                   if ( is_category( '7' )) {
                   echo dynamic_sidebar( 'category_fashion' );
                   }
                   else 
                   {
                   echo dynamic_sidebar( 'main_sidebar' );
                   }   
                   ?>
         </div><!-- #first_sidebar .widget-area -->
    
    <?php endif; ?>