I've created a customize page show all posts with template:
<?php
/*
Template Name: All posts
*/
get_header();
?>
</header>
<div role="main" id="content" class="content-warp">
<div class="container">
<div id="primary" class="content-area col-md-8 post-list">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<div class="entry">
<?php the_content(); ?>
<?php
$current_date = "";
$count_posts = wp_count_posts();
$nextpost = 0;
$published_posts = $count_posts->publish;
$myposts = get_posts(array('posts_per_page' => $published_posts));
foreach ($myposts as $post) :
get_template_part('content', 'content-single');
?>
<?php endforeach; ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_sidebar('page'); ?>
<div style="clear:both"></div>
</div>
</div>
<?php get_footer(); ?>
But it not show post_content of Posts (all remain data are normal).
By the way, with default UI Categories (content.php), i just call below code and everything Ok: the same UI with new template but have post_content).
<?php get_template_part( 'content', 'single' ); ?>
I don't know why post_content is null in new my template. I'm using Llorix One LiteVersion: 0.1.7
Any help, thanks.
I try read document again and realize the_content not set data yet.
So, just add setup post data, look like:
...
foreach($myposts as $post) : setup_postdata( $post );
...
Also You can use new WP Query
$myposts = new WP_QUery(array(
'posts_per_page' => $published_posts
));
if ($myposts->have_posts()): ?>
<?php while ($myposts->have_posts()) {
$myposts->the_post();
get_template_part('content', 'content-single');
} ?>
<?php endif ?>
<?php wp_reset_query(); ?>
Related
I'm designing a theme from scratch. It's an One page site and my CMS is WordPress.
My folder structure is :
front-page.php
page-about.php
page-work.php
page-contact.php
I can display all my pages on the front-page.php as section but I lose all my custom classes (and specific styles). Here the loop in my front-page.php :
<?php get_header(); ?>
<?php query_posts('post_type=page&order=ASC'); ?>
<?php
if (have_posts()):
while (have_posts()) : the_post(); ?>
<section id="page-<?php the_ID(); ?>">
<div class="container">
<h2><?php the_title(); ?></h2>
<article>
<p><?php the_content(); ?></p>
</article>
</div>
</section>
<?php endwhile;
endif; ?>
<?php get_footer(); ?>
How can I keep the style (therefore, their classes/id's) of the page-xxx.php as section in my front-page.php ?
Thank you
you can add a custom class to specific page id, example :
add_filter('body_class', 'your_body_class');
function your_body_class($classes) {
if (is_page(xxx))
$classes[] = 'new-class';
return $classes;
}
you can use this plugin Custom Body Class
I did this (in the front-page.php) :
<?php get_header(); ?>
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ( $query->have_posts()) {
$query->the_post();
$tn = get_page_template_slug($post_id);
$word = array("page-", ".php",' ');
$template = str_replace($word,'',$tn);
get_template_part('page', $template);
}
}
?>
<?php get_footer(); ?>
Look like it works, I have three sections with my three pages and their classes. But now, I have no content.
My page-about.php :
<?php get_header();
/* Template Name: About */ ?>
<section id="about" class="section section-about">
<div class="container">
<h2 class="title-2"><?php the_title(); ?></h2>
<div class="col-md-6 col-lg-12"></div>
<article class="article-about">
<?php
if (have_posts()):
while (have_posts()): the_post(); ?>
<p><?php the_content(); ?></p>
<?php endwhile;
endif;
?>
</article>
</div>
</section>
<?php get_footer(); ?>
Why is my WordPress blog.php only showing one blog entry while I have two entries in the blog at present? I have verified that both are published. And I do have my reading setup to my blog page. Any ideas?.
http://kvalixhu.digitalthinkersni.co.uk/blog/
<?php /* Template Name: BlogPosts */ ?>
<?php get_header(); ?>
<div id="contentWrap" class="group">
<?php get_sidebar(); ?>
<div id="article">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=5'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<?php $wp_query = null; $wp_query = $temp;?>
<h2><?php the_title(); ?></h2>
<p> Posted By <?php the_author(); ?></p>
<div id="entryItem">
<?php the_content(); ?>
</div><!--entryItem-->
</div><!--article-->
<?php endwhile; ?>
</div><!--contentWrap-->
</div><!--mainWrapper-->
<?php get_footer(); ?>
Put article opening tag inside the loop
I assume, that you want to wrap each posts with a article div. Since more than one container with the same is not allow, you should change this to a class.
Remove $temp
Why are you using $temp? Just remove this, and don't set $wp_query to null.
<?php /* Template Name: BlogPosts */ ?>
<?php get_header(); ?>
<div id="contentWrap" class="group">
<?php get_sidebar(); ?>
<?php
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=5'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="article">
<h2><?php the_title(); ?></h2>
<p> Posted By <?php the_author(); ?></p>
<div id="entryItem">
<?php the_content(); ?>
</div><!--entryItem-->
</div><!--article-->
<?php endwhile; ?>
</div><!--contentWrap-->
<?php get_footer(); ?>
-simple blog
-twenty twelve child theme
I need: a second loop in single.php that shows the selected post and all the other posts below.
What I have so far in single.php (results in a blank page) :
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php get_sidebar(); ?>
<?php
// The Second Query
$the_query = new WP_Query();
// The Loop
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ):
$the_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
This should do the trick:
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php
$args_second = array(
'posts_per_page' => -1,
);
// The Second Query
$second_query = new WP_Query( $args_second );
// The Loop
if ( $second_query->have_posts() ):
while ( $second_query->have_posts() ):
$second_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
Notes:
You need to properly show the title and content using the_title() and the_content() inside the single loop.
To show other posts, you need to query them, you'll quickly understand by looking at the code above.
I'll leave the styling for you.
It is tested and working.
im working on wordpress theme and everything goes fine but i have this problem
i make a php page "taxonomy.php" it shows custom taxonomy posts
the problem i have if i visit empty taxonomy it shows me a posts from other taxonomy insted of showing error message
the code i use to display:
<?php
get_header();
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=9&post_type=covers'.'&paged='.$paged);
$term = $wp_query->queried_object;
?>
<div id="primary" class="site-content span12">
<div id="content" class="entry-content" role="main">
<?php
$product_terms = wp_get_object_terms($post->ID, 'cover_category');
if(!empty($product_terms)){
if(!is_wp_error( $product_terms )){
foreach($product_terms as $term){
echo '<h4 style="color:#cb2027">'.$term->name.'</h4>';
//Category Desciprion
echo '<h6 style="color:#cb2027">'.term_description($term->term_id, 'cover_category').'</h6>';
}
}
}
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h5><?php the_title(); ?></h5>
<?php endwhile; ?>
<?php get_footer(); ?>
Now if i visit this
http://localhost/wp/?cover_category=as -> this taxonomy as have 6 posts and show them correct
if i visit this http://localhost/wp/?cover_category=wawa -> this taxonomy wawa have 0 posts and it show posts from as taxonomy
This is an example for default category page:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div style="height: 190px;">
<?php if ( has_post_thumbnail()) : ?>
<div class="thumb" style="float: left; width:170px;">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<?php endif; ?>
<div style="float:left; width:475px;">
<h1><?php the_title(); ?></h1>
<?php the_excerpt(); ?>
En savoir plus
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<h3>Page en cours de construction</h3>
<?php endif; ?>
Im just wondering is it possible to use the pagination_links() function in a foreach loop in wordpress?
When I try it nothing happens, I have looked around and it seems this is a little trickier than I was expecting...
<?php
$args = array( 'numberposts' => 6, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="events">
<div class="newslistingblock">
<div class="newslistingblockheader"><p><?php the_title(); ?></p>
</div>
<div class="newslistingblockthumbnail">
<?php echo get_the_post_thumbnail( $post_id, 'news-thumb', $attr ); ?> </div>
<div class="newslistingexcerpt">
<?php the_excerpt( ); ?> </div>
</div>
</div>
<?php endforeach; ?>
Im basically looking for basic pagination, with "next", "prev" and numbers.
Any help on this would be great thanks.
EDIT:
I have decided to change the code to this to suit wordpress...
<?php
query_posts( 'posts_per_page=5' );
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<!-- Do suff -->
<div class="events">
<div class="newslistingblock">
<div class="newslistingblockheader"><p><?php the_title(); ?></p>
</div>
<div class="newslistingblockthumbnail">
<?php echo get_the_post_thumbnail( $post_id, 'news-thumb', $attr ); ?> </div>
<div class="newslistingexcerpt">
<?php the_excerpt( ); ?> </div>
</div>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('← Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries →') ?></div>
</div>
<?php endif; ?>
Why are you using foreach instead of while?
The default loop with pagenation should look like this (should work with foreach as well):
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- Do suff -->
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('← Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries →') ?></div>
</div>
<?php endif; ?>
This just shows the next and previous link, but if you want pagination with numbers, I would suggest the great plugin: Wp-Pagenavi.
Good luck!
EDIT:
The error you are experiencing is that you haven't set the paged variable correctly. You need to do the following:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=5&paged=' . $paged);
?>
Then everything should work.
You can find more information in the codex: http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query