[Resolved] How to add Edit link to Blog Archives

Home Forums Support [Resolved] How to add Edit link to Blog Archives

Home Forums Support How to add Edit link to Blog Archives

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1063305
    Jim

    I’m guessing this might require editing a child theme template, but not quite sure which one or exactly how…

    How can I add and style an “Edit” link next to each post on the Blog Archives pages? This needs to only be visible to logged in Admin users. Clicking the link would open the post editor for that specific post, or page.

    Once logged in, the “Edit Post” link in the Admin Bar is fine for editing that post. Our previous theme, however, displayed an “Edit” link under the title of each post on the Blog Archives, tag/category listings, or Search Results page.

    Thanks!

    #1063533
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Try this:

    1. Add a Hook Element: https://docs.generatepress.com/article/hooks-element-overview/
    2. Give it this content:

    <?php edit_post_link( __( 'edit', 'textdomain' ), '<p>', '</p>' ); ?>

    3. Check the “Execute PHP” option.

    4. In the Display Rules, choose your archives/search results and make it so it only appears to logged in users.

    Hope this helps πŸ™‚

    #1064287
    Jim

    Thank you!

    We have not yet activated the Elements module. Does that have any additional impact on PageSpeed performance? For example, are additional resources loaded on all pages, even if no custom elements are being called?

    #1064293
    Leo
    Staff
    Customer Support

    Should only have minimal impact.

    You can do a quick speed test before and after adding the code.

    #1064306
    Jim

    “minimal” is relative… πŸ˜‰

    Thanks again.

    #1064307
    Leo
    Staff
    Customer Support
    #1635086
    Amin

    Hello,

    Below code works on single post, but not on blog archive

    add_action( ‘generate_after_content’,’generate_add_edit_link’ );
    function generate_add_edit_link()
    {
    if ( ! is_singular() )
    return;

    edit_post_link( __( ‘Edit’, ‘generatepress’ ), ‘<footer class=”entry-meta”><span class=”edit-link”>’, ‘</span></footer>’ );
    }

    If I want to add ‘edit link’ on blog archive, what code should I add without using hook ?

    Thanks.

    #1635103
    Elvin
    Staff
    Customer Support

    Hi,

    You can follow Tom’s recommendation.

    Use a Hook Element and place this on the code area:

    <?php edit_post_link( __( 'Edit', 'generatepress' ), '<footer class="entry-meta"><span class="edit-link">', '</span></footer>' ); ?>

    You then set the hook to generate_after_content and set the display rule to “posts – all posts”, “all archives”, “blog” and “search results. Also make sure that “Execute PHP” is checked.

    Hook and Code
    https://share.getcloudapp.com/Jru1AAzB

    Display rules
    https://share.getcloudapp.com/NQuJOOzg

    A wise man once said:
    "Have you cleared your cache?"

    #1635145
    Amin

    Is it possible not to use hook element ?
    Thanks.

    #1635254
    Elvin
    Staff
    Customer Support

    You can try this out.

    add_action( 'generate_after_content','generate_add_edit_link' );
    function generate_add_edit_link()
    {
        if(is_user_logged_in() && current_user_can("edit_post", get_the_ID()) && !is_page() ){
    		edit_post_link( __( 'Edit', 'generatepress' ), '<footer class="entry-meta"><span class="edit-link">', '</span></footer>' );
    	}
        else { 
                return; 
        }
        
    }

    The edit link is visible to logged in users with edit post permission.

    I’ve also added && !is_page() within the condition so it doesn’t appear on static pages (but will appear on blog, archive, search and single posts).

    A wise man once said:
    "Have you cleared your cache?"

    #1635773
    Amin

    Thanks, the code is running well.

    #1636374
    Elvin
    Staff
    Customer Support

    Nice one. Glad you got it sorted. πŸ™‚

    A wise man once said:
    "Have you cleared your cache?"

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.