Archived topic
How to add Edit link to Blog Archives
11 replies · Started by Jim on November 14, 2019
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!
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 :)
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?
Should only have minimal impact.
You can do a quick speed test before and after adding the code.
"minimal" is relative... ;-)
Thanks again.
No problem :)
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.
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
Is it possible not to use hook element ?
Thanks.
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).
Thanks, the code is running well.
Nice one. Glad you got it sorted. :)