• Resolved Filia Libri

    (@filia-libri)


    Hey,

    I’m trying to figure out how to make individual designs for single posts in different categories.
    What I mean is that I have like ten categories (id 1-10) and want that, if I view a single post from category 1, the sidebar no. 1 appears and the prev and next post links link to posts that are also in category 1 while if displaying a post from category 2,3 or 8 the links should link to the previous and next post that belongs to one of those categories and that no sidebar gets displayed. And if I view a post that belongs to any other category the links should also be accordingly and sidebar no. 2 should be displayed.
    I have no idea how to accomplish that, everything I tried out ended with either a syntax error or a time out…
    It would be great if anyone has understood what I mean and could provide me with a helpful code 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • You want to edit the single.php or the sidebar.php depending on where these links are located.
    create a section that query’s the cat:
    <div id=”show-links”>
    <!–php magic–>
    </div>
    Is_category
    `
    <?php
    if ( is_category(‘1’) { ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <p><?php the_title(); ?></p>
    <?php endwhile; endif; ?>
    <?php }
    else if( is_category(‘2’)){ ?>
    <!–Have Fun–>
    <?php } ?>

    You want to edit the single.php or the sidebar.php depending on where these links are located.
    create a section that query’s the cat:
    <div id=”show-links”>
    <!–php magic–>
    </div>
    Is_category

    <?php
    if ( is_category('1') { ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <p><?php the_title(); ?></p>
    <?php endwhile; endif; ?>
    <?php }
    else if( is_category('2')){ ?>
    <!--Have Fun-->
    <?php } ?>

    Not sure if that’s exactly right but puts you in the right direction

    Thread Starter Filia Libri

    (@filia-libri)

    Yeah, that’s what I tried… I edited the single.php but it didn’t work. I didn’t even try to do it with three different ifs (which I would need) because two already turned out impossible and didn’t show anything but fatal errors and script time outs 🙁

    I had to do that with is_page for a website give it a try, should work just fine

    Thread Starter Filia Libri

    (@filia-libri)

    Won’t work:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<div id="single-reviewpost">
    			<?php if ( is_category('&cat=348, 347, 353, 408, 296, 215') ) { ?>
    					<?php get_template_part( 'loop-single', 'single' ); ?>
    					<?php get_sidebar( 'review' ); ?>
    	</div><div id="single-newspost">
    			<?php } else if ( is_category('&cat=540') ) { ?>
    					<?php get_template_part( 'loop-single', 'single' ); ?>
    	</div><div id="single-blogpost">
    			<?php } else if ( is_category('&cat=-540, -348, -347, -353, -408, -296, -215') ) { ?>
    					<?php get_template_part( 'loop-single', 'single' ); ?>
    					<?php get_sidebar( 'blog' ); ?>
    	</div><?php } ?>
    <?php endwhile; endif; ?>

    Won’t work either:

    <div id="single-reviewpost">
    <?php if ( is_category('&cat=348, 347, 353, 408, 296, 215') ) { ?>
    			<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    					<?php get_template_part( 'loop-single', 'single' ); ?>
    					<?php get_sidebar( 'review' ); ?>
    			<?php endwhile; endif; ?>
    	</div><div id="single-newspost">
    <?php } else if ( is_category('&cat=540') ) { ?>
    			<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    					<?php get_template_part( 'loop-single', 'single' ); ?>
    			<?php endwhile; endif; ?>
    	</div><div id="single-blogpost">
    <?php } else if ( is_category('&cat=-540, -348, -347, -353, -408, -296, -215') ) { ?>
    			<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    					<?php get_template_part( 'loop-single', 'single' ); ?>
    					<?php get_sidebar( 'blog' ); ?>
    			<?php endwhile; endif; ?>
    	</div><?php } endif; ?>

    Both won’t show any errors but will simply display nothing…
    Most likely what I tried isn’t even possbile but I don’t have a clue…

    Thread Starter Filia Libri

    (@filia-libri)

    Oh, I got it 😀
    This seems to work just fine:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<div id="single-reviewpost">
    			<?php if ( query_posts($query_string . '&cat=348, 347, 353, 408, 296, 215') ) { ?>
    					<?php get_template_part( 'loop-single', 'single' ); ?>
    					<?php get_sidebar( 'review' ); ?>
    	</div><div id="single-newspost">
    			<?php } else if ( query_posts($query_string . '&cat=540') ) { ?>
    					<?php get_template_part( 'loop-single', 'single' ); ?>
    	</div><div id="single-blogpost">
    			<?php } else if ( query_posts($query_string . '&cat=-540, -348, -347, -353, -408, -296, -215') ) { ?>
    					<?php get_template_part( 'loop-single', 'single' ); ?>
    					<?php get_sidebar( 'blog' ); ?>
    	</div><?php } ?>
    <?php endwhile; endif; ?>

    I’m sure there are more elegant ways to do this but I’m happy that I got it to work out at all so for now I’m happy with this 😀

    LOL yeah its not always pretty but sometimes those ugly hacks are needed to get projects finish. i say get it out first then prioritize making it better

    That may be a bit cleaner, you only need to exit the php to execute the html

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div id="single-reviewpost">
    <?php if ( query_posts($query_string . '&cat=348, 347, 353, 408, 296, 215') ) {
    get_template_part( 'loop-single', 'single' );
    get_sidebar( 'review' ); ?>
    </div><div id="single-newspost">
    <?php } else if ( query_posts($query_string . '&cat=540') ) {
    get_template_part( 'loop-single', 'single' ); ?>
    </div><div id="single-blogpost">
    <?php } else if ( query_posts($query_string . '&cat=-540, -348, -347, -353, -408, -296, -215') ) {
    get_template_part( 'loop-single', 'single' );
    get_sidebar( 'blog' ); ?>
    </div><?php }endwhile; endif; ?>

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Individual design for single posts in different categories’ is closed to new replies.