Community @ The Turning Gate

Support community for TTG plugins and products.

You are not logged in.

#1 2018-11-04 23:08:49

lofty
Member
From: UK
Registered: 2012-09-26
Posts: 249
Website

PHP Wordpress posts

Well, this is embarrassing!

For some time in CE4 and BL1 I have been using a piece of code in my home page to show a couple of the latest post excepts from my blog.

When use this code in my BL2 updated site I get a big red banner with this message:

Something went wrong
Class not found: Requests. To fix this, reinstall Backlight at /backlight/installer.
If the problem persists, please report error at 
http://community.theturninggate.net/viewforum.php?id=44 in ClassFinder.php on line 55

I have been trying to find a fix to this but with no joy. Would appreciate some help if any of the PHP bods can point me in the right direction it would be very much appreciated.

I have removed the code for the time being so no link provided Rod, nothing to see right now smile

This is the code I have been using:

<ul>
	<?php 
		require($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php'); 
		$args = array('posts_per_page' => 2);
		$latest_posts = new WP_Query( $args ); 	
		if ( $latest_posts->have_posts() ) {
			while ( $latest_posts->have_posts() ) {
		$latest_posts->the_post();
	?>
<li>
	<div class="col_3 square__frame">
		<div class="square__content blog_pic">
			<a href="<?php the_permalink(); ?>">
				<?php if ( has_post_thumbnail() ) { ?>
					<span class="post_thumbnail"><?php the_post_thumbnail(); ?></span>
				<?php } ?>
			</a>			
		</div>
	</div>

	<div class="col_3 square__frame text">
		<div class="square__content left">
			<div class="square__padding">
				<div class="gallery_title">
					<?php the_title(); ?>
				</div>
				<div class="gallery_text">
					<?php the_excerpt(); ?>
				</div>
			</div>
		</div>
	</div>
</li>
	<? } 
		} else {
			echo '<p>There are no posts available</p>';
		}
	wp_reset_postdata();
	?>
</ul>

Last edited by lofty (2018-11-05 00:54:53)

Offline

#2 2018-11-05 05:43:51

Daniel Leu
Moderator
Registered: 2012-10-11
Posts: 1,061
Website

Re: PHP Wordpress posts

Hi Jon,

With Bl2, phplugins are now class based. Did you implement the changes listed at http://community.theturninggate.net/vie … hp?id=8966. Otherwise could you please post the entire function you use? Thanks!


Best


Daniel Leu | Photography   
DanielLeu.com

Offline

#3 2018-11-05 06:04:00

Daniel Leu
Moderator
Registered: 2012-10-11
Posts: 1,061
Website

Re: PHP Wordpress posts

Daniel Leu wrote:

Hi Jon,

With Bl2, phplugins are now class based. Did you implement the changes listed at http://community.theturninggate.net/vie … hp?id=8966. Otherwise could you please post the entire function you use? Thanks!


Best

I added your code to my site and I get the same error...


Daniel Leu | Photography   
DanielLeu.com

Offline

#4 2018-11-05 07:22:07

Ben
Moderator
From: Melbourne, Australia
Registered: 2012-09-29
Posts: 3,747

Re: PHP Wordpress posts

There isn't a class called 'Requests' within Backlight, so this looks like an issue in your WP includes not finding what they need to find within WP itself.

Offline

#5 2018-11-06 04:01:58

lofty
Member
From: UK
Registered: 2012-09-26
Posts: 249
Website

Re: PHP Wordpress posts

Hi Ben,

I thought that might be the case, however the only change is from BL1 to BL2, the code was copied as is and has worked fine up till now. Just thought there might be something I'd missed (I do that a lot these days) as this code seems to be very common on the net. I've done some forum stalking on the WP side and not come up with anything. It was a nice thing to have, so any ideas would be great.

Offline

#6 2018-11-06 15:31:41

Matthew
Administrator
From: Seoul, South Korea
Registered: 2012-09-24
Posts: 5,439
Website

Re: PHP Wordpress posts

Hi Jon,

These days, I think the WP REST API is probably the way to go for this stuff. It was first packaged into Wordpress in the 4.7 release. Here's an example. This function will get five posts from your blog.

function main_top() {
  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://yourdomain.com/blog/wp-json/wp/v2/posts',
    CURLOPT_USERAGENT => 'User Agent X'
  ));
  $posts = curl_exec($curl);
  curl_close($curl);

  $posts = json_decode($posts, true);
  if(is_array($posts)){
    echo '<ul>';
    for ( $i = 0; $i < 5; $i++ ) {
      if (isset($posts[$i])) {
        echo '<li><a target="_blank" href="' . $posts[$i]['link'] . '">' . $posts[$i]['title']['rendered'] . '</a></li>';
      }
    }
    echo '</ul>';
  }

  return false;
} // END /**/

Resources:

cURL:
https://stackoverflow.com/questions/277 … n-http-get

PHP 5 For Loops:
https://www.w3schools.com/php/php_looping_for.asp

Convert and Loop through JSON with PHP
https://jonsuh.com/blog/convert-loop-th … s-objects/

WP REST API Handbook:
https://developer.wordpress.org/rest-api/reference/

And you can visit the URL in the code above to view the post data, so that you can see how to pick it apart.

You should also install the JSON Formatter extension for Chrome:
https://github.com/callumlocke/json-formatter


Campagna Pictures, http://campagnapictures.com
The Turning Gate, http://theturninggate.net

Offline

#7 2018-11-06 15:59:53

Daniel Leu
Moderator
Registered: 2012-10-11
Posts: 1,061
Website

Re: PHP Wordpress posts

Matthew wrote:

Hi Jon,

These days, I think the WP REST API is probably the way to go for this stuff. It was first packaged into Wordpress in the 4.7 release. Here's an example. This function will get five posts from your blog.

function main_top() {
  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://yourdomain.com/blog/wp-json/wp/v2/posts',
    CURLOPT_USERAGENT => 'User Agent X'
  ));
  $posts = curl_exec($curl);
  curl_close($curl);

  $posts = json_decode($posts, true);
  if(is_array($posts)){
    echo '<ul>';
    for ( $i = 0; $i < 5; $i++ ) {
      if (isset($posts[$i])) {
        echo '<li><a target="_blank" href="' . $posts[$i]['link'] . '">' . $posts[$i]['title']['rendered'] . '</a></li>';
      }
    }
    echo '</ul>';
  }

  return false;
} // END /**/

Resources:

cURL:
https://stackoverflow.com/questions/277 … n-http-get

PHP 5 For Loops:
https://www.w3schools.com/php/php_looping_for.asp

Convert and Loop through JSON with PHP
https://jonsuh.com/blog/convert-loop-th … s-objects/

WP REST API Handbook:
https://developer.wordpress.org/rest-api/reference/

And you can visit the URL in the code above to view the post data, so that you can see how to pick it apart.

You should also install the JSON Formatter extension for Chrome:
https://github.com/callumlocke/json-formatter

Wow, that's cool! I'm currently using the RSS feed to get the latest blog posts. But this is way easier. Thank you for sharing!

Jon, to get the link to the thumbnail, have a look at https://medium.com/@dalenguyen/how-to-g … 023b9896c6

Last edited by Daniel Leu (2018-11-06 16:10:29)


Daniel Leu | Photography   
DanielLeu.com

Offline

#8 2018-11-06 22:18:23

lofty
Member
From: UK
Registered: 2012-09-26
Posts: 249
Website

Re: PHP Wordpress posts

Hi Matt,

that's fantastic, thank you very much! Thanks for the link Dan.

I have been going a little crazy trying to sort this, happy bunny now though smile

Offline

Board footer

Powered by FluxBB