Site logo

[Resolved] Adding custom code using elements

Home Forums Support [Resolved] Adding custom code using elements

Home Forums Support Adding custom code using elements

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1581156
    ETO

    Hello!
    Here you can find two custom codes I need to use in inc/structure/archive.php: https://www.diffchecker.com/e7cgNx0q

    I would like to add these codes to the same file using Elements so that I can update the theme without having to add them manually each time.

    I would be grateful if you could find a solution to this issue?

    #1581210
    Elvin
    Staff
    Customer Support

    Hi,

    I’ve checked your code and a some part of it is from <style> tag.

    Style tags can be placed anywhere so you don’t have to worry where you place them.

    As for this:

    <?php if ( function_exists('yoast_breadcrumb') ) {
      yoast_breadcrumb( '<p id="breadcrumbs">','</p>' );
    } ?>

    Place it inside a Hook Element and hook it on generate_before_archive_title and set the display rule to the page you want it to apply on.

    As for this:

    <div style="width:100%;">
    		
    		<?php $term = get_queried_object();
    $term_id = $term->term_id;
    $taxonomy_name = $term->taxonomy;
    $termchildren = get_term_children( $term_id, $taxonomy_name );
    echo '<ul>';
    foreach ( $termchildren as $child ) {
        $term = get_term_by( 'id', $child, $taxonomy_name );
        echo '<li><h2><a href="' . get_term_link( $term, $taxonomy_name ) . '">' . $term->name . '</a></h2></li>';
    }
    echo '</ul> </br></div>'; ?>

    Place it inside another Hook Element and hook it on generate_after_header and set the display rule to the page you want it to apply on.

    #1581285
    ETO

    Thank you, Elvin.
    If I do just as you wrote above, will there be any difference in terms of the place the breadcrumbs and the other code appear on my site?

    #1581298
    Elvin
    Staff
    Customer Support

    If I do just as you wrote above, will there be any difference in terms of the place the breadcrumbs and the other code appear on my site?

    For the contents of <style>, you can actually place them within Appearance > Customize > Additional CSS if you don’t need display rule condition for it. No need to hook it if its not something you want conditions placed on.

    For the breadcrumbs, most users place them on the generate_after_header hook which comes right after the closing </header> tag but on your code, you placed it before the do_action( 'generate_before_archive_title' ); line so I just assumed you want to place it on the generate_before_archive_title hook.

    If you read the file, there are actually clues as to which hook to best place things into. Look out for those do_action, add_action and the markup closing tags.

    #1582048
    ETO

    Thank you so much, Elvin. I have successfully transferred the codes above to hooks.
    However, there are several codes in a few files, which my theme developer added although I mentioned the hooks of GP theme.

    The next custom code is in the content-single.php file: https://www.diffchecker.com/KjN2aGIn

    I placed this code on both generate_after_content and generate_before_comments_containerhooks. But it breaks theme.

    What shall I do for this?

    #1582184
    Elvin
    Staff
    Customer Support

    Thank you so much, Elvin. I have successfully transferred the codes above to hooks.
    However, there are several codes in a few files, which my theme developer added although I mentioned the hooks of GP theme.

    If there are actually more codes than initially mentioned, it may be best to actually create a child theme. If your site developer made templates, then it may be best to just add it in along with the other child theme files.

    Tip: Hooks Element is for presentation purpose. It’s for inserting tidbits of markups.

    Add this CSS so the comments area wraps under the content area.

    .site-content {
        display: flex;
        flex-wrap: wrap;
    }
    #1584862
    ETO

    Thanks, Elvin!

      functions.php
      header.php
      archive.php
      content-single.php

    I have custom codes in the files I listed above. We couldn’t use child theme as there were some codes in inc/structure/archive.php file and child theme supports only the files in the main directory of the theme. However, I managed to move the custom codes in this file to the hooks in elements thanks to your help.

    Now I wonder which method is better to choose, transfer the custom codes from the listed files above to hooks and the Snippets plugin OR use a child theme? What is the difference and which is better to use considering the future updates of the main theme, WordPress, plugins, etc?

    #1585072
    Elvin
    Staff
    Customer Support

    We don’t recommend making changes to the default theme files as any changes made to them will be wiped once the theme updates.

    Now I wonder which method is better to choose, transfer the custom codes from the listed files above to hooks and the Snippets plugin OR use a child theme?

    Since you seem to be doing lots of changes so child theme is the way to go.

    Code snippets is for general use of smaller, fewer snippets. Child theme is for multiple big changes like template editing(what you’re doing now).

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.