Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End How to put code in Loop?

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #240376
    Doom_87
    Participant

    Hello. It would be great if someone here could help me because I keep looking and looking but can’t find a solution since I’m new to WordPress. I need to put a plugin’s code inside the theme’s loop but I don’t know where exactly I should place it. I don’t even know which theme file contains the loop. Is it the style.css?

    Anyway, the plugin is for displaying featured images inside the posts and it’s this one:

    https://wordpress.org/plugins/featured-image/

    Until now I have been putting manually the shortcode before each post but I want it to be automated. The creator of the plugin says this is the code that must be placed inside Theme Loop:

    <?php if ( function_exists(‘get_featured_img’) ) get_featured_img(); ?>

    I tried to contact him but unfortunately he has not replied.

    I hope you could help me!
    Thanks.

    #240422
    Atelierbram
    Participant

    Probably in a single.php template (“single” stands for “single posts”), somewhere in this while statement. More info about the default WordPress loop

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    // stuff
    
      <?php if ( function_exists('get_featured_img') ) get_featured_img(); ?>
    
    // stuff
    
    <?php endwhile; ?>
    
    #240428
    Doom_87
    Participant

    Thanks for answering.
    Here’s my single.php so I hope you could point out easier where exactly to put it:

    http://codepen.io/Doom_87/pen/ZWrJpg

    #240466
    Atelierbram
    Participant

    I read through your initial question again, and are now thinking you don’t really need this plugin for what you want.

    In your functions.php you should have a line like this: add_theme_support( 'post-thumbnails' );

    Then in your single.php one can do something like:

    <div class="entry-content entry-single entry-text entry-blog">
    <?php
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
      the_post_thumbnail();
    }
    ?>
    
    <?php the_content(); ?> 
    
    </div><!-- .entry-content --> 
    ?>   
    

    See also: Featured Images & Post Thumbnails

    If unsure, you can also always look into a barebones theme like Underscores

    #240472
    Doom_87
    Participant

    Thanks for the answer!

    Here’s how my single.php looks like:
    http://pastebin.com/Sv678HdA

    #240502
    Atelierbram
    Participant

    We will have to start digging a bit deeper: in your single.php file on line-number 14 there is this code-snippet:

    <?php get_template_part( 'content', get_post_format() ); ?>
    

    The first piece get_template_part is a WordPress implementation in a custom syntax of the so called “PHP include”.

    The second piece get_post_format tells the template engine to look for a file which starts with “content”, then a hyphen and then the name of the “post format“.

    If there is no post-format set, then it probably defaults to something like content-single.php.

    So the file which is included, and which you will have to look for now is either content-single.php or content-aside.php, content-gallery.php and so on.

    Now in this file you can do what I wrote about before: put this snippet in:

    <?php
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
      the_post_thumbnail();
    }
    ?>
    

    and in functions.php

    add_theme_support( 'post-thumbnails' );
    
Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.