• Resolved Jaaaarne

    (@jaaaarne)


    Hello everybody!

    I’ve tried searching but nothing came up in Codex. Please help me find a solution, it’s very important to me.

    There are two categories on my site for which I need single post pages look differently. One category is News, so each news post will only need to have a date/time. The other is Articles in which every article will need to have a source named/linked (a newspaper, magazine, website etc.), but don’t need to have a date. So, I’m using the same div container to display different information depending on a category to which the post belongs. If it’s a News post, then the date and time should be displayed, and if it’s an Article, then the source should be displayed instead (I’m using post_meta for that).

    Is there a way to do that? It’s easy with displaying different categories differently, but I found no information on displaying different posts differently. Please help me, I’ll be so grateful. 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • Use custom fields..

    http://codex.wordpress.org/Using_Custom_Fields

    Then switch the content depending on the presence of a custom field, or the value of that field…

    Thread Starter Jaaaarne

    (@jaaaarne)

    Thank you. That was not what I was asking about, but I’ve found a solution myself. 🙂

    Custom fields would handle the situation you presented, but if you have a solution then that’s what matters… 😉

    Thread Starter Jaaaarne

    (@jaaaarne)

    Well, I knew that custom fields are exactly what I needed, and I had no problem using them. They worked okay. What I had problem with was the actual switching, since I’m not that good with PHP. 🙂 While searching for the solution I finally found a Codex article describing the Variable Sidebar: http://codex.wordpress.org/Conditional_Tags and I worked from there. In the single post template in the place where I needed the content switched I put this conditional tag with categories’ id. Since there are only two categories, it was easier to do:

    <?php
    if (in_category('3')) {
            echo "<div class=\"press-meta\"> <span class=\"press-copy\"> ";
            $author = get_post_meta($post->ID, 'author', true);
    	echo($author);
    	echo " &nbsp; | &nbsp; ";
            $source = get_post_meta($post->ID, 'source', true);
    	echo($source);
            echo " </span> ";
    	the_time('d.m.Y');
            echo " </div>";
    } else {
            echo "<small> ";
    	the_time('d.m.Y');
    	echo " @ ";
    	the_time('G:i');
    	echo " ";
    	the_tags('&nbsp; | &nbsp; ', ', ', '');
    	echo " </small> <br />";
    }
    ?>

    So, if WP sees the post belongs to the Articles category, it displays the author and the name of the original publisher along with the publish date, and when it sees it’s a News post, it displays the date and the time of the news along with the post’s tags.

    If it works… 🙂

    Although i have to point out there is one small addition you could make here…

    $author = get_post_meta($post->ID, 'author', true);

    You’re not performing any check to see if this returns a value (same with $source)….

    Essentially this won’t return anything when the field doesn’t exist, but i’d do it like so….

    $author = get_post_meta($post->ID, 'author', true);
    if($author) {echo $author;}

    Not sure it’ll make a huge difference, but the current way still echo’s $author even if it doesn’t have any value…

    Also..

    echo($source) need not have brackets…
    echo $source; will work fine…

    Perhaps it might be a little easier on the eyes like so…

    <?php if(in_category('3')) : ?>
      <div class="press-meta">
        <span class="press-copy">
        <?php
        $author = get_post_meta($post->ID, 'author', true);
        $source = get_post_meta($post->ID, 'source', true);
    
        if($author) {
            echo $author;
        }
            echo ' &nbsp; | &nbsp; ';
    
        if($source) {
            echo $source;
        }
        ?>
        </span>
        <?php the_time('d.m.Y');?>
      </div>
    <?php else :?>
      <small>
        <?php the_time('d.m.Y'); ?> @ <?php the_time('G:i');?> <?php the_tags('&nbsp; | &nbsp; ', ', ', '');?>
      </small>
      <br />
    <?php endif;?>
    Thread Starter Jaaaarne

    (@jaaaarne)

    Like I said, I’m not good with PHP 🙂 My version does work for me and that’s okay. But thank you for your corrections, I’ll be glad to apply them if you don’t mind. 🙂

    Go for it… 😉

    Any problems, post back… 🙂

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Different single post view for different categories’ is closed to new replies.