manythingsme
Member
Posted 4 months ago #
Hello!
Is there a way to insert php into a page directly from the WP Admin? I'd like to insert a "query by tag" into one of my pages. (Using a template is out of the question, as is using the "/tags/tagname" url.)
Specifically, I'd like to insert this code into the page:
<?php
// The Query
$query = new WP_Query( 'tag=cooking' );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();?>
How would I do that?
Thanks!
There's a plugin for that.
http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/
I haven't used it but you can search the following link for other plugins that can help.
http://wordpress.org/extend/plugins/search.php?q=PHP
manythingsme
Member
Posted 4 months ago #
Cool, thanks!
What code do I need to use to display all posts with a certain tag? I tried the code above, but it didn't work. :/
You need to become a bit more familiar with WordPress's The Loop if you want to get results like what you described.
Also, check into using The Loop within The Loop if you want what you described.
One last thing, if you only need PHP within posts and pages to display categories or tags or any "taxonomic" situation, you may want to consider using plugins to do just that rather than hard coding PHP into the posts. I just figure that hard-coding removes your ability to use the Visual editor on a whim.
This will get one instance of each tag, and opens a tag archive for all posts with the tag. And in a specified category.
If you don't want to put it in your template you can use it in a php widget, then maybe use "Widgets on Pages" plugin to get it on the page.
[code moderated per forum rules - please use the pastebin]
manythingsme
Member
Posted 4 months ago #
Thanks for the suggestion of using Widgets on pages...I ended up finding "posts By tag", a plugin which does exactly what I need it to do (though, for some reason it screws up the "read more" button. Thoughts?)
Thanks for your help!
Not off hand, but you need to be able to control the output for excerpts. Not sure if your plugin is handling that.
You could do this if using my code, but if want a possible plugin solution try "advanced excerpt". It should let you remove or change the read more. If you could cancel the excerpt's "read more"
(through advanced excerpt or by removing the template code) you could make one mannually using the_permalink.
<p><a href="<?php the_permalink(); ?>">read more</a></p><?php
but you'd have to add it to the output of your tag query..not sure how to do that with the plugin. I'd have to know what functions it uses, It could be a job finding out.
I just reviwed a little better, my code doesn't effect the output of the post,(the_excerpt) it just does the query, if your plugin is similar, not sure why the read more would be effected. Was it okay originally?