• Resolved cybershot

    (@cybershot)


    I am working on coding a wp site. I have a box at the top of the page with three tabs

    1.current projets
    2.future projects
    3.what’s in my blog

    What I want is to be able to post to a category and have it show up only in the box and not in the front page or home page. So in the div for current projects, I have this

    <ul>
    <?php $recent = new WP_Query("cat=24&showposts=10"); while($recent->have_posts()) : $recent->the_post();?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark">
    <?php the_title(); ?>
    </a></li>
    <?php endwhile; ?>
    </ul>

    The idea is to paste that in the future projects and the what’s in my blog category. I wanted the post title as well as the excerpt(). But it’s posting to the homepage as well as a normal post. How do I get it to just post to the category? Do you have better code than what’s above?

Viewing 5 replies - 1 through 5 (of 5 total)
  • stvwlf

    (@stvwlf)

    You need to exclude those categories from your homepage.

    check query_posts
    https://codex.www.ads-software.com/Template_Tags/query_posts

    sometimes it is easier to build a custom page template and use it as a static home page on which you display your blog posts built from a custom query
    https://codex.www.ads-software.com/Pages#Creating_Your_Own_Page_Templates

    Thread Starter cybershot

    (@cybershot)

    I did exclude them. I am really close. Here is where I am at.

    I created a file called blog.php in that file I put this

    <?php query_posts('cat=26'); ?>
    
     <!-- Start the Loop. -->
     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
     <!-- The following tests if the current post is in category 3. -->
     <!-- If it is, the div box is given the CSS class "post-cat-three". -->
     <!-- Otherwise, the div box will be given the CSS class "post". -->
     <?php if ( in_category('3') ) { ?>
               <div class="post-cat-three">
     <?php } else { ?>
               <div class="post">
     <?php } ?>
    
     <!-- Display the Title as a link to the Post's permalink. -->
     <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
     <!-- Display the Time. -->
     <small><?php the_time('F jS, Y'); ?></small>
    
     <!-- Display the Post's Content in a div box. -->
     <div class="entry">
       <?php the_content(); ?>
     </div>
    
     <!-- Display a comma separated list of the Post's Categories. -->
     <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
     </div> <!-- closes the first div box -->
    
     <!-- Stop The Loop (but note the "else:" - see next line). -->
     <?php endwhile; else: ?>
    
     <!-- The very first "if" tested to see if there were any Posts to -->
     <!-- display.  This "else" part tells what do if there weren't any. -->
     <p>Sorry, no posts matched your criteria.</p>
    
     <!-- REALLY stop The Loop. -->
     <?php endif; ?>

    in the index.php file where I want the post to show up I put this

    <?php include('blog.php'); ?>

    Now the content box shows only the post made to the blog category, the current projects page shows only the post made to that category. the problem now is that the homepage shows only the category post as well. All the regular posts that are suppose to be there dissapeared. I used this code just before the loop in the index file for the main loop

    <?php
    if (is_home()) {
    query_posts("cat=-24,-25,-26");
    }
    ?>
    stvwlf

    (@stvwlf)

    Hi

    I don’t understand why you are including your blog.php file within index.php. Its supposed to replace index.php.

    read the page about custom page templates. Create a page and set that page to use your blog.php file as its page template. Before that will work you need to add this header to blog.php

    <?php
    /*
    Template Name: Blog
    */
    ?>

    Thread Starter cybershot

    (@cybershot)

    I know how to make custom pages. But that is not what I am trying to do here. I did finally figure it out thanks to google. just took a long time. Wasn’t sure if you could do this. I didn’t want to make a custom page. I wanted three of my categories to show up on the homepage in a custom box. There are three tabs. When you click the tab, the content for that category shows up in the box. Then you can click the title to go to the page to post your comment or do whatever, continue reading the post.
    The issue was that when I posted to a category, it would also show up on the homepage in the regular post area, don’t want that. The fix came when I found a site showing how to make your own query post.

    <?php $recentPost = new WP_Query();
    $recentPost->query('cat=26&showposts=3');
    
    ?>
    
    <?php while ($recentPost->have_posts()) : $recentPost->the_post(); ?>
    <h3 id="proj-title"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></h3></a>
    <?php the_excerpt(); ?>
    <a class="detail" href="<?php the_permalink() ?>">continue reading</a>
    <?php edit_post_link('Edit', '<p>', '</p>'); ?>
    <?php endwhile; ?>

    Now with this code, I could say show only category 26 and only show three titles and just the_excerpt() of the post. Then there is a button that says read more that you can click. I put this code in three files. blog.php, current-projects.php, and future-projects.php then I was able to call them from within the content box by just doing

    `<?php include (‘blog.php’); ?>

    Now when I click the tab, the category shows up that I want to see and nothing else.

    Thread Starter cybershot

    (@cybershot)

    EDIT

    The only thing left to do now is figure out how to get the next and previous buttons working for older and newer posts. When I tried to add them into that code, it crashed. I don’t know why. it errors out on an endif.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘how to display category post only’ is closed to new replies.