0

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.

enter image description here

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?

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.

  • "...to make your menu an actual wordpress menu". Hmmm, so you mean instead of kludging together this HTML dropdown, I can build a "proper" menu out of all my post links? That sounds awesome... I'll have to look in to that. -- Regarding the Body Tag, sincere apologies, where do I check that and know what class comes from which file? – BruceWayne Oct 18 '18 at 20:59
  • Yes! Less kludge = easier to manage. In the front-end, look at the page source or inspect the page, find the actual <body> open tag. Assuming body classes are used in your theme, you'll see something like this: <body class="home page-template page-template-home-php"> which indicates the file "page-template-home.php" is your target. – jdm2112 Oct 18 '18 at 21:08
  • Thanks!! I'm really not trying to keep adding straggler questions, but a quick one that would help my big-picture understanding with WP: I've read about how functions.php (in my CHILD theme) is the place to store code and snippets. Therefore, I've been trying to keep it all there...however, it seems like there may indeed be a good reason to split code up and move it around different .phpfiles. So - would it be safe to say that generally, keeping all your custom code in one file is not preferred over storing the code in smaller misc. files, i.e. single.php, header.php, etc? – BruceWayne Oct 18 '18 at 21:22
  • Not a problem - happy to help. One of the strengths of WordPress is its flexibility; there is always more than one way to do something. The best explanation I have seen for what goes in the functions file is to treat it like a plugin file. Extra functionality involving queries, post sorting, API calls, etc. Create a function in functions.php that you can call from a template or a partial. You are extending the WP core with extra functions that won't be overwritten with a WP update. Core functions like the_title() for example, used in lots of templates but defined once in functions.php – jdm2112 Oct 18 '18 at 21:40
0

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.

  • Thanks for the note - loop_start "Fires once the loop is started." - would that work for me, because The Loop fires after loading headers, before content? – BruceWayne Oct 18 '18 at 20:22
  • 1
    Exactly. I just tested it on my test project, it adds content after the header, and before the post. But note that this will add your content wherever you have a loop on your website. – dboris Oct 18 '18 at 20:26
  • 1
    I actually think it is way better to edit your template files and manually add what you need only on the templates you want. – dboris Oct 18 '18 at 20:26
  • Using loop_start returns an error: Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 8192 bytes) in /opt/bitnami/apps/wordpress/htdocs/wp-includes/wp-db.php on line 1889 – BruceWayne Oct 18 '18 at 20:58
  • That points to a probable infinite loop, or something similar. You should double check your code which generates this dropdown. – dboris Oct 18 '18 at 21:02

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.