• Resolved lil_bugga

    (@lil_bugga)


    This is doing my head in, I just can’t figure out why my single.php page always loads 10 posts (as set by how many posts to display per page).

    I’ve looked about online and found loads of soloutions and ideas and none seem to work for me. I’ve tried a query to set post display to 1, I’ve tried my own custom loops if $i < 0 and while have posts & $i < 0 and still can’t beat it. I know its not issues from code blocks because I can remove calls to the header, footer and sidebar and it still displays incorrectly.

    Heres my current code

    <?php
    /**
     * The Template for displaying all single posts.
     *
     * @package WordPress
     * @subpackage Bare
     * @since Bare 1.0
     */
    
     ?>
    
    <h1>Single.php</h1>
    
    <?php
    // Exclude posts with a category ID of 8 (My Work)
    query_posts('cat=-8'); 
    
    if (have_posts()) while (have_posts()) : the_post(); ?>
    
        <div id="nav-above" class="navigation">
        	<div class="nav-previous">
    			<?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '←', 'Previous post link', 'twentyten' ) . '</span> %title' ); ?>
        	</div>
       		<div class="nav-next">
    			<?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '→', 'Next post link', 'twentyten' ) . '</span>' ); ?>
            </div>
        </div><!-- #nav-above -->
    
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <h1 class="entry-title"><?php the_title(); ?></h1>
    
                <div class="entry-meta">
                    <?php posted_on(); ?>
                </div><!-- .entry-meta -->
    
                <div class="entry-content">
                    <?php the_content(); ?>
                    <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
                </div><!-- .entry-content -->
    
                <div class="entry-utility">
                    <?php posted_in(); ?>
                    <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
                </div><!-- .entry-utility -->
            </div><!-- #post-## -->
    
        <div id="nav-below" class="navigation">
        	<div class="nav-previous">
    			<?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '←', 'Previous post link', 'twentyten' ) . '</span> %title' ); ?>
            </div>
        	<div class="nav-next">
    			<?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '→', 'Next post link', 'twentyten' ) . '</span>' ); ?>
            </div>
        </div><!-- #nav-below -->
        <hr />
        </br>
    
    <?php comments_template( '', true );
    endwhile; // end of the loop. ?>

    You might notice I’m excluding posts from a set category which works on the post display themselves, but the links still pick them up, is there anyway to prevent the links picking up these posts as well as get single.php to display just one post?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Try commenting out query_posts(‘cat=-8’); I don’t know why that is there.

    Thread Starter lil_bugga

    (@lil_bugga)

    kevin taking that out does fix the issue of displaying a single post, but then causes me the issue of displaying posts I don’t want display.

    i used the query_posts(‘cat=-8’); code snippet to hide any post with a category of My Work, as I don’t want them to display with the normal blog content etc.

    What soloution would you consider using to prevent posts with a category of 8, my work, from showing?

    When someone clicks on a post in your blog they are taken to single.php. WordPress sets up the single query, and takes you to single.php. By putting in the query_posts() you are re-doing the query and you are including all posts except the category you exclude. So in effect you are making it show like a blog.

    A solution may be to make your posts private.

    do you want to show an empty single post page if someone clicks a post of category 8?

    if so:

    – remove
    query_posts('cat=-8');

    – and change the one line to:

    if (have_posts()&&!in_category(8)) while (have_posts()) : the_post(); ?>

    (totally untested)

    That’s cool alchymyth, I didn’t realize you could do that in the loop code.

    It may be better to alert your users that the post is private though.

    Would this work?

    If (have_posts()) : while (have_posts()) : the_post();
        if(!in_category(8)) {
         the_title();
         the_content()
      }else{
       <p>Sorry but this post is private</p>
      }
    endwhile;endif;
    Thread Starter lil_bugga

    (@lil_bugga)

    Ok the website I’m working on is for a client so that they can mahange the content themselves without coding.

    The way I have the website working is as follows.

    The client creates new posts as normal so they can update their blog etc. They will also have a page called My Work which lists all of the work that they have done.

    I have a custom page template and loop set up, page-mywork.php, and this is used soley to display any post which has a category of My Work.

    My aim is to prevent these posts showing up inside index.php which I worked for me when using “query_posts(‘cat=-8’);” but as kevin said above was causing my issues with single.php.

    The items within the My Work category are displayed within the My Work page, but the links have been removed so they never load, or call single.php

    My aim is to prevent these posts showing up inside index.php which I worked for me when using “query_posts(‘cat=-8’);”

    ok;
    keep the exclusion code in index.php;
    although the code as it is will make problems with pagination.

    does your theme have a single.php?
    if yes, do not add the “query_posts(‘cat=-8’);” code to single.php.
    however, @kevin’s earlier suggestion https://www.ads-software.com/support/topic/singlephp-displays-all-posts?replies=7#post-3023490 should work for single.php.

    btw:
    does the theme have an archive.php template, and category.php?
    then you might need to do the exclusion there as well.

    depends on what links and menus are used in your site.

    Thread Starter lil_bugga

    (@lil_bugga)

    Cheers guys for both of your help.

    My theme design does have both index and archive and I had already used the “query_posts(‘cat=-8’);” lines in them which work well.

    I’ve used kevins code system to show a message saying certain posts are private should they be loaded, and stumbled across the wordpress codex for the next post link function which says how to exclude posts from a certain category, which should prevent any of the posts with a category or My Work from even showing.

    So for anyone else that might read this at somepoint in the future my code for single.php is as follows.

    <?php
    /**
     * The Template for displaying all single posts.
     *
     * @package WordPress
     * @subpackage Bare
     * @since Bare 1.0
     */
    
    get_header(); ?>
    
    <h1>Single.php</h1>
    
    <?php get_sidebar(); 
    
    if (have_posts()) while (have_posts()) : the_post();
        if(!in_category(8)) { ?>
    
        <div id="nav-above" class="navigation">
        	<div class="nav-previous">
    			<?php if (!in_category(8)) {
    				previous_post_link ( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentyten' ) . '</span> %title', false, '8' );
               } else { ?>
    		   		""
               <?php } ?>
        	</div>
       		<div class="nav-next">
    			<?php if (!in_category(8)) {
    				next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'twentyten' ) . '</span>', flase, '8' );
               } else { ?>
    		   		""
               <?php } ?>
            </div>
        </div><!-- #nav-above -->
    
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <h1 class="entry-title"><?php the_title(); ?></h1>
    
                <div class="entry-meta">
                    <?php posted_on(); ?>
                </div><!-- .entry-meta -->
    
                <div class="entry-content">
                    <?php the_content(); ?>
                    <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
                </div><!-- .entry-content -->
    
                <div class="entry-utility">
                    <?php posted_in(); ?>
                    <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
                </div><!-- .entry-utility -->
            </div><!-- #post-## -->
        <hr />
        </br>
    
    	<?php comments_template( '', true ); ?>
    	    <div id="nav-below" class="navigation">
    		<div class="nav-previous">
    			<?php if (!in_category(8)) {
    				previous_post_link ( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentyten' ) . '</span> %title', false, '8' );
               } else { ?>
    		   		""
               <?php } ?>
        	</div>
       		<div class="nav-next">
    			<?php if (!in_category(8)) {
    				next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'twentyten' ) . '</span>', false, '8');
               } else { ?>
    		   		""
               <?php } ?>
            </div>
        </div><!-- #nav-below -->
    
    	<?php }else{ ?>
       <p>Sorry but this post is private</p>
      <?php }
    endwhile; ?>
    
    <?php get_footer(); ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Single.php displays all posts’ is closed to new replies.