how to put “edit” link under each excerpt in WordPress?

In the blog I have created, there is an edit link at the bottom of each post when you look at the live site whilst logged in as admin.

Website client looking for a way to have that edit link on the ‘blogroll’ homepage so that it appears underneath each blog post excerpt.

Read More

I’ve looked all over google for an answer but can’t find anything. I don’t even think it’s possible given what I understand about WordPress codex conditional logic.

Can someone please let me know if it is possible or not and how to approach this problem?

==Update==

Ok with Tim’s guidance, I located the content.php where the excerpt logic was being produced for the blog roll. Inserted Tim’s proposed code like so:

<?php if ( true == generate_show_excerpt() ) : ?>
    <div class="entry-summary" itemprop="text">
        <?php the_excerpt(); ?>
                        <?php
                        if(is_user_logged_in() && current_user_can("edit_post", get_the_ID())){
                           edit_post_link("Edit this post");
                        }
                        ?>
    </div><!-- .entry-summary -->
<?php else : ?>
    <div class="entry-content" itemprop="text">
        <?php the_content(); ?>
        <?php
        wp_link_pages( array(
            'before' => '<div class="page-links">' . __( 'Pages:', 'generate' ),
            'after'  => '</div>',
        ) );
        ?>
    </div><!-- .entry-content -->
<?php endif; ?>

Just had to make sure it was wrapped in a PHP function call ( i.e. ).
Seems to work the treat.

Related posts

3 comments

  1. I don’t even think it’s possible given what I understand about WordPress codex conditional logic.

    It most definitely is possible! 🙂

    In your theme, you’ll have most likely a home.php, archive.php or index.php file that is running through a loop of your posts. In that file, there’ll be code that looks like this:

    while(have_posts()): the_post();
      // lots of code here to display your post data
    endwhile;
    

    You’ll need to locate directly where to put this, but somewhere in that while loop will be your excerpts (most likely looking like get_the_excerpt(); or similar). After that, you can place the edit_post_link() function.

    Wrapped in a condition to check that a user is logged in and has permission to view the post, this will print out the ‘edit post’ link you are looking for:

    if(is_user_logged_in() && current_user_can("edit_post", get_the_ID())){
      edit_post_link("Edit this post");
    }
    

    Where exactly you place this depends on your theme and how it is constructed, but with a bit of looking around hopefully you will be able to locate it. Feel free to edit your question and place in the portion of code you are trying to edit if its not working for you.

    If you’re using a main theme with a child theme, then you should look for the relevant portion of code in your main theme, and copy the file into your child theme to make changes.

  2. Hello this is a simple task

    <a href="<?php get_edit_post_link($post->ID) ?>">Edit</a>
    

    Put this code inside the if admin login condition with in the loop..

  3. In my particular WordPress installation using GeneratePress theme, I put this php code into my child theme functions.php file to solve the issue:

    if ( ! function_exists( 'generate_posted_on' ) ) :
    /**
     * Prints HTML with meta information for the current post-date/time and author.
     */
    function generate_posted_on() 
    {   
        $date = apply_filters( 'generate_post_date', true );
        $author = apply_filters( 'generate_post_author', true );
    
        $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
        if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) )
            $time_string .= '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s</time>';
    
        $time_string = sprintf( $time_string,
            esc_attr( get_the_date( 'c' ) ),
            esc_html( get_the_date() ),
            esc_attr( get_the_modified_date( 'c' ) ),
            esc_html( get_the_modified_date() )
        );
    
        // If our date is enabled, show it
        if ( $date ) :
            printf( '<span class="posted-on">%1$s</span>',
                sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
                    esc_url( get_permalink() ),
                    esc_attr( get_the_time() ),
                    $time_string
                )
            );
        endif;
    
        // If our author is enabled, show it
        if ( $author ) :
            printf( ' <span class="byline">%1$s</span>',
                sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%1$s <a class="url fn n" href="%2$s" title="%3$s" rel="author" itemprop="url"><span class="author-name" itemprop="name">%4$s</span></a></span> %5$s',
                    __( 'by','generatepress'),
                    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
                    esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
                    esc_html( get_the_author() ),
                    edit_post_link( __( 'Edit', 'generate' ), '<span class="edit-link">', '</span>' )
                )
            );
        endif;
    
    }
    endif;
    

    Just need to reposition where the edit button appears as right now it’s next to the author and I want it to be under the excerpt text.

Comments are closed.