WordPress.org

Ready to get started?Download WordPress

Forums

Category ID not being recognized in query_posts (3 posts)

  1. Maxaud
    Member
    Posted 4 years ago #

    I'm using the following page template to pull in some posts at the end of a page using a custom field called 'categoryid'. Similar code is working on a different site but not on this one for some reason.

    <?php
    /*
    Template Name: Category Articles Page
    */
    ?>
    <?php get_header(); ?>
    		<div id="container">
    			<div id="right-container">
    				<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    					<div class="post">
    						<h2><?php the_title(); ?></h2>
    						<?php if(!is_paged()) { ?>
    							<?php the_content('Read the rest of this page &raquo;'); ?>
    							<?php edit_post_link('edit', '<p>', '</p>'); ?>
    						<?php } ?>
    						<?php
    							$categoryid = get_post_meta($post->ID, 'categoryid', $single = true);
    						?>
    					</div>
    				<?php endwhile; endif; ?>
    				<?php
    					$queryarg = array(
    						'posts_per_page' => 20,
    						'cat' => $categoryid,
    						'paged'=>$paged,
    					);
    					query_posts($queryarg);
    				?>
    				<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    					<!-- do some things here for each post -->
    				<?php endwhile; endif; ?>
    				<div class="navigation">
    					<?php posts_nav_link(' · ', 'previous page', 'next page'); ?>
    				</div>
    			</div>
    			<div id="left-container">
    				<?php get_sidebar(); ?>
    			</div>
    		</div>
    <?php get_footer(); ?>
  2. PBP_Editor
    Member
    Posted 4 years ago #

    It may be easier to pull in posts using a tag

    <?php query_posts('tag=yourtaghere');
    while (have_posts()) : the_post();
    ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <br />
    <?php the_content('Read the rest of this page ยป'); ?>
    <?php endwhile; ?>
  3. Maxaud
    Member
    Posted 4 years ago #

    Thank you for the suggestion but that doesn't accomplish what I'm trying to do.

    I'm defining a category to list recent posts at the end of a page from that defined category.

    I need it to be defined by the use of a custom field from the write post page.

    This way a new page template doesn't have to be created for each category/tag. This also allows the users of the site to add more categories and list them easily.

Topic Closed

This topic has been closed to new replies.

About this Topic