WordPress.org

Forums

[resolved] Does anyone know how to hack a category page to display a designated post? (10 posts)

  1. andytford
    Member
    Posted 6 years ago #

    I've set my site up to be more like a CMS instead of a blog, and I'd like to hack the category page to display a post in order to look more like a landing page instead of an archive page.

    I'd like a particular post to be the body content of a category. I'm not a coder but would like to insert a piece of code that says:

    if this is category 6, then display post 5;
    if this is category 8, then display post 13;

    I've been able to get almost there with:

    <?php if (is_category('1')) {
    echo '<p>'; the_content('post=5'); echo '</p>';
    }
    ?>

    ...but it still displays all the content for every post in that particular category. By the way, category 1 is named Info and post 5 is named Info.

    Any help would be greatly appreciated.

    Thanks in advance ~

  2. EMG
    Forum Volunteer
    Posted 6 years ago #

    ... Why not just use Pages to do the same thing?

    Pages allow you to display a single page/post of static content rather than dynamic content and there are a few ways to display links to these pages (using the Pages widget or simply adding a Pages navigation/link block in your sidebar/navigation bar area).

  3. EMG
    Forum Volunteer
    Posted 6 years ago #

    Also! You can create custom Pages as well, which will allow you to customize how different pages look. :)

  4. andytford
    Member
    Posted 6 years ago #

    This was an oversight on my part and the site is already built (with many posts). Also, tags were a large priority. I've used pages in the past and am familiar with templates. I need the equivalent for categories and posts.

  5. EMG
    Forum Volunteer
    Posted 6 years ago #

    Ohhh okay, I see. :)

    I know that when I was first using WP, I never really realized how versatile the Pages functionality could be. With the recent upgrades, Pages are more functional than ever, which is something I've been fiddling with for my latest project. Wasn't sure if perhaps maybe you missed the Pages like I did so I thought I'd throw it out there.

    I'm not much of a php coder, either, otherwise I'd help you work out that script you have started. Hopefully someone else can help tweak the rest for you!

    Just for clarification, though, are you wanting for the links to specific categories (like say, if a post was categorized under Apples and had a link to the Apples category and the sidebar/widget bar Categories links) to link to a specific post within that Apples category?

    If such is the case, then would it be possible to script something that substitutes all the links to the category to be links to specific posts instead?

  6. kapiljain.in
    Member
    Posted 6 years ago #

    Hi,

    You can create custom pages for category, for example

    category-6.php
    if this is category 6

    category-8.php
    if this is category 8

    and write the following code to display specific post in custom pages:

    <?php $posts = query_posts('p=5');?>
    <?php if (have_posts()) :?>
    	<?php while (have_posts()) : the_post(); ?>
    		<div class="post">
    			<h1><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title() ?></a></h1>
    			<div class="entry"><?php the_content();?></div>
    		</div>
    	<?php endwhile; ?>
    <?php endif; ?>

    In the above code just replace p=5 in line no 1 with the Post ID that you want to display.

    See http://codex.wordpress.org/Template_Tags/query_posts to know more about query_posts() function of wordpress.

    I think it might help you.

    Thanks

  7. andytford
    Member
    Posted 6 years ago #

    I sort of solved this dilemma:

    <?php if (is_category('6')) {
     echo '<h2>'; query_posts(''); the_title(); echo '</h2>';
      echo '<p>'; query_posts(''); the_content(); echo '</p>';
    }
    ?>

    I'm sure there's a less sloppy way to code this, and I'm pretty sure this isn't correct on some level either. Regardless, it's working for what I need.

  8. kapiljain.in
    Member
    Posted 6 years ago #

    You may use it like that:

    <?php if (is_category('6')) {
    	dispPosts(5);
    } elseif (is_category('8')) {
    	dispPosts(13);
    }
    ?>  
    
    <?php function dispPosts($Ids) {
    	$posts = query_posts('p='. $Ids);
    	if (have_posts()) :
    		while (have_posts()) : the_post();?>
    		<div class="post">
    			<h1><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title() ?></a></h1>
    			<div class="entry"><?php the_content();?></div>
    		</div>
    	<?php endwhile;
    endif;
    } ?>

    I think it might help you.

  9. Mark / t31os
    Moderator
    Posted 6 years ago #

    Why create a function for that?

    Why not just use a variable...

    Based on your example..

    <?php
    $thisvariable = '';
    if (is_category('6')) {
    	$thisvariable = 'p=5';
    } elseif (is_category('8')) {
    	$thisvariable = 'p=13';
    }
    	query_posts( $thisvariable );
    	if (have_posts()) :
    		while (have_posts()) : the_post();?>
    		<div class="post">
    			<h1><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title() ?></a></h1>
    			<div class="entry"><?php the_content();?></div>
    		</div>
    	<?php endwhile;
    endif;
    ?>

    Functions are not worth using unless you need to reuse it, same with variables really, unless there's another use for them, then you really should just write what you need...

    Even this would be suited..

    Another based on previous example..

    <?php if (is_category('6')) {
    	query_posts('p=5')
    } elseif (is_category('8')) {
    	query_posts('p=13')
    } else {
    	query_posts();
    }
    	if (have_posts()) :
    		while (have_posts()) : the_post();?>
    		<div class="post">
    			<h1><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title() ?></a></h1>
    			<div class="entry"><?php the_content();?></div>
    		</div>
    	<?php endwhile;
    endif;
    ?>

    Avoid functions and variables UNLESS you need to reuse them, or you're just creating needless code, which potentially increases server load and page load times, although marginal, 10 functions later you may think twice... :)

  10. yaron22
    Member
    Posted 6 years ago #

    Hi how do I do this:
    When a user click a category, he gets the fisrt post of this category in a single post format (with 'next post' link)

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic