wordpress looping through posts in custom php page - php

I trying to loop through posts in a custom php page but no matter what I do, no posts are found
here is the code I wrote in my-custom-page.php
<?php
require_once("/wp-load.php");
get_header();?>
<div id="blog">
<?php if(have_posts()) : ?>
<?php echo"anything"; ?>
<?php endif; ?>
</div>
<?php get_footer();?>

You should require wp-load.php via the full path to this file.
Hardcoded example:
require_once("user/home/public-html/wordpress/wp-load.php");
Softcoded example (suposing your file is in the same directory as WordPress):
require_once(dirname(__FILE__)."/wp-load.php");
You have also to query the posts before you display them. So, you need to add this line to your code:
query_posts('post_type=post');
The query arguments may vary depending on what you want to display. Some of them are the member variables of the WP_Post class. Go to https://codex.wordpress.org/Class_Reference/WP_Post for reference.
Here you have a re-writing of your code that displays the titles of the 30 latest posts published:
<?php
require_once(dirname(__FILE__)."/wp-load.php");
query_posts('post_type=post&showposts=30');
get_header();?>
<div id="blog">
<?php
if (have_posts()) :
while (have_posts()) :
the_post();
the_title();
echo '<br />';
endwhile;
else :
echo 'Sorry, no posts found.';
endif;?>
</div>
<?php get_footer();

wp_count_posts :
#return object Number of posts for each status.
you're trying to echo an object which ends in a fatal error. Furthermore if you want to see all posts the_post is not right. Look for it at the function reference : https://codex.wordpress.org/Function_Reference/the_post. I would do it other (google smth like "get all posts").

if you will use the code inside your theme
use the same code of Mr.Carlos but with out dir
require_once("/wp-load.php");

Related

Integrating Wordpress page into existing HTML site

I've run into a bit of a wall with a site I'm designing for a client. This was originally a very low budget static site but the client now needs the ability to edit some content from time to time. I've heard that you can just embed a wordpress page into existing HTML site and have followed the steps on the Codex site but can't seem to get it working. Any help is greatly appreciated.
So the wordpress page I'm trying to embed is the following:
http://octagonclubmiami.com/cs/
And the code I'm using is as follows:
This is posted at the beginning of the php file
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('cs/wp-blog-header.php');
?>
And this is within the body of the php file
<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php
endforeach;
?>
The actual page I'm trying to embed into is the following:
http://octagonclubmiami.com/community_service_test.php
As it sits it's currently displaying I guess some sample post instead of the page mentioned earlier.
Thanks in advance!
The better solution (I think) is to use get_post() and specify the ID number of the post/page you wish to output:
<?php
$page = get_post( 'ID NUMBER HERE' );
setup_postdata( $page );
?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
You can identify the post's ID number in the WordPress back-end.

show wordpress post outside non wordpress php page

I need to display the wordpress blog posts in a non wordpress php page. I have tried the following code.
<?php
// Include WordPress
define('WP_USE_THEMES', false);
//exact path for wp-load.php.
// This file is kept in the root of wordpress install
require('http://test.com/wordpress/blog/wp-load.php');
//Query wordpress for latest 4 posts
query_posts('showposts=5');
?>
<?php while (have_posts ()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
But it showed me the following error
Fatal error: Call to undefined function query_posts()
How to fix this?
please look into the following line in your code
require('http://test.com/wordpress/blog/wp-load.php');
in the require function you should use the relative or physical path. You should not include the url.
Since you need the wordpress database and framework this seems very unlikely to work at all. Try getting XML, RSS or JSON data from your wordpress which you can fetch with a self-made script.
For external integration, it's likely more reliable to approach this via RSS route. Simplest and possibly laziest way of making this work is by use of simplexml_load_file (and HTTP streams.)
$t = simplexml_load_file( "http://blogs.voanews.com/breaking-news/feed/" );
foreach( $t->channel->item as $item ) {
printf(
"<div>%s <a href='%s'>%s</a></div><hr/>",
$item->description,
$item->link,
$item->title
);
}
This outputs the feed as you'd expect to see it. Note that this doesn't use any kind of caching, so every page request hits the original feed.
<div>Some Chinese officials are furious at Apple's iPhone for apparently
helping users have too much of a good time. Chinese media say the complaints
surround the iPhone's voice-activated personal assistant, known as
“Siri,” which has been helping some users find prostitutes and
brothels. The Mandarin language version can apparently present users with as
many as [...] <a href='...(snip)...)'>iPhone Under Fire in China over
Prostitution</a></div>
You could use the rss option, you could write a new, hidden code and read data using that file ... in JSON format
Maybe the first thing to do is search for a extention/plugin/module that does this for you;
You are not the first one who wanted to do this, i think :p
For Displaying recent posts outside wordpress setup, first include wp-load.php file.
require( './blog/wp-load.php' );
// Load the recent top 10 posts
$args = array( 'posts_per_page' => 10, 'post_status'=>"any", 'post_type'=>"post", 'orderby'=>"date","order"=> "DESC", "suppress_filters"=>true);
$postslist = get_posts( $args );
Now you can loop through $postlist variable.
foreach ($postslist as $post) : setup_postdata($post);?>
<ul class="media-list">
<li class="media">
<div class="media-left"> <?php the_post_thumbnail( array(80,80));?> </div>
<div class="media-body">
<a target="_blank" href="<?php the_permalink();?>"><h5 class="media-heading"><?php the_title(); ?></h5></a>
<p><?php echo $post->post_content; ?></p>
</div>
</li>
</ul>
<?php endforeach;
After you include the wp-load.php you have to instantiate the query like this:
$wp_query = new \WP_Query();
$wp_query->query('showposts=5');
After that your loop should look like this:
<?php while ($wp_query->have_posts()) :
$wp_query->the_post();
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>

Looping through an array in Wordpress

I have created a custom post type with an image gallery upload. Now I am trying to display the gallery on the front end. This is what I have so far that works to display 1 image, but if multiple images are uploaded all the urls get stuck in the src tag. So I'm guessing I should loop through that array and spit out each one separately? Would that be the route to go and if so how can I accomplish this? Any help is appreciated.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
echo '<img src="'.get_post_meta($post->ID, 'gallery-upload', true).'">';
?>
<?php endwhile; else: ?>
<p><?php _e('No posts were found. Sorry!'); ?></p>
<?php endif; ?>
EDIT:
This is what is being returned:
<img src="http%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F800x400-volbeat-mock1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1574_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1576_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1576_4_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F2244_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F300789_2349086884438_1168050047_32154880_1451576942_n.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F373795_278881222158106_278880528824842_834930_1454244548_n.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F20110909-121141.jpeg">
Looking at the function reference, get_post_meta ordinarily returns an array unless the third argument is set to true. Something like this should work, more or less.
<?php
foreach(get_post_meta($post->ID, 'gallery-upload') as $meta) {
foreach(explode(',', $meta) as $src) {
echo '<img src="'.htmlentities($src).'">';
}
}
?>
EDIT: Apparently gallery-upload is stored as comma-separated values. Updated my snippet above to hopefully account for this.

Wordpress postcounter

How do I get a value of how many posts I have? Is there a wordpress tag for that?
I want to use it in my theme like this: Number of posts: 37
Yes there is a function that does that :
$count_posts = wp_count_posts();
From the official Documentation :
The default usage returns a count of the posts that are published. This will be an object, you can var_dump() the contents to debug the output.
Or :
Again qoute :
If you want to show the number of published posts use this code.
$published_posts = wp_count_posts();
echo $published_posts->publish;
I'm not sure which one is the right method, never tried before but seems like both will do the trick.
This code may help you to show no of post in current page
<?php
$count = 1;
if (have_posts()) : while(have_posts()): the_post(); ?>
layout of regular template file here
<?php $count++;
endwhile; endif;
echo $count;
?>
Alternatively you can use as shown in Wordpress Documentation wp_count_posts
<?php
$count_posts = wp_count_posts();
?>

error wordpress, adjusted sidebar.php to show latest 10 posts

I'm trying to edit my sidebar.php file in my current them WP is using to display the last # of posts (only the titles) as links.
I tried using the example of http://codex.wordpress.org/Integrating_WordPress_with_Your_Website but I always get the error on the line that states where the file wp-blog-header can be found.
the error when opening the index blog page where the sidebar should be shown:
// Get the last 3 posts.
Warning: require(/blog/folder/wp-blog-header.php) [function.require]: failed to open stream: No such file or directory in /blog/folder/wp-content/themes/default/sidebar.php on line 7
So what is wrong?
Is there a way to permanently embed a function in my html template page that retrieves the latest few posts everytime an article is displayed on the template page?
the code:
<?php require('/the/path/to/your/wp-blog-header.php'); ?>
<?php $posts = get_posts('numberposts=10&order=ASC&orderby=post_title'); foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
You don't need the require. Also, I've cleaned up the code a bit.
Try this and tell me does it work.
<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) :
start_wp();
the_date();
echo "<br />";
the_title();
the_excerpt();
endforeach;
?>
How about using this one liner :
<?php wp_get_archives('title_li=&type=postbypost&limit=10'); ?>
all you have to do is post it in sidebar.php
neater, no ?

Categories