getting posts from Wordpress to out of WP in codeigniter view - php

I use codeigniter and need to display last 3 posts at footer from blog as blabla.com/blog located.
when I create a test.php file as below. it works well,
test.php
<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('./blog/wp-blog-header.php');
query_posts('showposts=3');
?>
<ul>
<?php while (have_posts()): the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
but when I copy same code to footer_view.php of codeigniter structre, it doesnt work and giving error as below:
error at codeigniter footer_view:
Fatal error: Call to undefined method
stdClass::set_prefix() in
/blabla/blog/wp-settings.php
on line 268
any idea what can be the problem? :/ appreciate helps!!

I've used 3 tricks for getting WordPress content into CodeIgniter:
Pull via XMLHttpRequest from a custom WP template (skip headers/footers/sidebars). I like this method as it is highly decoupled, and it makes for fast page loads.
Pull via CURL or get_file*. This is similar to using XMLHttpRequests, but server side.
Wrap WP in a library. This is more work, but the essence is calling the core WP object from a CI library. I prototyped this method last year, but found that #1 performed better (and it allowed me to move the content to another server later).
Note, you could also IFrame the page, but IFrames seem a bit hackish given #1 and #2.

Have you thought about using the RSS feed from wordpress to display the blog posts with codeigniter? It would be a more flexible solution.

Related

Changes to page.php and single.php not applying

I'm having quite a weird issue with my WordPress site. I am building a theme from scratch and I'm trying to make change to the containers on my blog page (page.php and single.php). Weirdly enough, I have made no major changes to the code other than add a wrapper, but the changes aren't showing up.
I've made many themes without this issue so I'm unsure what's going wrong this time. I have set the blog page to be the posts page in the settings so I can't understand why the template won't update. The code I have is below, and is just the standard WordPress code with the exception of 'grid wrapper' and attempting to remove the sidebar which also shows.
I have done quite a bit of research on this to see what the problem could be, but sadly the only post similar was in 2014 with no answer.
<div class="container main-content">
<div class="grid wrapper">
<section class="the-content">
<main>
<h1><?php the_title(); ?></h1>
<?php while ( have_posts() ): the_post(); ?>
<?php get_template_part('template-parts/content', 'page'); ?>
<?php endwhile; // End of the loop. ?>
</main>
</section>
</div>
</div>
Long story short, my content shows just fine. I just can't edit the wrapper which contains that content.
I would change parameters in get_template_part()
This is how it supposed to be used: Understanding get_template_part
So if you have blank Wordpress file structure, I'm not sure if you have template-parts for sure. You can have them only if you intentionally made them.
Use wordpress Wp-Super_cache Plugin
I had the same issue. Most likely index.php is overriding your file as in my case. It depends how your theme handles the loop so check out on your index.php and change it there.
If i remember well, the blog page set in the back-office reference first at home.php and then index.php. Page and single aren't call.

Embedding Wordpress header and footer in Yii framework app

I'm building a website that uses Wordpress as the CMS and is built with Yii framework. All the business logic works fine. Most visible pages are filled with content from Wordpress admin and only the user profile section is built with Yii.
My problem is that I would like to reuse the layout of the wordpress pages in the Yii app. Initially I tried to do it in a blank PHP file and everything worked as expected with this code:
<?php
require( '/var/www/wordpress/wp-load.php' );
get_header();
?>
<div>
some content here
</div>
<?php
get_footer();
?>
The problem is that when I make a new layout in Yii so that I can reuse the header and footer it crashes and displays blank page. I tried both with require('/var/www/wordpress/wp-blog-header.php'); and require( '/var/www/wordpress/wp-load.php' );.
This is the code I use to load wordpress functionality in Yii but unfortunately it does not work.
<?php /* #var $this Controller */
spl_autoload_unregister(array('YiiBase','autoload'));
define('WP_USE_THEMES', true);
require('/var/www/wordpress/wp-blog-header.php');
//require( '/var/www/wordpress/wp-load.php' );
get_header();
spl_autoload_register(array('YiiBase', 'autoload'));
?>
Any help would be much appreciated. Ideally I would like to use get_header() and get_footer() wordpress functions in Yii layouts/views.
Thanks
UPDATE
I checked Apache logs and it seems that the following error is logged when I try to open the page in my browser: PHP Fatal error: Call to a member function get() on a non-object in /var/www/wordpress/wp-includes/query.php on line 27
Also when I comment out the get_header(); row the page opens without any errors (and without any styling too). So something prevents get_header() to work property in Yii.
I ended up adding require( '/var/www/wordpress/wp-load.php' ); in the Yii's index.php file just before the initialization code. Then I was able to use all WordPress functions in my project. This helped me reuse the header and footer layout from WordPress in Yii using the get_header() and get_footer() functions.

What to include in php page to use wordpress's functions?

I have a wordpress website and I want to create a page that also has javascript and upon a certain user action the javascript calls the server with an AJAX request.
This is a GET request to a php script I created. Since I extended wordpress with my plugin I put this php in my plugin's folder.
The problem is that from this php script I want to access everything that wordpress offers, e.g. the database access, but I do not know how.
What do I have to include in this php file in order to access the functions offered by wordpress? I wanted to use database access so I included the wp-db.php file and declared the global wpdb variable, but it did not help.
Can anyone tell me how to accomplish this?
Thanks in advance!
Wordpress has it's own build in ajax: http://codex.wordpress.org/AJAX_in_Plugins
Use this instead of your own ajax script.
In this ajax script you can use all WP functions. It will also make your plugin more like wordpress standards.
As recommended in he WordPress Codex itself.
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>
By using the blog header no database queries are executed by default, you have to supply the get_posts that will fetch what-ever articles you need based on your application's parameters:
<?php $posts = get_posts('numberposts=10&order=ASC&orderby=post_title'); ?>
<?php foreach ($posts as $post) : start_wp(); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>

Creating a custom page on wordpress and adding content to it

I want to create a php page on the website (CMS Wordpress), but I do not understand how to incorporate the template files, for example
<?php get_header(); ?>
And other content. I know that it is possible to create pages through the admin interface, but I do not fit... if you try to create a page of template files and then go for it the direct link:
http://local/wp-content/themes/mytheme/mypage.php
with pre-embedding the code
<?php get_header(); ?>
it will fail like:
Fatal error: Call to undefined function get_header()
What should I do?
How i can "say" this CMS that the page is important for me, and it performs some functionality? That is, in fact, get the full url to her and that did not give out errors…
You need to include wp-load.php in your script to use any of WP functions:
<?
require_once('../../../wp-load.php');
get_header(); ?>
try that. xD

How to have different logic for a page and a post within Wordpress?

I am modifying a wordpress template and need to slightly separate rendering logic for a post and a page, specifically in relation to how the date renders out. The problem is that I cannot find any code to do this, I am sure that it exists. Is there a variable that exists within wordpress that tells me whether the item being displayed is a page or a post?
In an ideal world it would look something like this:
<?php if (is_page()) : ?>
page logic
<?php else: ?>
post logic
Would appreciate any help!
Pages are a type of post, so get_post_type should return appropriately different values for pages vs normal blog posts.
I found this link: http://wordpress.org/support/topic/sidebar-logic-for-postblogroll-and-page-type which seemed to do the business for me.
The answer (copied straight from the page) was:
<?php if(is_singular($post)): ?>
Page Content
<?php else:?>
Post Content
<?php endif;?>

Categories