This is what I have so far. Unfortunately this grabs ALL my pages' titles and content including sub-pages. I only want to display top-level main pages. How can I do this?
<?php query_posts('post_type=page'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
*Note: I want to display both title and content for the pages. Not just title. Otherwise I would use:
<?php wp_list_pages('depth=1'); ?>
UPDATE: Following konsolenfreddy's advice I was able to loop through my pages properly. However a new problem arose. The content is being striped of its tags. Anyway I can keep them? Here's my new code:
<?php
$pages = get_pages('parent=0');
foreach ($pages as $pagg) {
$option .= $pagg->post_title;
$option .= $pagg->post_content;
echo $option;
}
?>
You can use get_pages() (see http://codex.wordpress.org/Function_Reference/get_pages ), it takes the same arguments as wp_list_pages()
Does this work?
<?php query_posts('post_type=page&post_parent='.$parent);?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
Related
I have a simple custom page template with a simple loop that displays links to posts of the same category ID=11.
However, the problem is that although the links are working correctly, all posts are displaying the same content (the content of the first post). I can't work out why this is. Any help would be really appreciated, thanks.
Here is the loop on the custom page template
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
else:
// no posts.
endif;
?>
And here is what I have on single.php
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
You should call the_post() in single.php before doing anything else.
Try this:
<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
Leave your other code alone. It looks like it should be working as expected.
Worked it out through some trial and error. I had a list of post titles in the sidebar and needed to use wp_reset_query.
In single.php use the following code to get the content and title.
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
use this in your custom page, i used wp_reset_postdata();
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
wp_reset_postdata();
else:
// no posts.
endif;
?>
And on single.php use this
<?php
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
?>
To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});
I have a static wordpress front page. I want the code that works on my regular blog template to work on the homepage too.
Currently, it only shows one post. The code in the main index.php is this...
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
content.php calls this....
<div class="col-md-4">
<?php the_excerpt(); ?>
</div><!-- /col-md-4 -->
This works great while on the blog page. But i can't get this exact same content on to the static home page. I know I need a different query for homepage but no idea what that is. Any help appreciated!
Your code looks right to me, you can try to reset the loop and query the posts again.
<?php
rewind_posts();
query_posts( $args );
while ( have_posts() ) : the_post();
?>
<!-- Do stuff... -->
<?php endwhile; ?>
global $query_string;
query_posts($query_string.'&post_type=post&posts_per_page=15');
Adding this on top of your code should work. Try it out.
I'm just learning to write WP templates [with Pods] and I think there is a more efficient way to code the if/while loops in the example below...? ie: Chaining the while?
An explanation EDIT: and Code Example would be appreciated in helping my learning curve. Thnx.
<?php
/*
Template Name: myPage Template
*/
get_header();
$mypod = pods('my_events');
$mypod->find('name ASC');
?>
<div>
<?php while ( $mypod->fetch() ) : ?>
<?php
// set our variables
$snippet= $mypod->field('event_snippet');
?>
<p><?php echo $snippet; ?></p>
<?php endwhile; ?>
<?php if ( have_posts() ) : while ( have_posts() ) :the_post(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; endif; get_footer(); ?>
What I am thinking can be done simular to [but anything I try breaks]:
<?php
/*
Template Name: myPage Template
*/
get_header();
if ( have_posts() ) : while ( $mypod->fetch() ) : ( have_posts() ) :the_post();
$mypod = pods('my_events');
$mypod->find('name ASC');
$snippet= $mypod->field('event_snippet');
?>
<div>
<p><?php echo $snippet; ?></p>
<?php the_content(); ?>
</div>
<?php endwhile; endif; get_footer(); ?>
I'm using this code to show my 10 homepage posts:
<?php query_posts('category_name=homepage&showposts=10&order=DESC');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
I would like somehow to identify the first post and change the code only for the first post,
For example first post should show me the_excerpt instead of the_title like this:
<?php query_posts('category_name=homepage&showposts=10&order=DESC');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_excerpt(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
Use a variable to count the number of posts you're looping through. If the counter is 0, you're on the first post:
<?
query_posts('category_name=homepage&showposts=10&order=DESC');
$i = 0;
while ( have_posts() ) : the_post();
$i == 0 ? the_excerpt() : the_title();
the_content();
$i++;
endwhile;
?>
You can set a counter that increments each loop.
Start it by 1
$count = 1;
each loop do $count++;
if ($count ==1){the_excerpt();} else{the_content();}