WordPress - how to insert html code selectively in posts - wordpress

I want to display custom html code when a post is rendered (so not when is inserted into the database).
I currently do this with add_filter('the_content', 'my_custom_method'). The only problem is that I want this do be displayed only inside the post (when is viewed in its own page), not when all posts are rendered .
I banged my head against the wall, but couldn't find any method to tell me if i'm currently inside an individual post or not (this has to work for every url rewriting possible, so i can't rely on url)
Is there such a method? I believe it should be, but i can't find it. Thanks.

the function for checking if the post is in its own page is is_single()
add_filter('the_content', 'my_custom_method');
function my_custom_method(){
if(is_single()){
//code for your custom html code
}
}
the function is_single() checks if the page being rendered is a single page or not.

The easiest way to do this would be to modify your templates. Wordpress template sets should have a file named single.php (inside wp-content/themes/<theme name>). This is the page that gets rendered when you are viewing the page for a single post.
You could edit this file and insert whatever you needed to for the posts there.

Related

What's the best way to insert HTML chunk via WordPress plugin function?

I am creating WordPress plugin and I want to be able to provide an interface (to the programmer that installs the plugin) through some registered function in WP that when called in the template prints a HTML chunk inside the page.
What's the best way of doing it? I don't want to use echo to print large chunk of HTML from inside the function. The ideal would be having a .html file or .php file with the HTML code in it and my plugin function would simply load it when the function is called.
Does that make sense? Should I be using widgets API for this instead?
Thank you.
Something like this, which I use for an rss feed. if you remove the is feed conditional clause you should be able to just add content.
/*-----------*/
/* Add custom feed content footer
/*-----------*/
function add_feed_content($content) {
if(is_feed()) {
$content .= '<p>*This post was first published on...*</p>';
$content .= '<footer><p>*Add some Content here*</p></footer>';
}
return $content;
}
add_filter('the_excerpt_rss', 'add_feed_content');
add_filter('the_content', 'add_feed_content');
You might also need to create an admin page with a form to store the content and retrieve it if it likely to change, or just hard-code it here if it is a one-off.

Add dynamic content before the loop in Wordpress

I'm developing a content slider for Wordpress and I would know how is possible to put this content slider (as html content) into the body before the loop.
I tried to filter the content with add_filter('the_content', 'functionName), but I get the content slider before each post.
If you use add_filter('the_content'), your function will be called everytime the content of a post is output, whether a single post or a series of posts in a loop. If you need to "hook" before any post content is output in a page, the only dynamic parts of all WP themes you can reach are get_header() or get_sidebar() (or event get_footer). So your best luck would be not to use a filter with the content, but an action, with get_header, like this :
add_action('get_header', 'your_function'); // Add priority & param args if necessary
The problem is that this gets executed before header.php is called, and usually, the body tag is opened in header.php...
That is if you cannot modify the theme itself. Otherwise, you can easily add an action in the theme itself and execute it where you want.
Another technique would be to add your html content after document is ready, through JavaScript which you can output in the footer.

Making a plugin operate through a shortcode

Many plugin need some [shortcode] to be placed in a page, sometimes within the loop. But usually it only makes the actual [shortcode] appear where I placed it and nothing else !
For example such and such contact form plugin asks me to put [contact form plugin] in my contact page and I'm supposed to see a form appearing there as a result, but instead I see a blank page with the shortcode appearing.
I'm relatively new to WordPress so this question must sound stupid, stil can anybody take the pain to explain to me ?
AFAIK, [shortcode] is intended to be appeared or operated within the loop. if you want it to display outside it (says, on sidebar, or on footer) you need to manually compute it's value.
To do that, you can use do_shortcode() function:
<?php echo do_shortcode ( '[your-shortcode-text]' ); ?>

How do I load jQuery for only one specific page in my WordPress theme?

I'm fiddling with a WordPress theme. I'm aware I can use wp_enqueue_script in my header.php to load WordPress's supplied jQuery library.
I was just going to use wp_enqueue_script in my header, but it seems inefficient when I only want to use it on a particular Page (just on one single page with a particular page_id.)
Given that, what's the best way of including jQuery only on one particular Page?
Presumably I can't do page_id detection in header.php, because that won't be in The Loop, right? But I'm guessing I'm missing some fairly obvious method -- I'm fairly new to theme development...
Thanks!
Yes you can, is_page doesn't need to be called in The Loop, since it doesn't change when The Loop runs. So is_page(42) will only return TRUE if you're on the page with id 42. It also works with the page title or name (slug), which might be more future-proof if you ever replace delete this page and replace it with a new one with the same slug.
here is an article about dynamic body ids
http://perishablepress.com/press/2009/05/26/dynamic-body-class-id-php-wordpress/
after you get your page name you can add a conditional statement in your template index.php that says something like this in your page header or before the closing body tag:
// $page_name would be the page name you extracted with a function from the post
if($page_name === 'about'){
echo '<script type="text/javascript" src="jquery.js"></script>'
}

How do I create a custom WordPress page?

I want my WordPress blog to have a page called music. On that page I will query the DB for posts with the category music and then change around the look and feel of the posts. So I can't just put a link to /categories/music/ because I want to do custom work on the posts.
Should I put this code in a separate php file and link to it? I think I may lose access to all the nice WordPress API calls if I do that.
I was thinking about using a filter, but I am not sure which one to use. I was thinking something like the following except the_title has not been grabbed yet so I cannot check the title.
function show_music(){
if( is_page() && the_title('','',false) == 'music' ){
echo "got here";
}
}
add_filter('pre_get_posts', 'show_portfolio');
How would you go about this?
You need to put the below code in the file, and then put the file in the Theme folder. Then you can create a page using Wordpress pages and select a page template with the name you put in this comment:
/*
Template Name: Something Goes Here
*/
You need to create custom page within your theme. If you dont have idea how to create custme page or template page in WordPress theme then view my easy tutorial How to create template page in WordPress

Resources