• Resolved Tizionario

    (@tibasweb)


    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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Give an example of the type of output you would like to get.

    Thread Starter Tizionario

    (@tibasweb)

    <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 would

    Thread Starter Tizionario

    (@tibasweb)

    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.

    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.

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    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 the wp_head was 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_scripts already fired, $mh_loop_count is 1, and $mh_slider_done is false then you’re probably in the loop right after the wp_head above the posts, post titles, content, etc.

    Thread Starter Tizionario

    (@tibasweb)

    Could you explain better your solution? It’s not so clear to me

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    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_head and 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_start will 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.

    Thread Starter Tizionario

    (@tibasweb)

    Much better now, I’ll try your solution, thanks a lot for the support and patience ??

    Thread Starter Tizionario

    (@tibasweb)

    IT WORKS!!!!
    Thanks a lot Jan ??

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    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?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Add dynamic content before the loop’ is closed to new replies.