Add dynamic content before the loop
-
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.
-
Give an example of the type of output you would like to get.
<div id="slider> <ul> <li>A slide</li> <li>A slide</li> <li>A slide</li> <li>A slide</li> </ul> <div>I’ve simplified the output as well, hope this could be helpful.
The content is retrieved dynamically with some permalinks, but I don’t thinks this is an important piece of information, what I would know is: is there a way to add this content before each post in the homepage using some kind of hook?I tried with
the_content, but it doesn’t work as I wouldI see only one solution: to tell the user he has to edit the theme code in order to enable the slider where he wants.
I see. You can put things into the_content but that puts it below the post title and other stuff. You could put a hook on the_post() but that could result in some weird behavior.
I see only one solution: to tell the user he has to edit the theme code in order to enable the slider where he wants
That’s one solution and probably the easiest most reliable way to do it.
Another way to do it is to test if
did_action( 'wp_enqueue_scripts' )fired which would mean thewp_headwas done and you’re probably in the main loop right after the head.Give this untested code a look. I had to do something similiar with WP-PageNavi in my theme.
// Insert slider div before the posts but after wp_head add_action( 'loop_start' , 'mh_main_loop_test' ); function mh_main_loop_test( $query ) { global $mh_loop_count, $mh_slider_done; if ( did_action( 'wp_enqueue_scripts' ) == 1 ) { $mh_loop_count++; if ( $mh_loop_count == 1 and !$mh_slider_done ) { // Slider div goes here $mh_slider_done = true; } } }If
wp_enqueue_scriptsalready fired,$mh_loop_countis 1, and$mh_slider_doneis false then you’re probably in the loop right after thewp_headabove the posts, post titles, content, etc.Could you explain better your solution? It’s not so clear to me
Glad to but I may do it in a roundabout fashion that may be more unclear. š
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.
Ideally WordPress would have a hook like
wp_before_loop_after_headand many theme frameworks do have a hook like that. As far as I know that function doesn’t exist in the stock WordPress distribution.You want to output HTML inside the body but before the posts, titles, content, etc. Hooking into the
loop_startwill do that for you. But there can be many loops executed in one page view and just hooking that without some tests would put your HTML output all over the place.What this code does is run at the start of each loop but it checks if the loop is after an action that runs in
wp_head.If that action has been executed then the function starts counting the number of times the loop is ran.
You want your HTML output at the top so that would be the first loop and if that’s the case then output your HTML. I added
$mh_slider_done = true;but that’s really not necessary and is a remnant of the code I copied.I hope that explanation was clear if not let me know here.
Much better now, I’ll try your solution, thanks a lot for the support and patience š
IT WORKS!!!!
Thanks a lot Jan šIt did? Holy Catfish!
I mean, I’m glad to help. š
Edit: The caveat is that this will work in most themes. As long as you put that caveat in your plugin readme.txt file then this should be fine.
Hi, I need to add text and images instead of slider. What should I do? Do I need to add above code to content.php?
Is that the only php file I need to edit?
The topic ‘Add dynamic content before the loop’ is closed to new replies.