• Ilse

    (@directionsud)


    I’m trying to set up a child theme. I’ve copied the content of the wp-content/themes/twentyseventeen folder into the wp-content/themes/twentyseventeen-child. I understood the functions.php of the child theme should not be the same as the parent version. I’ve tried to edit the functions.php in the child theme by just using the <?php tag.

    I noticed the functions.php file in my parent theme lost it’s code as well (minus the <?php tag) and that my site broke down. I’ve put back the master code but how can I edit my functions.php file in my child theme without creating the same edits in my parent theme file?

    I understood that the functions.php gets loaded differently than for example the style.css. I’m fairly new to child themes, but I’ve read the basics for the past two hours. Is it normal to have the code from the functions.php child theme file being copied automatically into the parent theme version? I feel like I’m doing something wrong/missing something. Should I start completely over by creating just a style.css and functions.php with only <?php? How do I prevent this gets copied into my parent files?

    Hope anyone can help this newbie ?? Thanks a lot! Regards, Ilse.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    >> I’ve copied the content of the wp-content/themes/twentyseventeen folder into the wp-content/themes/twentyseventeen-child <<

    That’s not what you should do. Only copy over files you need to modify. DO NOT COPY OVER style.css or functions.php.

    If you want an example of a 2017 child, see https://github.com/sterndata/sds_2017

    Moderator bcworkz

    (@bcworkz)

    Adding to what Steven said, if there are theme functions that you wish to alter, first see if there is a filter or action hook you can use to alter behavior. If you do need to copy over a function’s declaration in order to change it, unless the function declaration is wrapped in if ( ! function_exists( 'function_name' )): logic (making it “pluggable”), you must give the function a new name and alter whatever code calls that function. You can only use the same name with pluggable functions, in which case your declaration should also use the if ( ! function_exists( 'function_name' )): logic or you’ll have trouble activating your child theme. Once activated, your version will take precedence.

    Thread Starter Ilse

    (@directionsud)

    Thank you the both of you. I’ve never done anything with PHP (or coding in general) so I’ll try to keep that Pandora’s box lid as closed as possible ?? but open to learning!

    Basically, I’m just looking for a way to create a child theme to be able to show only the excerpt of my blog posts on my blog page instead of showing all my blog posts in full length. The excerpt field in the blog itself doesn’t seem to do the trick (I understood not showing excerpts is a default 2017 setting).

    Moderator bcworkz

    (@bcworkz)

    Full text vs. excerpt behavior is one of the more confusing aspects of WP. There are a few options for you to choose from, but behavior is also influenced by your theme. If your theme forces full text and you don’t want to manually insert <!–more–> tags, adding a child theme with an edited index.php is a viable solution.

    Thread Starter Ilse

    (@directionsud)

    I’ve managed to set up a working 2017 child theme. Then replaced:

    get_template_part( 'template-parts/post/content', get_post_format() );
    with:
    get_template_part( 'template-parts/post/content', 'excerpt' );
    in index.php Line 45 and archive.php Line 40 – as I found elsewhere on this forum.

    You should have heard the loud YES!!! that almost woke up the entire household at 2AM when those excerpts showed up nice and shiny after refreshing ??

    Next challenge: show a thumbnail next (or above/below/..) to the blog archives page (preferably the blog panel on the homepage as well). I noticed a lot has been written about it already but I have a more specific question: I would like to have a thumbnail image (or a cropped version of the feature image of the specific blog post) but not show the feature image on the top of my blog page.

    I’ve tried to add to the template-parts > post > content.php:
    <div class=”entry-content”>
    <?php
    get_the_post_thumbnail();
    the_content(
    sprintf(
    /* translators: %s: Post title. */
    __( ‘Continue reading<span class=”screen-reader-text”> “%s”</span>’, ‘twentyseventeen’ ),
    get_the_title()

    But it doesn’t do the trick, even though a feature image for certain posts has been defined. Is there any (simple) way to show a thumbnail, and not show the feature image on the blog post? (assuming the thumbnail is based on the feature image).

    Thanks for the help (and yes, I realise I’ve drifted away from my initial problem.. but hope someone can help pointing me in the correct direction). Merci ??

    • This reply was modified 3 years, 9 months ago by Ilse.
    • This reply was modified 3 years, 9 months ago by Ilse.
    • This reply was modified 3 years, 9 months ago by Ilse.
    • This reply was modified 3 years, 9 months ago by Ilse.
    Moderator bcworkz

    (@bcworkz)

    Such a simple thing, having one’s code actually work. Yet so rewarding when it does so after a struggle. Be careful, it can be addictive ??

    Removal is a matter of editing the right template to remove the responsible code. Some CSS adjustment might be required if removal leaves a hole. But if you’ve correctly removed the image output code along with the related container HTML, the space ought to close up automatically.

    It’s still a featured image even if it’s less featured and more thumbnail. Output the img tag anywhere you like with the_post_thumbnail('thumbnail'); (specify whatever size you like. I’d more likely use ‘medium’ over ‘thumbnail’) The call still needs to be within the loop context because the current post dictates what image is used. Outside of loop context, use get_the_post_thumbnail() which accepts a post argument. In this case you need to explicitly echo out the return, whereas the_post_thumbnail() echoes for you.

    Thread Starter Ilse

    (@directionsud)

    Sorry, had to read your reply 5 times and still haven’t got a clue were to start. Where should I place the_post_thumbnail('thumbnail'); or the_post_thumbnail('medium'); ? In the content.php?

    Output the img tag? .. The call still needs to be within the loop context?
    ..accepts a post argument? Echo out the return… well you got the idea..

    This is a ‘bit’ too technical for me ??

    • This reply was modified 3 years, 9 months ago by Ilse.
    Moderator bcworkz

    (@bcworkz)

    Sorry for the confusion. You place the call on the template where you want it to appear. Different themes can use different templates for the same purpose. content.php would be a likely candidate. If you have trouble identifying the right template, try the template debugger plugin.

    “Loop context” is going to take some explaining. The templates involved for any given request will somewhere include code for “The Loop”, code which includes

    while ( have_posts()) : the_post();
      //code to output a post
    endwhile;

    Calling the_post() establishes the current post in several global values, all within the scope of this loop. Outside of this loop, determining the current post from these global values is unreliable. the_post_thumbnail() gets the image related to the current post established by these global values, so you cannot reliably use it outside of the loop.

    get_the_post_thumbnail() will also use the current post, but you can instead optionally specify a specific post. So if you’re outside the loop and can get the right post ID through your own means, this function can still be used.

    So which function you use depends on whether it is used within or outside of the loop. Outside of the loop you need to tell it which post you want the image for. There is one exception. On singular pages where only one post had been queried for, the global values established in the loop will retain the current post values after the loop has completed. So for singular templates only, you can still use the_post_thumbnail() after the loop has completed.

    I hope that makes more sense, though I fear I may have added more confusion instead of clarifying.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Setting up a Child theme – functions.php’ is closed to new replies.