Still need help... Embedding Wordpress page content into a PHP site - php

've done a lot of searching on the net and here, and cannot find the answer to this, though I know it may be ridiculously simple. I'm missing something...
I am trying to embed the content of a Wordpress PAGE (one which was created automatically by the Wordpress plugin All In One Event Calendar) into a page on my website.
(Note: I've confirmed that "page" and "post" are the same thing...)
When I visited the Wordpress codex, I got this page: http://codex.wordpress.org/Function_Reference/get_page which tells me that the get_page function has been deprecated and send me to the "get post" page of the codex.
The URL of the wordpress PAGE (not POST) that I'm trying to embed looks like http://xxxxxx.com/test/blog/?page_id=4.
I have the following code at the top of my site...
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('/home3/xxxxxx/public_html/xxxxxx/xxxxxx/blog/wp-load.php');
?>
(Note: have confirmed that wp-load works... wp-blog-header.php DOES NOT. Thanks, Bacon!)
And created the loop:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$id = 4;
$p = get_post($id);
echo apply_filters('the_content', $p->post_content);
endwhile; else :
_e( 'Sorry, no posts matched your criteria.' );
endif; ?>
The code I am using to attempt to embed the page content is:
<?php
$id = 4;
$p = get_post($id);
echo apply_filters('the_content', $p->post_content);
?>
It doesn't seem to be working. It returns the error message (Sorry, no posts matched your criteria.) Yet there IS a post (page) with the ID of 4.
What am I missing?
Please be gentle and thank you in advance for any help you can provide.

Require wp-load.php instead of wp-blog-header.php.
get_post($id) should also work as a way to get a page.

Related

Finding out which Wordpress template is used for a page from admin page

I'm trying to retrieve the filename/path of template used on the 'Edit Page'-page in the Dashboard.
Similar to what wp-includes/template-loader.php (source) does on the front end: finding out which template to render.
Unfortunately, expressions like is_front_page() - which Wordpress' template-loader.php uses to find out if it should use get_front_page_template() - don't work correctly on the admin page. Which is to be expected because those expression use the global $wp_query object, and not the current query.
What I've tried so far:
Running a post loop inside the admin page
$args = array(
'p' => get_the_ID(),
'post_type' => 'any'
);
$query = new \WP_Query($args);
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?= the_title(); ?><br>
Is front page: <?= is_front_page() ? 'true' : 'false' ?>
<?php endwhile; endif; ?>
Displays:
Home
Is front page: false
Using get_post_meta
<?= get_post_meta(get_the_ID(), '_wp_page_template', true); ?>
Displays:
default
...which would be the same for front-page.php on Home and page.php on another default page, so this doesn't help me.
In short
What I'm trying to get is front-page.php when I'm editing my 'Home' page. Or custom-template.php when I'm editing some page with the custom template selected. Or about-page.php when I'm editing a page called 'About'. How to get the correct filename or path?
If your specific problem is with the home page, you could use a combination of get_page_template() and comparing the edited page's ID with get_option('page_on_front') (see WordPress option reference). There's also an option, show_on_front, which indicates whether the front page shows posts or a static page.
Maybe this helps? I don't know if there are other edge cases where a different template will be used...
Use get_page_template():
<?php echo realpath(get_page_template()); ?>
It outputs something like /foo/bar/baz/wp-content/themes/your-theme/{page-template}.php
You can choose not to use realpath() and just get the template name.
The only solution I found is this:
global $template;
echo basename($template); // front-page.php
I know it's ugly, but I just couldn't find a way to not use this global variable.

Wordpress use index.php instead of single.php to show post

My Wordpress websites are not using single.php to show posts on the website. Every time I open a post, it opens it in index.php.
My single.php looks like this
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<? echo the_content(); ?>
<? endwhile;
endif; ?>
<? get_footer();
?>
How can I fix this?
I had the same problem with neither the single-CUSTOM-TYPE.php nor the single.php being rendered after clicking the single-post-link.... only index.php instead of the correct file...
What helped me was a simple change back to Standard Permalinks in "Settings" -> "Permalinks" and a restore back to "Name of the Post" (Beitragsname)....
...maybe this might help someone else as well...
greetz
This happens if the LOOP is not correctly setup ensure that index.php, and single.php contains the LOOP.
The loop normally looks something like this, but will change to setup requirements.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
The Wordpress Codex site is pretty awesome and will answer most questions, check out http://codex.wordpress.org/The_Loop
Furthermore questions and discussions such as this one is more ideal if you post on stacks sister site Wordpress Stackexchange. I expect this question will be deleted or moved to https://wordpress.stackexchange.com/.
You should check your loop.php or loop-single.php weather it is routing from these files or not this are the page from where it will bring the data from database

WordPress PHP Plug-in - Post to external website via save_post command

I use stackoverflow very often and just found out in google about this one, Nice :D
Well,
i want to save my every post i write from wordpress to my 3rd (OEXChangeble) website aswell at the same time, so i need to send the info to my website, developing a plugin iguess
I need basically the permalink (and i would be able to extract the rest of the params from my wesite), but better if i can get title, tags, permalink and description(or some of it)
i understand by my google research that all i need to do is add something like
<?php
//header of plugin
function myFunctionThatSendsMyWebsite($url){
// procedure containing a file_get_contents('myqwebsite?url=') request to my website
}
add_action('page_post', 'myFunctionThatSendsMyWebsite', $permalink));
?>
I have problems thoug finding the name of variables i have missing (marked by ???). I know that $post contains all objet, how to extract the info from it (if there is), or if it's complicated, it would be enought for me with permalink
Any tip?
Thanks!
according to this link, this should work, the post id will be sent to this function automatically and you can get anything you want from a post id.
function myFunctionToSendPost($post_ID) {
$post = get_post($post_ID);
$title = $post->post_title;
$content = $post->post_content;
$permalink = get_permalink($post_ID);
...
sendToYourServer($params);
return $post_ID;
}
add_action('publish_post', 'myFunctionToSendPost');
BTW, this function is called when a post is published, you can change it to happen when saved by
add_action('save_post', 'myFunctionToSendPost');
add these lines to your theme's functions.php file.
1) While this doesn't take advantage of the save_post feature, you can even use this code to display blog posts on a completely separate web site, as long as it’s on the same server and you have filesystem access to the WordPress directory on the original site. Simply modify the require() in the first block on this page to use the full path to your WordPress installation:
<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('/var/www/example.com/wordpress/wp-load.php');
query_posts('showposts=1');
?>
position your post with a while loop:
<?php while (have_posts()): the_post(); ?>
<?php endwhile; ?>
if you want to specify which parts of the post to display use this code:
<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p>Read more...</p>
<?php endwhile; ?>
2) How about using the rss feeds from your wordpress blog?
The following code will display a list of your feed titles with descriptions including a hyperlink to the original WordPress posts:
<?php // Load the XML file into a Simple XML object
$filename = http://feeds.feedburner.com/<em>yourfeedname</em>";
$feed = simplexml_load_file($filename);
// Iterate through the list and create the unordered list
echo "<ul>";
foreach ($feed->channel->item as $item) {
echo "<li><a href='" . $item->link . "'>" . $item->title . "</a></li>";
}
echo "</ul>";
echo "<li>" . $item->description . "</li>";
?>
3) Feedburner has a free feature called BuzzBoost where you can get your posts to show in a regular HTML website that once activated you can simply copy the script that they provide into your HTML where you want the list to appear. From your feedburner account you can adjust some elements like whether the Blog title should appear or not, the format of the date, etc...
You can also style the output using regular CSS within you websites existing CSS.
Check it out Feedburner here:
https://www.google.com/accounts/ServiceLogin?service=feedburner&continue=http%3A%2F%2Ffeedburner.google.com%2Ffb%2Fa%2Fmyfeeds&gsessionid=5q8jqacBluH1-AnXp08ZFw
Are you trying to save a copy of the post somewhere else when they first publish the post? Or on every page view?
You can trigger it when the post is saved by writing a plugin that implements the save_post hook:
http://codex.wordpress.org/Plugin_API/Action_Reference
To do it on every page, you'd probably write a plugin with a filter hook on the page (if it's something you want other people to use) or if it's just one site, you could add it to your theme.
But... Are you sure you want to do this? It seems like your keepyourlinks site might be better implemented as an update service: http://codex.wordpress.org/Update_Services

Custom home page with a single post?

This is a question about Thematic framework. If this isn't the right place, just ignore this.
I'd like to ask how do I go about it? I copied the page template from the parent theme and renamed it to home.php and then I set it as the template for the home page. Then I added a hook to thematic_above_indexloop() in functions.php but it doesn't seem to work. Here's my action hook:
function show_single_post(){
$i=0; // Initialize to Zero;
if (have_posts()) :
while (have_posts()) : the_post();
if ($i==0) {$recentpostid = $post->ID; $i=$i+1;}
endwhile;
endif;
//get only the latest post
$posts = query_posts( 'p='.$recentpostid."'");
}
add_action('thematic_above_indexloop', 'show_single_post');
However, if I embed that code snippet into the home.php directly, it works. There must something wrong with my action hook? I am relatively new to using hooks pls enlighten me.
Since the hello world function works, I have to believe that your function is the problem.
There is a simpler way to get the single latest post:
function show_single_post(){
query_posts('orderby=ID&order=desc&showposts=1');
}
add_action('thematic_above_indexloop', 'show_single_post');
I tested this with Thematic and it shows the last post on the homepage. Give it a shot.
You can check out all the parameters accepted by query_posts on the Codex page
I'm not familiar with Thematic, so I browsed the documentation wiki at http://themeshaper.com/thematic/guide/. I couldn't find the action you reference there. Was it removed from the current version of the framework?
Create a simple "hellow world" function to test if the hook is broken vs your code being broken.
function hello_world(){
echo "hello world";
}
add_action('thematic_above_indeloop', 'show_single_post');
If you see "hello world" on your homepage, you'll know the hook is working. If not, you can be sure (as you probably already are) that your show_single_post function isn't broken.

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