hooks - Add function to every post?

admin2025-06-07  44

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.

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.

Share Improve this question asked Oct 17, 2018 at 21:09 BruceWayneBruceWayne 1519 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749252248a317629.html

最新回复(0)