1

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.

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.

Ryan Coolwebs
  • 1,611
  • 5
  • 22
  • 44

3 Answers3

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.

Tim Malone
  • 3,364
  • 5
  • 37
  • 50
  • Ok, awesome advice Tim. I managed to get something working in my functions.php file but it needs work on the positioning. Great that you suggested using a child theme (was getting worried until I read your last para). What is the merit of doing it your suggested way vs using the functions.php file? – Ryan Coolwebs May 11 '16 at 23:20
  • I wouldn't consider doing this sort of thing through the functions.php because it's much more a 'theming' sort of aspect rather than logic - functions.php is meant to contain logic to modify different aspects of how Wordpress works. Doing this through that logic is possible, but more convoluted - and as you've found, it's harder to control the positioning. The theme templates are exactly for this situation: to place things exactly where you want them to be. – Tim Malone May 11 '16 at 23:23
  • Ok I have found the right place to put the script in the content.php file under the wp function. Only issue is that it's printing out the function code. Think I need to wrap it in a php call of its own. – Ryan Coolwebs May 11 '16 at 23:48
  • Oh yeah, that's it. I have updated my original question with the new info. – Ryan Coolwebs May 11 '16 at 23:50
  • Great work. Yep, any PHP code always has to be between ``, otherwise PHP will never see it and it'll get printed out as text instead :) – Tim Malone May 12 '16 at 00:28
0

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..

0

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.

Ryan Coolwebs
  • 1,611
  • 5
  • 22
  • 44