最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

hooks - Add function to every post?

matteradmin8PV0评论

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' );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far