WordPress.org

Forums

If Else Statement on single.php (5 posts)

  1. Design Locker
    Member
    Posted 1 year ago #

    Hello,

    Using child them of gridster-lite theme.

    Trying to load two different post types. If category id = 8 load standard post type, else load new post type. My single.php in my child theme looks like this;

    <?php
    /**
     * The Template for displaying all single posts.
     *
     * @package Gridster
     */
    
    get_header(); ?>
    
    <?php get_sidebar(); ?>
    
    <?php while ( have_posts() ) : the_post(); ?>
    
    <?php if (is_category( '8' )) {
     get_template_part( 'content', 'single' );
    } else {
     get_template_part( 'content', 'property' );
    }
    ?>
    
    <?php endwhile; // end of the loop. ?>
    
    <?php get_footer(); ?>

    It loads the standard post type (content-single.php) no problem but when the new post type page loads it loses all formatting (not loading header, sidebar, footer, etc). I created an exact copy of the content-single.php and named it content-property.php and placed it in my child theme.

    Standard Post: http://www.irishlandsales.ie/wip/2014/03/a-normal-post/

    New Post Type: http://www.irishlandsales.ie/wip/2014/03/bulgaden-castle/

  2. Viamultimedia
    Member
    Posted 1 year ago #

    Hello,

    get_template_part( 'content', 'property' );

    Replace width
    get_template_part( 'content', get_post_format() );

    Tony

  3. Design Locker
    Member
    Posted 1 year ago #

    Hi Viamultimedia,

    Thanks for the reply, made the change you suggested and the same issue unfortunately. single.php is now as follows;

    <?php
    /**
     * The Template for displaying all single posts.
     *
     * @package Gridster
     */
    
    get_header(); ?>
    
    <?php get_sidebar(); ?>
    
    <?php while ( have_posts() ) : the_post(); ?>
    
    <?php if (is_category( '8' )) {
     get_template_part( 'content', 'single' );
    } else {
     get_template_part( 'content', get_post_format() );
    }
    ?>
    
    <?php endwhile; // end of the loop. ?>
    
    <?php get_footer(); ?>
  4. Design Locker
    Member
    Posted 1 year ago #

    Found my way around it. Created a new post template and installed the Custom Post Templates plugin to call the new template when required.

  5. alchymyth
    Forum Moderator
    Posted 1 year ago #

    generally, to check for a category in single.php, use

    in_category()

    http://codex.wordpress.org/Function_Reference/in_category

    http://codex.wordpress.org/Conditional_Tags

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.