WordPress.org

Forums

Code Hack (9 posts)

  1. stiwdio
    Member
    Posted 1 year ago #

    Hi Guys

    I have a theme Im working on, heres the code for the single post category;

    if ( have_posts() ) {
    	while ( have_posts() ) {
    		the_post();
    		//get all the page data needed and set it to an object that can be used in other files
    		$pexeto_page=array();
    		$pexeto_page['sidebar']=( 'Blog' );
    		$pexeto_page['slider']='none';
    		$pexeto_page['layout']=pexeto_option( 'post_layout' );
    
    		//include the before content template
    		locate_template( array( 'includes/html-before-content.php' ), true, true );
    
    		$pexeto_page['sidebar']=( 'Play' );
    
    		//include the post template
    		locate_template( array( 'includes/post-template.php' ), true, false );
    
    		//include the comments template
    		comments_template();
    	}
    }

    Where it defines what sidebar to use
    $pexeto_page['sidebar']=( 'Play' );

    Would it be possible for it to check the category of the post and if, for instance the category ID is 12 then $pexeto_page['sidebar']=( 'Play' ); otherwise use

    $pexeto_page['sidebar']=( 'Another Sidebar' );

    Would anyone know if this is possible and how to format it?

    Thanks for your help
    Darren

  2. esmi
    Forum Moderator
    Posted 1 year ago #

  3. stiwdio
    Member
    Posted 1 year ago #

    Hi esmi

    Thanks for that, I've read up on it; and come up with this now; but don't think it will work, as I now have a php within a php comment if that makes sense;

    <?php
    /**
     * Single Post Template - this is the template for the single blog post.
     */
    
    get_header();
    
    if ( have_posts() ) {
    	while ( have_posts() ) {
    		the_post();
    		//get all the page data needed and set it to an object that can be used in other files
    		$pexeto_page=array();
    		$pexeto_page['sidebar']=( 'Blog' );
    		$pexeto_page['slider']='none';
    		$pexeto_page['layout']=pexeto_option( 'post_layout' );
    
    		//include the before content template
    		locate_template( array( 'includes/html-before-content.php' ), true, true );
    
    <?php 
    
     if ( in_category( ‘play )) { 
    
    $pexeto_page['sidebar']=( 'Play' );
    
     // They have long trunks…
    } elseif ( in_category( ‘blog' )) { 
    
    $pexeto_page['sidebar']=( ‘Blog' );
    
    } else { 
    
    $pexeto_page['sidebar']=( ‘Careers );
    
    }
    ?>		
    
    		//include the post template
    		locate_template( array( 'includes/post-template.php' ), true, false );
    
    		//include the comments template
    		comments_template();
    	}
    }
    
    //include the after content template
    locate_template( array( 'includes/html-after-content.php' ), true, true );
    
    get_footer();   ?>

    I now get a parse error, could dou show me the error of my way? thank you in advance

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Just use:

    if ( in_category( ‘play )) {
    $pexeto_page['sidebar']=( 'Play' );
    } elseif ( in_category( ‘blog' )) {
    $pexeto_page['sidebar']=( ‘Blog' );
    } else {
    $pexeto_page['sidebar']=( ‘Careers );
    }

    I'd also suggest that you review how to use opening and closing PHP tags: http://uk3.php.net/manual/en/language.basic-syntax.phptags.php

  5. stiwdio
    Member
    Posted 1 year ago #

    Hi esmi
    Thanks for that; Ive added that;

    <?php
    /**
     * Single Post Template - this is the template for the single blog post.
     */
    
    get_header();
    
    if ( have_posts() ) {
    	while ( have_posts() ) {
    		the_post();
    		//get all the page data needed and set it to an object that can be used in other files
    		$pexeto_page=array();
    		$pexeto_page['sidebar']=( 'Blog' );
    		$pexeto_page['slider']='none';
    		$pexeto_page['layout']=pexeto_option( 'post_layout' );
    
    		if ( in_category( ‘play )) {
       $pexeto_page['sidebar']=( 'Play' );
    } elseif ( in_category( ‘blog' )) {
    $pexeto_page['sidebar']=( ‘Blog' );
    } else {
    $pexeto_page['sidebar']=( ‘Careers );
    }
    
    		//include the before content template
    		locate_template( array( 'includes/html-before-content.php' ), true, true );
    
             	//include the post template
    		locate_template( array( 'includes/post-template.php' ), true, false );
    
    		//include the comments template
    		comments_template();
    	}
    
    }
    
    //include the after content template
    locate_template( array( 'includes/html-after-content.php' ), true, true );
    
    get_footer();   ?>

    But get an error: unexpected T_CONSTANT_ENCAPSED_STRING

    Is it because I can't use the if statement within the loop thats already there? its frying my brain a little - I'm no wordpress expert as you can tell :-)

    Thanks
    Darren

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    Is it because I can't use the if statement within the loop thats already there?

    No. Where is the rest of the error message?

  7. stiwdio
    Member
    Posted 1 year ago #

    it just says this;

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/stiwocom/domains/stiwdio.com/public_html/stiwdio/wp-content/themes/Stiwdio/single.php on line 20

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    Knowing the line number is pretty critical. ;-)

    $pexeto_page['sidebar']=( ‘Blog' );

    The opening single quote is actually a curly quote. These quote characters are not allowed in PHP. Change that line to:

    $pexeto_page['sidebar']=( 'Blog' );

    And check all of your other single quotes too.

  9. stiwdio
    Member
    Posted 1 year ago #

    think I fixed it esmi, just a few rouge ' ' thank you for all your help and teaching me also :-)

    Darren

Topic Closed

This topic has been closed to new replies.

About this Topic