Trying to display recent posts outside wordpress - php

I try to display recent posts on my main website which is outside wordpress, my blog is actually blog.domain.com and the directory root of the wordpress is in my main website if this help, here is my code:
// Include the wp-load'er
include('blog_subdomain/wp-load.php');
// Get the last 10 posts
// Returns posts as arrays instead of get_posts' objects
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 10
));
// Do something with them
echo '<ul>';
foreach($recent_posts as $post) {
echo '<li>', $post['post_title'], '</li>';
}
echo '</ul>';
But when I try to access my page where I want the recent post to be displayed it redirects me to the blog address.

Related

Wordpress - Imported pages appear, but lost data

I exported just the pages from one WP install to another and it was successful. When I go to the pages list, it displays them all and shows parent/child relationships, but doesn't seem to be recognized anymore when using some custom code in the template. It works on the original site just fine and I compared WP versions; they're the same.
Here is the code I'm using:
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'orderby' => 'date', 'order' => 'ASC'));
// Get the page as an Object
$products = get_page_by_title('Products');
// Filter through all pages and find Products' children
$products_children = get_page_children($products->ID, $all_wp_pages);
// echo what we get back from WP to the browser
echo '<ul id="product-nav">';
foreach($products_children as $child){
echo '<li><a href="'.get_page_link($child->ID).'" title="'.$child->post_title.'">';
echo get_the_post_thumbnail($child->ID).'<span>'.$child->post_title.'</span>';
echo '</a></li>';
}
echo '</ul>';
When I performed a var_dump() on $all_wp_pages, they aren't being displayed there either. It's as if they don't exist, but show up on the back end as expected. Finally, I tried changing one of the child page's parent, then back again in hopes that would reset it, but had the same result.
Any idea why this would be happening?

Wordpress - next_post_link() not filtering by post_type showing nothing

I'm trying to add previous and next links to my portfolio posts. I have various custom posts, but I only want to link to my portfolio custom posts.
I have been looking in the documentation and have tried using this in the wordpress loop
<?php next_post_link('%link', 'Next post in category', true); ?>
This returns nothing.
If I use
<?php next_post_link('%link', 'Next post in category', false); ?>
It returns a link, but it links to a different custom post from a different post type.
Any ideas on how to do this?
The third parameter accepted by next_post_link() indicates whether the next post must be within the same taxonomy term as the current post. If set to true, only posts from the current taxonomy term will be displayed. As you can see, it has no bearing on the type of post.
Therefore, you will need to use an alternative function.
Ref: https://codex.wordpress.org/Function_Reference/next_post_link
I managed to solve it by doing the following,
Performing a WordPress query getting all of the relevant posts
for the post type 'portfolio'
Declare an array of IDS and put each post ID inside.
Get the index from the array of the current post
Check the page exists, if show display the link to the page
// get_posts in same custom taxonomy
$postlist_args = array(
'posts_per_page' => -1,
'order' => 'ASC',
'post_type' => 'portfolio',
);
$postlist = get_posts( $postlist_args );
// get ids of posts retrieved from get_posts
$ids = array();
foreach ($postlist as $thepost) {
$ids[] = $thepost->ID;
}
//Get index of current post
$thisindex = array_search($this_post_id, $ids);
//Check to see if pages exist
if(isset($ids[$thisindex-1]))
{
$previd = $ids[$thisindex-1];
echo '<a rel="prev" href="' . get_permalink($previd). '">previous</a>';
}
if(isset($ids[$thisindex+1]))
{
$nextid = $ids[$thisindex+1];
if ( !empty($nextid) ) {
echo '<a rel="next" href="' . get_permalink($nextid). '">next</a>';
}
}
/** End of function **/
?>
This is adjusted for my solution, credit
http://bucketpress.com/next-and-previous-post-link-in-same-custom-taxonomy

how to get the posts from my index page to someother pages(like blog) in WordPress

i am beginner to WordPress, i have created the index.php it automatically shows my posts.. but i wanna posts to show also in another php file like blog.php.. how can i retrieve the same posts which is shown in index.php.........
The scenario is, i am developing theme for http://themeforest.net show that i wanna make some features in my blog page like without sidebar, left sidebar, right sidebar... but my home(index.php) contains posts.. whenever i starts new page with same coding(like blog.php), it doesn't show the index.php 's posts..
This sample piece of code retrieves 3 posts from categpry id 4
<?php
$args = array( 'numberposts' => 3, 'order'=> 'DESC', 'orderby' => 'post_date', 'category' => 4 );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
$a= get_the_date();
?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Refer the WordPress documentation http://codex.wordpress.org/The_Loop
it contain different example how to display post include or exclude post from specific categories etc..

Wordpress: include content of one page in another

How do I include the page content of one or more page in another page?
ex. I have pageA, pageB and pageC and I want to include the contents of these pages in pageX
is there a wordpress function that loads the post of a specified page/post?
like show_post("pageA")??
There is not a show_post() function per se in WordPress core but it is extremely easy to write:
function show_post($path) {
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Note that this would be called with the page's path, i.e.:
<?php show_post('about'); // Shows the content of the "About" page. ?>
<?php show_post('products/widget1'); // Shows content of the "Products > Widget" page. ?>
Of course I probably wouldn't name a function as generically as show_post() in case WordPress core adds a same-named function in the future. Your choice though.
Also, and no slight meant to #kevtrout because I know he is very good, consider posting your WordPress questions on StackOverflow's sister site WordPress Answers in the future. There's a much higher percentage of WordPress enthusiasts answering questions over there.
I found this answer posted on the Wordpress forums. You add a little code to functions.php and then just use a shortcode whenever you like.
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
the_title();
}
the_content();
}
wp_reset_postdata();
}
add_shortcode( 'my_content', 'get_post_page_content' );
For the shortcode,
[my_content id="Enter your page id number" title=Set this to true if you want to show title /]
Pages are just posts, with a post_type of 'page' in the database. You can show the content of multiple pages on another page by writing a post query in your pageX template that gets the posts you specify and output them in a Loop.
There are three ways to get post content from the database:
get_posts
query_posts
WP_Query
These links all point to the WordPress Codex. Get_posts and query_posts have an argument available, 'page_id', where you can specify the id of the page you'd like to retrieve and display.
You could install a Plugin "Improved Include Page". Once installed, you create page X and enter:
[include-page id="123"]
[include-page id="124"]
[include-page id="125"]
where these are the ID's of pages A, B and C respectively
<?php query_posts('p=43');
global $more;
//set $more to 0 in order to only get the first part of the post
$more = 0;
// the Loop
while (have_posts()) : the_post();
// the content of the post ?>
the_title();
the_content();
endwhile; ?>
This is obviously a portion of the post, I got the detail from the wordpress codex.
Interesting... I looked around for how to embed Wordpress content elsewhere (as in, on another website), and found some things...
www . shooflydesign.org/buzz/past/embedding_wordpress . html
Shows how to embed Wordpress content in another site with a php script; maybe just use the same thing to embed Wordpress into itself?
www . corvidworks . com/articles/wordpress-content-on-other-pages
Similar concept; embed Wordpress on another page; just try to use that tool in a new WP post
feeds themselves
My searching pulled up some suggestions to just use a feed to your own blog to embed into a post. There's nothing preventing that. You might want it automated and so restructuring the feed to look right might be problematic, but it's worth a shot depending on what you want to do.
Hope those are quasi-helpful. While they all are solutions for getting your WP content on some place other than WP... they might work for your given question and allow you to display A, B, and C on X.
There is a Wordpress function to display the content of a particular page inside another using query_posts() it is:
<?php query_posts("posts_per_page=1&post_type=page&post_id=134"); the_post(); ?>
You set the number of pages to be displayed to 1, post type is page instead of post and the page id
Kit Johnson's wordpress forum solution with creating a shortcode works, but adds the inserted page in the top of the new page, not where the shortcode was added. Close though, and may work for other people.
from the wordpress post, I pieced together this which inserts the page where the shortcode is put:
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$output = "";
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
$output .= get_the_title();
}
$output .= get_the_content();
}
wp_reset_postdata();
return $output;
}
Then, the shortcode bit works as expected. If you don't want the title, title=false does not work, you need to leave title off entirely.

How can I loop through posts as well as child pages to display them all by date in Wordpress 2.9

Some background info --
In wordpress I have my portfolio as a parent page with each item of work a child page.
I also have a blog.
I want to display the most recent 6 items on the homepage whether they are from my portfolio or my blog.
I have a loop that displays the posts and on another page I have a loop that displays the child pages of a specific parent page. I only need to display the_title and the_post_thumbnail.
Is it possible to loop through both the posts and the child pages and display them in order of date (recent first).
So the final display would be a mixture of posts and child pages.
Could it be done by having a separate loop for pages and one for posts, then somehow adding the results to an array and then pull them out in date order (recent first).
Any help would be greatful.
Thanks
Use a custom query.
Example
$latest_content = new WP_Query(
array(
'posts_per_page' => 6,
'post_type' => 'any',
'post_status' => 'publish'
)
);
if ( $latest_content->have_posts() )
{
while ( $latest_content->have_posts() )
{
$post = $latest_content->next_post();
// Do anything you know about the loop.
print $post->guid . '<br>';
}
}
rewind_posts();
I have not tried it myself, but I think you can use the get_posts template tag with post_type => 'any'.
Like so (default ordering is date descending):
<?php
$args = array('post_type' => 'any', 'numberposts' => 6);
$posts_and_pages = get_posts($args);
?>

Categories