add a page to sidebar - wordpress

So I have a page called "latest news" and using a custom template t_latest_news.php
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="latest-news" id="post-<?php the_ID(); ?>">
<?php the_content(); ?>
<?php edit_post_link('Edit this page','<p class="edit-link">','</p>'); ?>
</div><!-- /.latest-news -->
<?php endwhile; endif; ?>
I've created a page item and put some content into that page. Now, I want to show the content on the sidebar. How can I do that please?
I've tried something like:
<?php include(get_query_template('t_latest_news.php')); ?>
<?php include(TEMPLATEPATH . 't_latest_news.php'); ?>
<?php get_query_template('t_latest_news.php') ?>
<?php get_template_part( 't_latest_news.php' ); ?>
But none of them works. HELP!
<?php query_posts('page_id=76'); ?>
<?php while (have_posts()) { the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php } ?>
<?php wp_reset_query(); ?>
It works with "page_id" but not pagename. any idea?

To query a specific page by name you do this:
<?php
query_posts('pagename=about'); //retrieves the about page only
?>
You should remove the .php at the end so that it reads t_latest_news
I was just showing this as an example, please be advised:
The query_posts function is intended to be used to modify the main page Loop only. It is not intended as a means to create secondary Loops on the page. If you want to create separate Loops outside of the main one, you should use get_posts() instead. Use of query_posts on Loops other than the main one can result in your main Loop becoming incorrect and possibly displaying things that you were not expecting.
see: http://codex.wordpress.org/Template_Tags/get_posts for more information

Related

have_post() does return null value in wp

I used the following code in my page template:
<?php
while(have_posts()):the_post();
the_content();
?>
But nothing is displayed. The loop is not working. I'm sure that, there is sufficient information as content in my template page.
You should use if condition to check if post exists else skip the loop. Make sure to ON the error log and check the exact error.
<?php wp_reset_query(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php
/**
* Template Name: My Template
*/
the_post();
the_content();
?>
If you save the following code as a page template and call the page template into a page by choosing it, then the page will show the page content without any hassle.
Note: it's THE minimal bit of code.
use the following code
Try using wp_reset_query()
<?php
wp_reset_query();
while(have_posts()):the_post();
the_content();
endwhile;
?>

Showing only 5 post in wordpress

HI I have edited and make a template page of responsive theme to make some thumbnails of the features pics of the posts.
ALl it's okay I can see them, but even if the posts are 9 I could see only 5. If I add one ony I see the new one, there is something like "show only latest 5 posts" but I can't understand WHERE!
get_header(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part( 'loop-header' ); ?>
<?php responsive_entry_before(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php responsive_entry_top(); ?>
<?php get_template_part( 'post-meta-page' ); ?>
<div class="post-entry">
<?php the_content(__('Read more ›', 'responsive')); ?>
<?php wp_link_pages(array('before' => '<div class="pagination">' . __('Pages:', 'responsive'), 'after' => '</div>')); ?>
</div><!-- end of .post-entry -->
(this is my added code)
<ul>
<?php
$posts = get_posts();
foreach($posts as $post) : setup_postdata($post);
?>
<li><div class="fotoBoxContent"><a class="fotoBox" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); the_title(); ?></a></div></li>
<?php endforeach; ?>
</ul>
<?php responsive_entry_bottom(); ?>
</div><!-- end of #post-<?php the_ID(); ?> -->
<?php responsive_entry_after(); ?>
<?php responsive_comments_before(); ?>
<?php comments_template( '', true ); ?>
<?php responsive_comments_after(); ?>
<?php
endwhile;
get_template_part( 'loop-nav' );
else :
get_template_part( 'loop-no-posts' );
endif;
?>
Try adding query_posts( 'posts_per_page=NUMBER_GOES_HERE' ); immediately before the <?php while (have_posts()) : the_post(); ?>
Replacing NUMBER_GOES_HERE with the amount of posts you want displayed. Use -1 to show all posts
Also within Wordpress itself Settings->Reading has a field where you can set Blog pages show at most
I'm still not sure what exactly you want to achieve but if you just want to have thumbnails of posts that are in the main loop, then you do not need to do additional query.
All you need to do is something like this:
1.) Before <?php if (have_posts()) : ?> you initialize some variable:
$thumb_data='';
2.) After <?php if (have_posts()) : ?>
$thumb_data='<ul>';
3.) replace your "added code" with this:
$thumb_data.='<li><div class="fotoBoxContent"><a class="fotoBox" href="'.get_the_permalink().'">'.get_the_post_thumbnail()." ".get_the_title().'</a></div></li>';
4.) After the main while loop, add:
$thumb_data='</ul>';
5.) Mow all the HTML code for the list of thumbnails will be in $thumb_data, so just echo this variable in the template where you want the HTML code to appear.

Why the_content doesn't work in wordpress single.php

My code:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="posts" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2>
<?php the_title(); ?>
</h2>
<div class="entry">
<?php the_content();?>
</div>
</div><!--post end-->
<?php endwhile; ?>
<?php else : ?>
<h3>no content</h3>
<?php endif; ?>
I put the code into my customed wordpress theme file single.php. Why it can't output the post content, it can output the post title. thank you.
You can try the following to see if it works instead of the_content
<?php echo wpautop($post->post_content); ?> // content with p tags
<?php echo $post->post_content; ?> //without p tags
Also an option
<?php echo wpautop( get_the_content() ); ?> // content with p tags
see if that works for you.
When developing Wordpress themes it's advisable for you to switch the debug mode (found on your installation's root in wp-config.php) to true. This will alert you if you have any errors.
In your case, try out the <?php the_excerpt(); ?>.
Also, this may sound a bit dumb, but do you actually have posts? Not pages or rather content in that post?
Many a times i have come across queries from developers or first time theme developers about not able to show the_content() on their custom template page.
The issue is that the_content() function runs inside the WP loop so for example
if (have_posts()) {
while (have_posts()) {
the_post();
the_content();
}
} ?>
This will work, but in some cases you want to call the function outside the loop – the solution for that is:
echo $post->post_content;

Pull latest posts from a specific category

I am trying to pull latest posts from a specific category.
I am currently able to pull all latest posts and display them the way I want using the code below but I am unable to do the same thing from a specific category.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="content"><div id="circle"><div id="circle_text1"><p><?php the_time('M') ?></p></div>
<div id="circle_text2"><p><?php the_time('dS') ?></p></div></div>
<div id="text"><div id="title"><p><?php the_title(); ?></p></div>
<div id="name"><p>By <?php the_author(); ?></p></div>
<div id="blurb"><p><?php the_content('<br />Read More'); ?></p></div></div>
<div id="line_rule"><p> </p><hr /></div></div>
<?php endwhile; ?><?php else : ?><h2>Not Found</h2><?php endif; ?>
Thanks in advance
This is a basic WP query that resets itself and can be used multiple times in a template. You can add your html to it. showposts is the number of posts to show; -1 shows all posts.
<?php $my_query = new WP_Query('category_name=mycategoryname&showposts=10'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>
if you are getting posts from database then use order by ID desc that will show latest post on the top. if ID is auto-incremented. like
select * from posts order by ID desc
I do this all the time. This will work:
Change the cat= number to whatever category ID you want to use.
Add html markup as you please and whatever other content you want to pull.
<?php query_posts('cat=4&order=ASC');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
In the example I am only grabbing the post content, but you could put in the title, meta-data, etc. The important parts are all here.
Try it out.
Add this line of code above your opening IF statement.
<?php query_posts( 'cat=1' ); ?>
And then change the 1 to match the ID of the category you are trying to display.
:)
You can do that by simply by adding this first line to your code:
<?php query_posts( 'category_name=slug_of_your_category&posts_per_page=10' ); ?>
Replace "slug_of_your_category" with the slug of your category, and "10" with the amount of posts you need.

WordPress Loop Display Content if Have Posts

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I'm using the loop to pull a few custom posts to display on the site. I have a designated <div> to hold the posts. I've run into the problem when there are no posts to be pulled, the div box still displays with no content in it. How would I insert the code of my div container within the "if" statement so that the div is only created if there are posts?
<?php if (have_posts()) : ?>
<div>
<?php while (have_posts()) : the_post(); ?>
…
<?php endwhile; ?>
</div>
<?php endif; ?>
You may find the documentation on PHP's alternative syntax useful
Try the following code snippet:
<?php if (have_posts()) { ?>
<div>
<?php } ?>
<?php while (have_posts()) : the_post(); ?>
...
<?php endwhile; ?>
<?php if (have_posts()) { ?>
</div>
<?php } ?>
<?php endif; ?>
I hope this helps!
Create a 404.php for your theme and drop the if (have_posts()) completely. You can start the loop with the while statement, and WordPress will use the 404.php if there are no posts.

Resources