I'm working on a custom page template that display child pages in a parent page with pagination.
It is working, but the header, and footer are not properly displayed. But when I removed the PHP file of the custom page. The header and footer are back to normal.
What's wrong with my PHP code?
Here's the snippet:
<?php
$ids = array();
$pages = get_pages("child_of=".$post->ID);
if ($pages){
foreach ($pages as $page){
$ids[] = $page->ID;
}
}
$paged = (get_query_var("paged")) ? get_query_var("paged") : 1;
$args = array(
"paged" => $paged,
"post__in" => $ids,
"posts_per_page" => 5,
"post_type" => "page"
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();
?>
<div>
<h3><?php the_title(); ?></h3>
<div><?php the_date(); ?></div>
<p><?php the_content(); ?></p>
</div>
<?php endwhile; ?>
<?php endif;
paging_nav();
wp_reset_query();
?>
Related
I want to create custom page to show list title post by tag.
Example:
This title post by tag "handphone"
Title post
Title post
Title post
...etc
Any have code for this problem?
Create a template for your page
write the postcode below into the template
<?php
$original_query = $wp_query;
$wp_query = null;
$args=array('posts_per_page'=>5, 'tag' => $brand_name);
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
?>
after that assigne the template from admin end to your page and lets see it's wwork or not .
Create a php file in your theme directory. You can give any name. And use code something like this. Replate tag_name by your desire tag. And then create a page. Set the template. You will see the list.
<?php
/**
* Template Name: Title by Tag
*
*/
get_header();
$args=array('posts_per_page'=>5, 'tag' => 'tag_name');
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
?>
<li>
<?php the_title(); ?>
</li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Create page and add below code to your page
<?php
/**
* Template Name: Get title by tag
*
*/
get_header();
$tagname = 'handphone';
$wp_query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => 'your-posttype', // or 'any'
'tag_slug__in' => $tagname,
'posts_per_page' => -1
));
if ( have_posts() ) :
while (have_posts()) : the_post();
the_title();
endwhile;
endif;
wp_reset_postdata();
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I'm using this code in different sections of my website and I want to display different posts in each section of the front page according to their category.
Can someone please help em to add a category there one is "surf" , I've tried everything.
Or maybe a different way to do it?
Thank you.
<div class="posts">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$total_post_count = wp_count_posts();
$published_post_count = $total_post_count->publish;
$total_pages = ceil( $published_post_count / $posts_per_page );
if ( "1" < $paged ) : ?>
<div class="page-title section small-padding">
<h4 class="section-inner"><?php printf( __('Page %s of %s', 'radcliffe'), $paged, $wp_query->max_num_pages ); ?></h4>
</div>
<div class="clear"></div>
<?php endif; ?>
<?php while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php get_template_part( 'content', get_post_format() ); ?>
</div> <!-- /post -->
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="archive-nav">
<?php echo get_next_posts_link( '« ' . __('Posts Antigos', 'radcliffe')); ?>
<?php echo get_previous_posts_link( __('Posts Recentes', 'radcliffe') . ' »'); ?>
<div class="clear"></div>
</div> <!-- /post-nav archive-nav -->
<?php endif; ?>
<?php endif; ?>
</div> </div> <!-- /posts -->
If you are trying to display category by ID , Then
global $post;
$args = array( 'category' => '12' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Note: In $args = array( 'category' => '12' ); 12 is the ID of
the category
But if you want to display category by Name, Then
global $post;
$args = array( 'category_name' => 'uncatogerized' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Here, uncategorized is a category name
I need to display the content of child pages on a parent page in Wordpress (not posts). This works, displays the title of the child page, but I can not get the content to display. (last 2 lines)
global $post;
$args = array( 'numberposts' => 1, 'category_name' => 'foundation','post__in' => get_option( 'sticky_posts' ) );
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) {
$the_query->the_post();
?>
<article class="lead">
<h1><?php the_title(); ?></h1>
<section>
<?php the_content(); ?>
</section>
</article>
<?php
}
$newQuery = new WP_Query();
$all_pages = $newQuery->query(array('post_type' => 'page'));
$foundation = get_page_by_title('Foundation');
$foundation_children = get_page_children($foundation->ID, $all_pages);
query_posts('pagename=foundation');
while(have_posts()) {
the_post();
the_title();
the_content();
}
foreach($foundation_children as $tc) {
echo $tc->post_title; // this works
echo $tc->the_content; // this doesn't
}
The correct variable is post_content.
Try echo $tc->post_content;
Reference: http://codex.wordpress.org/Class_Reference/WP_Post
I created a single.php Wordpress template file to show standard WP Posts.
This is the truncated version of the code:
<?php
// First (Main Content) Loop
if (have_posts()) :
while (have_posts()):the_post();
?>
<div class="entry-content">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php
// Second (Related Posts) Loop
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'category__in' => 3,
'posts_per_page'=> 4,
'paged' => $paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new wp_query($args);
if( $wp_query->have_posts() ) :
while ($wp_query->have_posts()) :
$wp_query->the_post();
?>
<div class="entry-content">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php
endwhile;
echo '<div id="navigation">'.get_next_posts_link('Load More').'</div>';
endif;
}
$wp_query = null;
$wp_query = $temp;
?>
The 4 related posts are generated as expected and the get_next_posts_link generates a link to /post-name/page/2.
I'm using Paul Irish's Infinite Scroll plugin to load page/2's posts into the container (Which is working fine on the archive- and index.php pages on the site).
Unfortunately when you click get_next_posts_link the same 4 posts are appended to the container.
Any ideas how to get this to work?
I've tried a lot of other solutions on StackOverflow but so far none have worked.
Any help appreciated, cheers.
I have one page (not an article) with n subpages.
In the main page, I need to show max 3 titles of the subpages and insert a pagination for the other.
How can I do that?
This is my simple code now:
<?php
$parent_id = 14; //main page id
$pages = get_pages( array( 'sort_column' => 'menu_order', 'numberposts' => 3, 'child_of' => $parent_id ) );
foreach ( $pages as $page ) : ?>
<div class="item">
<div class="item-title">
<h2><?php echo $page->post_title; ?></h2>
</div>
</div>
<?php endforeach; ?>
Thanks.
I solved by myself, the solution is to use wp_query() to create a new loop insted of using get_pages().
Here the new code for page title and contentwith pagination by Preeti Dua from Avigma Technology:
<?php
// Pagination variable
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// The Query
$the_query = new WP_Query( array( 'post_parent' => 782, 'post_type' => 'page', 'paged' => $paged) );
// The Loop
if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post();
global $post;
$thePostID = $post->ID; /* this variabled is used if you need to get custom fields of the subpage */
?>
<div id="side-post-title">
<?php the_title(); ?>
</div>
<div id="side-post-excerpt">
<?php the_excerpt(); ?>
<a href="<?php echo get_permalink( $page->ID ); ?>"> <div id="read-more">
<img src="/wp-content/uploads/2012/10/read-more-btn.png"/></div> </a>
</div>
<?php endwhile; endif; ?>
<nav class="navigation">
<div style="float:left;"> <?php next_posts_link('Show older', $the_query->max_num_pages) ?></div>
<div style="float:right;"> <?php previous_posts_link('Show newer') ?></div>
</nav>
not sure,
but try following plugin
http://wordpress.org/extend/plugins/wp-pagenavi/
If you'd like to add subPage title, description or even thumbnail in pagination button you can use the ACP free Wordpress plugin: http://wordpress.org/plugins/advanced-content-pagination/