最新消息: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 - Insert HTML in post, below nav bar but above content?

matteradmin10PV0评论

I'm trying to insert a dropdown (via HTML string) in to every post I have.

I'd like to add it before the post information, but below the website nav. bar and header.

In the screenshot, you'll notice it's added to the very top. I want it to be moved in the designated area.

So, you can see the "Choose an Article" and dropdown are at the top-top. I would like them inserted just before the Content (where <div id="content">).

Current code:

add_action('wp_head', 'append_header');
function append_header(){
    //Close PHP tags 
    ?>
    <script type="text/javascript" src="/wp-content/themes/grow-minimal-child/customjs.js"></script>
    <?php //Open PHP tags
    $args = ['post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1];
    $titles = get_post_titles($args);
    $dropdown = create_dropdown_html($titles);
    echo $dropdown;
}

I've tried also using the_content and appending the $dropdown html to that, but this just inserts the drop down after the "Test Post Please Ignore" and post info line. See this screenshot

How do I place the code to hook after the header and nav bar, but before the content itself?

I'm trying to insert a dropdown (via HTML string) in to every post I have.

I'd like to add it before the post information, but below the website nav. bar and header.

In the screenshot, you'll notice it's added to the very top. I want it to be moved in the designated area.

So, you can see the "Choose an Article" and dropdown are at the top-top. I would like them inserted just before the Content (where <div id="content">).

Current code:

add_action('wp_head', 'append_header');
function append_header(){
    //Close PHP tags 
    ?>
    <script type="text/javascript" src="/wp-content/themes/grow-minimal-child/customjs.js"></script>
    <?php //Open PHP tags
    $args = ['post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1];
    $titles = get_post_titles($args);
    $dropdown = create_dropdown_html($titles);
    echo $dropdown;
}

I've tried also using the_content and appending the $dropdown html to that, but this just inserts the drop down after the "Test Post Please Ignore" and post info line. See this screenshot

How do I place the code to hook after the header and nav bar, but before the content itself?

Share Improve this question edited Oct 19, 2018 at 2:43 Gonçalo Peres 1171 gold badge1 silver badge11 bronze badges asked Oct 18, 2018 at 19:48 BruceWayneBruceWayne 1519 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

Edit your single.php or single-post.php template, depending on which one is present and in use with your theme.

The JS file should be registered and conditionally enqueued when the template is loaded. This allows for dependency management and lots of other easy management actions.

Another way to consider is make your menu an actual WordPress menu or a widget and register a sidebar in your single.php template. Either way, you have the benefit of managing the content via the CMS as intended.

EDIT: your theme is likely making use of body classes which makes it very easy to know which template you need to edit. Look at the body tag and inspect the classes there.

With wp_head hook you are inserting content into the <head> of the HTML page, which is not what you want. Also, with the_content, you are basically inserting stuff into the post content.

You can hook to the loop_start action, I believe that will give you the results you need.

Note that this will add your content before any loop you have on your website. You might want to access the WP_Query object you have in parameters, and do additional checks.

Post a comment

comment list (0)

  1. No comments so far