lucaswe
Member
Posted 2 years ago #
Hello, i am trying to make wordpress page(a page that i create using the page.php file in the admin) show both posts and the dynamic page content.
I've so far made so it either shows the dynamic content on the page, as default for the page.php file or show the post from a specific category using : <?php query_posts('cat=5&showposts='); ?>
but i cant show both on the same page as i tried with a template file, anyone have a suggestion on hwo to do it? Ofc i could enter the text manually in the file and use the loop for the posts only but thats not a technique to prefer.
lucaswe
Member
Posted 2 years ago #
I solved it by myself by using the code below:
note: the code below shows both the page content and the posts with the right category ID in the same page.
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div></div>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
<?php query_posts('cat=5'); ?>
<div class="archive">
<ul>
<?php while (have_posts()) : the_post(); ?>
<li>
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><span class="archive-date"><?php the_time('M d Y') ?></span><?php the_title(); ?></a></h3>
</li>
<?php endwhile;?>
</ul></div>
Try this in your page template:
<?php
if ($posts) : foreach ($posts as $post) : start_wp();
//show page content
?>
<div class="post-content">
<?php the_content(__('<strong>Read more...</strong>')); ?>
</div>
<?php endforeach; else: ?>
<h1>Page not found - 404 error</h1>
<?php endif; ?>
<?php
//keep the original query
$temp_query = $wp_query;
//get the list of posts only in categorty 5
query_posts('cat=5');
?>
<?php if (have_posts()) : //loop through and display the posts ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
<?php endwhile; ?>
<?php endif; ?>
<?php
//restore the original query (optional but will save you trouble later)
$wp_query = $temp_query;
rewind_posts();
?>
haha you solved it just before I posted - both basically the same thing.
Note my use of storing and restoring $wp_query and rewind_posts() - if you have any loop code that comes after this you may need to use it.
This method works, but it enabled comments on pages that were disabled in the control panel.