WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Posting posts in a certain category to specific pages (5 posts)

  1. davidelliott294
    Member
    Posted 1 year ago #

    Hi,

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<h1><?php the_title(); ?></h1>
    		<?php the_content(); ?>
    	<?php endwhile; else: endif; ?>
    
    	<?php query_posts('cat=6'.get_the_title().'&post_status=publish,future');?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    	<p><?php the_content(); ?>
    	<?php endwhile; else: endif; ?>

    I'm using that code to post posts in specific categories to specific pages on my wordpress blog. It does what I want it to do but it seems to continuously loop and keep posting as your scroll down.

    Can anyone help me with this?

    Cheers

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    1. Don't use query_posts() to customise anything other than the main query. Use get_posts() or WP_Query() instead.

    2. Remember to add a posts_per_page parameter in your secondary query.

  3. davidelliott294
    Member
    Posted 1 year ago #

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<h1><?php the_title(); ?></h1>
    		<?php the_content(); ?>
    	<?php endwhile; else: endif; ?>
    
    	<?php get_posts('cat=6'.get_the_title().'&post_status=publish,future'.post_per_page="10");?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    	<p><?php the_content(); ?>
    	<?php endwhile; else: endif; ?>

    So the code looks like that? Specifically aimed at post_per_page parameter as i'm not 100% that is correctly written. I can only edit PHP.

    Wont it take the default settings from what is set in settings?

  4. davidelliott294
    Member
    Posted 1 year ago #

    Also changing query_posts to get_posts does nothing other than mess up the layout of the posts.

  5. davidelliott294
    Member
    Posted 1 year ago #

    Never mind. Sorted it.

    my code for the page now looks like this:

    <?php
    /**
     * The template for displaying all pages.
     *
     * This is the template that displays all pages by default.
     * Please note that this is the wordpress construct of pages
     * and that other 'pages' on your wordpress site will use a
     * different template.
     *
     * @package WordPress
     */
    /*
    Template Name: howto
    */
    ?>
    <?php get_header(); ?>
    <!--Start Content Wrapper-->
    <div class="grid_24 content_wrapper">
      <div class="grid_16 alpha">
        <!--Start Content-->
    
        <div class="content">
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<h1><?php the_title(); ?></h1>
    		<?php the_content(); ?>
    	<?php endwhile; else: endif; ?>
    
    	<?php query_posts('cat=6'.get_the_title().'&post_status=publish,future');?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    	<p><?php the_content(); ?>
    	<?php endwhile; else: endif; ?>
    
          <!--Start Comment box-->
          <?php comments_template(); ?>
          <!--End comment Section-->
        </div>
        <!--End Content-->
        <?php endwhile;?>
      </div>
      <!--Start Sidebar-->
      <?php get_sidebar(); ?>
      <!--End Sidebar-->
    </div>
    <!--End Content Wrapper-->
    <div class="clear"></div>
    </div>
    <!--End Container-->
    <?php get_footer(); ?>

    and it works (y).

Topic Closed

This topic has been closed to new replies.

About this Topic