• I have the following code on index.php I want to include this line of code to display author info on top of the loop.

    <?php include 'author-top.php'; ?><!--author-->

    I want it to display right after the header as shows here

    <?php get_header(); ?>
    /* This is where I want my author div to display */
    <?php include 'author-top.php'; ?><!--author-->
        <div id="primary" class="content-area">
    
            <div id="content" class="site-content-home" role="main">
            <?php if ( is_home() && ! is_paged() ) : ?>
                <h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
            <?php endif; ?>
            <?php if ( have_posts() ) : ?>
    
                      <?php /* Start the Loop */ ?>
    
                      <?php while ( have_posts() ) : the_post(); ?>
    
                    <?php
                        /* Include the Post-Format-specific template for the content.
                         * If you want to overload this in a child theme then include a file
                         * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                         */
                        get_template_part( 'content', 'home' );
                    ?>
    
                <?php endwhile; ?>
    
                <?php spun_content_nav( 'nav-below' ); ?>
    
            <?php elseif ( current_user_can( 'edit_posts' ) ) : ?>
    
                <?php get_template_part( 'no-results', 'index' ); ?>
    
            <?php endif; ?>
            </div><!-- #content .site-content -->
        </div><!-- #primary .content-area -->

    This is what I get: https://gyazo.com/4154248fa37c84f9cb8f52871f186723.png

    When I try to use the same ‘php include’ somewhere else at the bottom of the index.php code everything seems to work well. The problem is I need it on the top so it shows on the top on the small screens.

    Is it anything I’m doing wrong? or it is that wordpress doesn’t allow to do use ‘php includes’ in there and if so why not.
    php author include

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hakkim

    (@hakkimpahammed)

    I could not find any issues in your code. But not sure what you have written in ‘author-top.php’ file. Anyway, can you try rename the file to ‘header-author.php’ and include it in your index.php file by calling it like get_header(‘author’) instead of ‘include()’.

    Thread Starter RaulMV

    (@raulmv)

    Thanks Hakkim,

    I tried that and it doesn’t work neither.

    This is the code I have in author-top.php

    <!-- get author bio **RAUL -->
        <!-- This is the author info displayed at the top of each page -->
    	<div id="author-bio">
    		<div class="author-image"><?php echo get_avatar( get_the_author_email(), '80' ); ?></div>
    		<div class="author-txt">
    			<h2 class="author-name" id="clickme"><?php the_author(); ?></h2>
    			<p class="toggle author-description"><?php the_author_description(); ?></br>
    			<a href="https://www.linkedin.com/in/raulmvicente"><img class="author-social" src="https://www.webleria.com/wp-content/themes/spun/images/linkedin_circle_color.png"></a>
    			<a href="https://www.twitter.com/raulmvicente"><img class="author-social" src="https://www.webleria.com/wp-content/themes/spun/images/twitter_circle_color.png"></a>
    			<a href="https://plus.google.com/116575236589076788314?rel=author"><img class="author-social" src="https://www.webleria.com/wp-content/themes/spun/images/google_circle_color.png"></a></p>
    		</div>
    	</div>
    
    <script>
    $('#clickme').click(function() { $( '.toggle' ).animate({ "height": "toggle", "opacity": "toggle" }, "slow" )});
    </script>

    Hakkim

    (@hakkimpahammed)

    Roul,

    I think, the issue is that, when you are including the file at the top of the page, the code will run before executing the WP loop. So the default parameters of WP will not be set ans so that the functions like ‘the_author’ and ‘the_author_description’ could not fetch the information.

    I am assuming that ’80’ is the author id as it is given in get_avatar( get_the_author_email(), ’80’ ). If so please try using like this instead of ‘the_author’, ‘the_author_description’ etc. See the examples shown at the bottom of the page to retrieve required information of the author.

    Thread Starter RaulMV

    (@raulmv)

    Thanks Hakkim, I didnt have a chance to try this option yet. I just want to let you know that the ’80’in <?php echo get_avatar( get_the_author_email(), '80' ); ?> it is not actually an ID it is the size of the thumbnail. That’s the way the API works.

    If that is the problem, I can set a user ID so I can display the author info outside the loop. Any idea how to give the author an ID?

    Hakkim

    (@hakkimpahammed)

    Sorry, I misunderstood that it was the ID of user.
    But whatever, you should use the ID of the author in the code,whenever you run it outside the loop. You will get ID of the user in admin side->users->Click on the user’s name you want and check the URL, you can see ‘user_id=XX’ like this and XX will be the ID of that user.

    Or you can install Reveal ID plugin. Which will list ID of everything(users, post, page, categories, tags etc) in corresponding section.

    Thread Starter RaulMV

    (@raulmv)

    I will do so asap and come back to the forum if that’s not working. Thanks Hakkim for your kind and useful replies.

    Thread Starter RaulMV

    (@raulmv)

    It is great the code works quite well just by giving an ID to the php tags as shown in this link you gave me.

    Thanks again Hakkim!

    Just to help others with the same problem. The tags I had outside the loop should contain an ID and the syntax should be like this (as shown in the link above):

    <?php the_author_meta('display_name', 1); ?>

    instead of what I had before:

    <?php the_author(); ?>

    Hakkim

    (@hakkimpahammed)

    Glad to here that your problem solved!!!
    Cheers!!!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘php include on index.php top of the page doesnt work’ is closed to new replies.