Getting recent blog posts from wordpress - php

I have installed wordpress on my site located at www.example.com/blog. on www.example.com I'd like to retrieve the top 5 latest blog posts and display date, url and blog title. Is this possible?
This means I want to get the blog posts from outside the wordpress installation using php and do a loop.

<?php
$loop = new WP_Query('showposts=5&orderby=ID&order=DESC');
if($loop->have_posts()): while($loop->have_posts()): $loop->the_post();
?>
<div class="post" id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
<span class="post-meta">
<?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?>
</span>
</div>
<?php endwhile; else: ?>
No recent posts yet!
<?php endif; ?>
See: WordPress Loop, query_posts(), WP_Query(). There are also plugins to get recent posts.

Yes you can use the RSS feed of your blog. Its a standard wordpress feature. Use a javascript (or some server side) rss client to fetch the top 5 entries from RSS feed and show it on your homepage.One such script is http://p3k.org/rss/

Use WP_Query like sugested by Sepehr and after you include wp-blog-header.php add this:
header("HTTP/1.1 200 OK");
This overrides WP's security check.

Yes you can.
in wordpress you must use blog in blog plugin. if it's use you set tempalte form your design and put the shortcode like "[blog_in_blog category_slug='my-category-slug' num=5]" in your cms page or php file and you display first 5 post with date any where in your site. you must create categories and put in short code.
blog in blog :- http://wordpress.org/plugins/blog-in-blog/

Related

How to make custom WordPress theme show pages, NOT posts

I have recently created a custom WordPress theme, following a guide I found online.
However, all of the guides I came across explain how to set up a blogging site and do not explain how to set up a static website page. I am guessing that it is something to do with this code in my index.php:
.
>. get_template_part( 'content', get_post_format() );
>. endwhile; endif;
>. ?>
When I remove this, it removes the blog layout. However, I do not know what to amend to make a static page layout.
Can anyone help please?
get_post_format() Returns the post format of a post. This will usually be called in the the loop, but can be used anywhere if a post ID is provided. and its usually found on the posts pages/blogs.. therefore Its used in a theme when you want to display blog posts.
As you you might know that wp have different posts types. let's just take two of them.
Post (Post Type: 'post')
Page (Post Type: 'page')
The get_post_format() is used to get the format of a post,(blog Post) if you open your wp dashboard and start a new post or editing existing post you will see different posts formats types
As you can see from the image above, the red highlited part is the post format, that is what you are requesting when u use get_post_format() wp treats that template as it should display posts, but not pages.
If you want the content of the page you then need to use the_content()
Therefore this
get_template_part( 'content', get_post_format() );
endwhile; endif;
becomes :
get_template_part( 'content', the_content() );
endwhile; endif;
alternatively :
<div class="YourContainer">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="YourContentDiv">
<?php the_content();?>
</div>
<?php endwhile; ?>
<?php else:
echo "no content found";
?>
<?php endif; ?>
</div>
Hope this helps, Goodluck
If you look at the page template hierarchy you will see which template is used in which circumstances:
https://developer.wordpress.org/themes/basics/template-hierarchy/
For example if you create a front-page.php template it will take precedence of other templates.
If you set a static home page in settings then the template used will depend on the hierarchy first a custom named selectable template if this isn't set then a page-$slug.php template e.g. home-page.php then if that doesn't exist then a page-$id.php e.g 15-page.php if this doesn't exist then page.php will be used then singular.php then index.php

How do I display one (featured) post from Wordpress on my static html website?

We have a Wordpress News Blog on our website (http://www.litepanels.com/news/) that is separate from the rest of the static html website. I just want to take the latest story (the featured post) and display it on our home page. The Blog and our website are on the same server. I found I can grab post titles like this:
<?php
require('../news/wp-blog-header.php');
?>
<?php query_posts('showposts=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?><br />
<?php endwhile;?>
but I do not know PHP well, how do I grab just the featured post (which has a title, image and text as you can see in the link above)
I am testing it here: http://www.litepanels.com/newwebsite/blog_test2.php
If that works for you, it means you've got all you need except for some way to display your information. For this, Wordpress provides some template tags that can be used within the while loop. the_content is the one you need. Others can be found in the Codex.

Not displaying any articles on a custom made file in Wordpress

I have created the file latest.php in the public_html so that when I go to www.domain.com/latest.php it will show me the latest articles. Sadly, nothing of the posts came up. Later, I will sort them with other ways (mostly based on custom fields).
This is my latest.php file (I removed any styling for better understanding)
<?php include("wp-load.php"); ?>
<?php get_header(); ?>
<?php wp_head(); ?>
**AND HERE IS WHAT I COPY-PASTED FROM MY INDEX.PHP THAT IS WORKING**
<?php while (have_posts()) : the_post(); ?>
<a title="" href="<?php echo get_permalink(); ?>" ><?php the_title(); ?></a>
<?php endwhile; // End the loop ?>
<?php posts_nav_link(' ยท ', 'previous page', 'next page'); ?>
My question is how can I make it possible to show the latest articles with pagination ? wp-load.php , wp_head and get_header are loaded correctly.
Should I use an entire different method for my task? If yes, which one?
wordpress does not works this way... if you want to make a custom page. create a new template page in the themes folder and then (from back end) create a new page and assign that template to that page.. this way you can put you custom code in the template file and wordpress can process it

How to make a page for Custom Post Types in Wordpress 3.0?

I have some custom post types, such as 'review'. I can't seem to find out how to make a section (e.g., www.mysite.com/reviews/) which works like the blog home page, but lists reviews instead of posts (with pagination and everything). I'd like to use a separate template for it too.
Create a page called reviews and then create a new template in your theme folder called page-reviews.php. Add all the necessary elements to the template and include a query_posts in front of your post loop. It should look like this:
<?php query_posts("post_type=reviews"); ?>
<?php if (have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" >
<h2><a href="<?php the_permalink() ?>" ><?php the_title(); ?></a></h2>
<?php the_content(); ?>
</div><!-- Post ends -->
<?php endwhile; ?>
<?php else: ?>
<p>Sorry, we could not find what you were looking for</p>
<?php endif; wp_reset_query(); ?>
You need help getting .htaccess to convert your categories into hard URLs. I thought WP did this automatically so you'll want to look and make sure you have set up the directory permissions on WP so it can write to your .htaccess file.
Please read this guide and it will be cleared up.
Duplicate the file in your theme called "single.php" and rename it to "single-reviews.php"
Since "single.php" is used for your regular posts, you can append the name of the custom post type to the end of "single-" to automatically use that.
Now once inside of the file "single-reviews.php" you can adjust the layout to be how ever you want.
If you get a 404 error, or it is not showing you the correct layout, you may need to flush the rewrite rules. You can do this two ways.
Go to the permalinks page in your backend and it will sometimes auto flush them.
The best way to do it is in your "functions.php" file in your theme directory add the following code:
add_action ( 'init', 'flush_rewrite_rules' );
function flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
Create a new page called reviews. Create a new page template that calls the custom post type. Assign the page template to the page...

WordPress: How to display only posts that are in a certain category?

I'm pretty new to WordPress but have spent some 50 odd hours studying up on it, trying things out and such and have the feeling I got a pretty good handle on it now..
However the one thing I simply cannot get working is to have a page spit out a list of posts of a certain category.
Here is my example: http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters
I have a post that if I go to it directly works correctly, but does not show up on this page.
The post direct link.
The category id is '3' while the category name is 'typographic-posters'.
I have a custom page template for the typographic-posters page that looks like this:
<?php
/*
Template Name: Typographic Posters
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php if (in_category('3')): ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?=get_image('flutter-image');?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
<?php get_footer(); ?>
Using this code however the page only shows gets the header, sidebar and nothing else..
If someone could help me out that would really help me get a handle on this filtering of wordpress categories.
Thanks for reading,
Jannis
in_category will only work outside of the loop on a single page. I suggest using the query_posts function to solve this problem. You may use query_posts('cat=3') or query_posts('category_name=typographic-posters') to get the posts you are looking for.
Once obtained, just use the normal WordPress loop to access these posts.
The easiest way is to create a file called category-3.php and use the standard code from normal index.php or category.php file. Wordpress will take care of fetching posts only from category with id=3 and it's child categories.
in_category will only work outside of the loop on a single page. I
suggest using the query_posts function to solve this problem. You may
use query_posts('cat=3') or
query_posts('category_name=typographic-posters') to get the posts you
are looking for.
Once obtained, just use the normal WordPress loop to access these
posts.
This worked excellent, but make sure that you go into Settings > Reading and set the posts page to the -- Select -- option or it will override this query and dump all recent posts there regardless of category.
Simply add before the loop:
<?php query_posts="cat=3&showposts=5">
This will force the loop to display 5 posts (showposts=5) from category 3 (cat=3).
I would 2nd Eimantas' suggestion. The Template Hierarchy will use the category-3.php to display posts in that category. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need. Plus the category template will better support pagination of posts.
But if you need to stick with a Page to display those posts, also see the Page of Posts example.
http://codex.wordpress.org/Template_Tags/query_posts
Just so you know where these answers are coming from...there are a lot more interesting functions you can do with query_posts as well.
This plugin could also help you if you want to be able to change the displayed categories without going through the code :
http://wordpress.org/extend/plugins/advanced-category-excluder/
I have filtered post by category Id using the method below:
query_posts('cat=1&showposts=3');
if (have_posts()) : while (have_posts()) :
// if(1) {
//echo the_category_ID();
the_post();
/**
* The default post formatting from the post.php template file will be used.
* If you want to customize the post formatting for your homepage:
*
* - Create a new file: post-homepage.php
* - Copy/Paste the content of post.php to post-homepage.php
* - Edit and customize the post-homepage.php file for your needs.
*
* Learn more about the get_template_part() function: http://codex.wordpress.org/Function_Reference/get_template_part
*/
$is_post_wrap++;
if($is_post_wrap == '1') {
?><div class="post-wrap clearfix"><?php
}
get_template_part('post', 'homepage');
if($is_post_wrap == '3') {
$is_post_wrap = 0;
?></div><?php
}
endwhile;
else :
get_template_part('post', 'noresults');
endif;
thank you for sharing on your thought its a great thought. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need

Categories