• Hi there,

    I have defined a set of categories on my site, and I wish to post a PAGE that explains these categories. However, I would like the page to only show the text IF there are any posts for those categories.

    So, as long as I haven’t written any posts and marked them as CategoryOne, the description for this text will be hidden. But as soon as I publish a blogentry with that category, it is shown.

    I’m thinking (with no WP-coding knowledge, so this is just mashup-code) something like this:

    [IF WP-doesexist-check(‘CategoryOne’]
    {
    Category One (title)
    This is the first category, and this is some text explaining it – and this text is only visible if there are any blogentries marked with Category One
    }

    [IF WP-doesexist-check(‘CategoryTwo’]
    {
    Category Two (title)
    This is the second category, and this is some text explaining it – and this text is only visible if there are any blogentries marked with Category Two
    }

    Is this at all possible, or does there exist any plugins that will enable me to do something like this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Using get_posts to test for 1 or more posts in a given category might work.

    If you’re just wanting to list categories on a “page” with descriptions, then i think you’re after a simple page template that prints out the category names and descriptions.

    Save this as a PHP file, put it in your theme folder, then attach it to the page you want to show the list on.

    <?php
    /*
    Template Name: Category Data
    Description: Does what you asked
    */
    get_header();
    $categories = get_terms( 'category' );
    
    if( $categories ) {
    ?>
    	<!-- HTML can go here -->
    	<?php
    	foreach( $categories as $category ) {
    		?>
    		<div class="examplediv">
    			<?php esc_attr_e( $category->name ); ?><br /><?php esc_attr_e( $category->description ); ?>
    		</div>
    		<br />
    		<?php
    	}
    	?>
    	<!-- HTML can go here -->
    <?php
    }
    //get_sidebar(); // uncomment the start of this line if you need to include the sidebar
    get_footer();

    See if that’s what you’re after…

    Thread Starter walking-about

    (@walking-about)

    Yeah, that’s pretty much it 😀 Great man. But, as you can see, it doesn’t follow the design, so the page looks all skewed up. Do you know how to get around this?

    Great work man, thanx alot for the help!

    Thread Starter walking-about

    (@walking-about)

    Ok, I’ve just had a blast with some of the code from the other php-files, and now it looks quite good. But I do still have one question, if it’s ok.

    I have created a page, and attached your code as a Template to it. On other pages, where I have done the same (contact-page for instance), the page still shows the text I wrote onto the Page in WordPress. But this one doesn’t show that, so I have to hardcode it in the php-file.

    And I have this snippet in there, which I would think should solve it;

    <div class=”entry”>
    <?php the_content(); ?>
    </div>

    Or am I very wrong here?

    Add a regular loop where you want it..

    <?php if( have_posts() ) : ?>
    	<?php while( have_posts() ) : the_post(); ?>
    		<div class="entry">
    			<?php the_content(); ?>
    		</div>
    	<?php endwhile; ?>
    <?php endif; ?>

    … stick it in place of one of the comments <!-- HTML can go here --> depending on whether you want it before or after the category listing…

    I have a line at my single post page that i want to hide, i tried to delete it but then the page just dissapear.

    The line is:

    <div class=”entry-meta”>
    <?php twentyten_posted_on(); ?>
    </div><!– .entry-meta –>

    (It shows the post date)

    Is there a code to rap around to hide it?

    Pleeease help me.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Hide text based on the existance of category-posts?’ is closed to new replies.