• 0

[PHP/Wordpress] How to do this?


Question

Hi,

I need to put some wordpress code into a php file, for instance display the latest 3 posts.. I've seen this snippet of code:

<?php
// Include Wordpress 
define('WP_USE_THEMES', false);
require('./blog/wp-load.php');
query_posts('showposts=1');
?>

<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p><a href="<?php the_permalink(); ?>">Read more...</a></p>
<?php endwhile; ?>

.. But I can't use this code, I need it all to be pure php code (like, without the opening and closing tags ). I'm not familiar with how to do this.

The reason I'm asking is that I'm trying to use it as a plugin for phpfox (a social networking script), which only accepts php code without opening/closing tags. Could someone help me convert the code or something? Thanks.

Link to comment
Share on other sites

21 answers to this question

Recommended Posts

  • 0

You can Mix php with HTML. The opening and closing PHP tags is normal.

You should approach this another way, do you get any errors when you try to use the code?

Link to comment
Share on other sites

  • 0

Nope, the script just stops loading everything the very second it sees a <?php or ?> tag. In this case, I can't use the tags. There must be some other way?

Link to comment
Share on other sites

  • 0

In that case you will need to echo the output.

i.e.

&lt;?php
// Include Wordpress 
define('WP_USE_THEMES', false);
require('./blog/wp-load.php');
query_posts('showposts=1');


while (have_posts()): the_post(); 
echo "&lt;h2&gt;". the_title()."&lt;/h2&gt;";
echo the_excerpt();
echo "&lt;p&gt;&lt;a href=\"".the_permalink()."\"&gt;Read more...&lt;/a&gt;&lt;/p&gt;";
 endwhile;

Code not tested but apart from my lack of knowledge with while statments I think it should work

Link to comment
Share on other sites

  • 0

Nope, the script just stops loading everything the very second it sees a <?php or ?> tag. In this case, I can't use the tags. There must be some other way?

Can you make sure you're not doing it on already opened php tags? such as:

<?php

...(previous code)

<?php

(wordpress script)

?>

...(code)

?>

Link to comment
Share on other sites

  • 0

Positive, it's just a finicky script.. It works great if you're a user, but if you're an administrator it's an absolute nightmare. :wacko:

I'll test the echo'd script.. Thanks.

Edit: gah, nothing displays. Maybe I'm not using the script right... Bloody nightmare. :crazy:

Link to comment
Share on other sites

  • 0

No, I meant administration is overly complex and the way the thing is built is far too messy... which makes it extremely difficult to manage. I'm stuck now! :laugh:

Thanks for the help. I've no idea where to go from here..

Edit: If I put

echo "This is some text";

In, on it's own, this text displays. But if I put

while (have_posts()): the_post(); 
echo "&lt;h2&gt;". the_title()."&lt;/h2&gt;";
echo the_excerpt();
echo "&lt;p&gt;&lt;a href="".the_permalink().""&gt;Read more...&lt;/a&gt;&lt;/p&gt;";
 endwhile;

In, .. Nothing displays. Including the 'this is some text' bit.

.. Wtf.

Link to comment
Share on other sites

  • 0

Um, try this code instead of the while statement:


wp_get_archives('title_li=&amp;type=postbypost&amp;limit=10');

if that doesn't work try


echo wp_get_archives('title_li=&amp;type=postbypost&amp;limit=10');

Where limit is how many "latest" posts you want to show.

Link to comment
Share on other sites

  • 0

It's not having it, It may not be possible to do it this way, but I don't see why not...

The first one produces no results, the second one just displays the code as text.

... I could try making a new php page and including it. Hummmm.. *gets to work*

Link to comment
Share on other sites

  • 0

the_title, the_excerpt and the_permalink are functions which output the title, excerpt and permalink. You are trying to retrieve a return value, concatenate that to some other strings and then send the resulting string to echo. However, those functions don't return anything, so nothing is added in the string and thus noting can be outputted.

The correct way to do this is by using the get_the_* functions instead. Instead of outputting the value, they return the value so you can use them as PHP strings.

That explains why nothing is inserted in the string, but normally you'd still receive an output with no post properties filled in, like:

&lt;h2&gt;&lt;/h2&gt;&lt;p&gt;&lt;a href=""&gt;Read more...&lt;/a&gt;&lt;/p&gt;

This means that the script still has another error somewhere...

After scanning through your code, I found that you didn't escape the double quotes for the href attribute of your Read more link. You can either use a backslash to escape the double quotes, or use single quotes instead of double quotes around your string.

This should work:

&lt;?php
while (have_posts()): the_post();
    echo "&lt;h2&gt;". get_the_title()."&lt;/h2&gt;";
    echo get_the_excerpt();
    echo "&lt;p&gt;&lt;a href=\"".get_the_permalink()."\"&gt;Read more...&lt;/a&gt;&lt;/p&gt;";
endwhile;
?&gt;

However I can't say anything about the rest of your code, perhaps there are still errors in other places you didn't post.

Link to comment
Share on other sites

  • 0

You could do something similar:

&lt;?php include_once ( '/home/username/public_html/wp-blog-header.php'); ?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Title Of Your Page&lt;/title&gt;
&lt;?php wp_head(); ?&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php get_sidebar(); ?&gt;
&lt;/body&gt;
&lt;/html&gt;

Ah **** I just read how you need it.. First off, ditch PHPFox! Use BuddyPress instead! ;).

Can you run a MySQL query there?

Could always try

SELECT post_title, post_name 
FROM wp_posts 
WHERE post_type = 'post' 
AND post_status = 'publish' 
ORDER BY post_date DESC 
LIMIT 3

$query = "SELECT post_title, post_name FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 3";
$result = mysql_query($query,$dblink);
if(mysql_num_rows($result)) {
	echo '&lt;ul&gt;';
	while($post = mysql_fetch_assoc($result)) {
		echo '&lt;li&gt;&lt;a href="/blog/',$post['post_name'],'"&gt;',stripslashes($post['post_title']),'&lt;/a&gt;&lt;/li&gt;';
	}
	echo '&lt;/ul&gt;';
}

Link to comment
Share on other sites

  • 0

The code I posted is all the code I have. Thanks for the help.

I can't seem to get anything to display. If I include the file however, it gives me a ton of errors, all coming from the phpfox script. Which is progress, I guess. At least I know it's doing something and it's also referencing other wordpress files.

If you're willing, here are the errors I see:

PHP 5: Assigning the return value of new by reference is deprecated - news/wp-settings.php (646)
0 	news/wp-config.php : 76   	Phpfox_Error::errorHandler(2048, "Assigning the return value of new by reference is deprecated", "news/wp-settings.php", 646, Array(2))
1 	news/wp-config.php : 76   	require_once()
2 	news/wp-load.php : 30   	require_once("news/wp-config.php")
3 	wpinclude.php : 4   	require("news/wp-load.php")
4 	module/core/include/component/block/dashboard.class.php(69) : eval()'d code : 1   	require("wpinclude.php")
5 	module/core/include/component/block/dashboard.class.php : 69   	eval()
6 	include/library/phpfox/module/module.class.php : 750   	Core_Component_Block_Dashboard-&gt;clean()
7 	include/library/phpfox/phpfox/phpfox.class.php : 204   	Phpfox_Module-&gt;getComponent("core.dashboard", Array(0), "block")
8 	file/cache/template_frontend_default_template_template.html.php.php : 168   	Phpfox::getBlock("core.dashboard")
9 	include/library/phpfox/template/template.class.php : 1763   	require("file/cache/template_frontend_default_template_template.html.php.php")
10 	include/library/phpfox/template/template.class.php : 1244   	Phpfox_Template-&gt;_getFromCache("theme/frontend/default/template/template.html.php")
11 	include/library/phpfox/phpfox/phpfox.class.php : 878   	Phpfox_Template-&gt;getLayout("template")
12 	index.php : 42   	Phpfox::run()

There are about 5 different errors which look like this, with the same description, just on different lines.

And at the end, we have:

Fatal error: Call to undefined method stdClass::set_prefix() in /mounted-storage/home19b/sub001/sc19698-ZJBN/www/news/wp-settings.php  on line 287

.. and as for ditching the script, It's not my site! I just design the thing. :cry: The owner bought the full pro script with the branding removal option a while back, he'd go nuts if we had to change it. :laugh:

*stomps feet* the code Cupcakes posted displays... you guessed it. Nothiiing. I've told the owner how much I dislike the script but he's not gonna part with it. Nooo way.

Link to comment
Share on other sites

  • 0

I wish we could see more of how it's set up because right now with the limited information we have I can't really see how to help you much =(

Link to comment
Share on other sites

  • 0

Hmm. I'm not really sure how to demonstrate how it works.

screen.PNG

This is what I'm seeing, it's a sort of include system? I've set the hook so it appears on the index, which it does.. But nothing shows. :[

Link to comment
Share on other sites

  • 0

Try this:

&lt;?php
define('WP_USE_THEMES', false);
require('./blog/wp-load.php');
query_posts('showposts=1');

while (have_posts()): the_post();
	echo '&lt;h2&gt;'.get_the_title().'&lt;/h2&gt;';
	echo '&lt;p&gt;'.get_the_excerpt().'&lt;/p&gt;';
	echo '&lt;p&gt;&lt;a href="'.get_permalink().'"&gt;Read more...&lt;/a&gt;&lt;/p&gt;';
endwhile;
?&gt;

Note: the_title() echo's out the title while get_the_title() only returns it. Most of them use the same structure, the_excerpt() echo's it but get_the_excerpt() returns it.

Good luck.

Link to comment
Share on other sites

  • 0

Jamesy, make sure your absolute path is correct too :).

Looks like it might be: /mounted-storage/home19b/sub001/sc19698-ZJBN/www/news/

If /news/ is where your Wordpress install is located. :)

There could be an issue with the ./ in the path if it's not correlating with the correct location.

Who is hosting the website?

Link to comment
Share on other sites

  • 0

Hmm.

Hosting is provided by servage.net.

As for the code ++iHazCool has posted, it gives a similar result to what I found before - I get tons of errors and the script stops loading everything at the point of where the php code was inserted. -_____-

I've got a demo admin account if anyone wants to take a bash at it themselves.. My knowledge of php is pretty much zero.. My skills are in design only. :no:

Using the absolute path, it has found the wordpress file, and it is referencing other wordpress files such as wp-settings.php and wp-config.php, but it's just a mountain of errors.

Also, just to make you all feel better, I managed to completely break the script the other day by hooking some code into the header - it made every page blank, including every admin cp. I had to go into phpmyadmin and drop all the tables and re-create them, and delete all directories and re-upload them.. and change the config.. and then change it again. Nightmare. All that from dropping a bit of code in. I wish this script gave out error messages instead of just blank pages. >_>

Link to comment
Share on other sites

  • 0

Ideally speaking scripts would/should have a config switch which can be used to toggle display of errors. If you are lucky the script you are trying to use might have one.

Link to comment
Share on other sites

  • 0

You could check your root directory or the script directory for an errors file. This would help you out as that would log all errors although not sure what kinda of control panel/setup they have so that might not be available.

I once had an 450MB file of errors when I was helping a client figure out an issue.

Link to comment
Share on other sites

  • 0

The script I'm using has a 3-level set of debug modes, would that help?

Unfortunately the debug mode is designed to output errors in the footer. But, since the script simply halts after the wordpress stuff.. no footer is loaded, therefore no debug information. Retarded is one word I'd like to use here. :unsure:

Is wordpress written in php5? Because phpfox is a php5 & mysql5 script and the host I'm using has php5.2. There won't be like an incompatibility anywhere will there?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.