1

I have some code that generates a dropdown list of all my post files. I would like to add this dropdown to the top of every post page, right before the post content.

What's the correct hook/action to do so?

custom-functions.php:

<?php
function create_post_dropdown($titles){
    ?>
    <div id="article-choice">
    <h3>Choose an Article, or browse below</h3>
    <select onchange="if (this.value) window.location.href=this.value">
        <?php 
        foreach($titles as $title => $url){
            echo "<option value=" . $url . ">" . $title . "</option>";
        } 
        ?>
    </select>
    </div>
    <?php
}
add_action('__after_header', 'create_post_dropdown');
function add_dropdown_to_posts(){
    $args = [
             'post_type' => 'post',
             'post_status' => 'publish',
             'posts_per_page' => -1
        ];
    $posts = new WP_Query( $args );
    $titles = get_post_titles($args);
    if (get_post_type() == "post"){
        $title = $post->post_title; // get_the_title();
        $title = create_post_dropdown($titles) . "<br>" . $title;
    }
    return $title;
}
add_filter('the_content', 'add_dropdown_to_posts');

The idea is that when a post page is opened/viewed, this dropdown (made via create_post_dropdown) will be added before the post content.

1

One way to do that is to have the custom dropdown function return the custom html. Then you can call it inside your add_dropdown_to_posts and put the returned content into a helper variable. After that just prepend the custom html to the $content variable that the the_content filter provides.

Like this,

function my_custom_dropdown_html() {
 return '<html stuff here>'; // you could put your html also in a variable and then return that.
}
function add_dropdown_html_before_the_content($content) {
 $dropdown = my_custom_dropdown_html();
 // if statement just to be safe - e.g you change the custom function output to something else than string of html and forget you've used it here
 if ( $dropdown && is_string( $dropdown ) ) {
  $content = $dropdown . $content; // prepend custom html to content
 }
 return $content;
}
add_filter( 'the_content', 'add_dropdown_html_before_the_content' );
  • Aha! That's so much simpler than what I was trying, and easy to follow and implement. Thanks! – BruceWayne Oct 18 '18 at 14:37
  • This seems to put the dropdown in between the page Header (with the article title, author, etc.) and the content (which makes sense, we're using the_content hook). How could I put it just before the Header HTML? There's no the_header hook that I could find. – BruceWayne Oct 18 '18 at 14:52
  • 1
    I guess it depends on the theme that you're using. If it's some theme from the WP theme repository or a premium theme, then it might provide some custom hooks, which you could use just like the the_content hook. But you need to look at the theme's code to see if there's any hooks available. Or if the page header is a template part, you could copy the part into a matching folder (e.g. /partials) in your child theme and modify it to your liking. And if it's a theme that you're building yourself then you could toss in a custom do_action to your page header file and then use that one. – Antti Koskinen Oct 19 '18 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.