【问题标题】:how to put "edit" link under each excerpt in Wordpress?如何在 Wordpress 的每个摘录下放置“编辑”链接?
【发布时间】:2016-05-11 06:26:07
【问题描述】:

在我创建的博客中,当您以管理员身份查看实时站点时,每篇文章的底部都有一个编辑链接。

网站客户端正在寻找一种方法在“blogroll”主页上添加该编辑链接,以便它出现在每个博客文章摘录的下方。

我已经在谷歌上寻找答案,但找不到任何东西。鉴于我对 Wordpress codex 条件逻辑的理解,我什至认为这不可能。

谁能告诉我这是否可能以及如何解决这个问题?

==更新==

在 Tim 的指导下,我找到了 content.php,其中正在为博客卷生成摘录逻辑。像这样插入 Tim 提议的代码:

<?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; ?>

只需确保它包含在 PHP 函数调用中(即 )。 似乎很有效。

【问题讨论】:

    标签: php wordpress edit codex


    【解决方案1】:

    鉴于我对 Wordpress codex 条件逻辑的理解,我什至认为这是不可能的。

    这绝对是可能的! :)

    在您的主题中,您很可能有一个 home.phparchive.phpindex.php 文件在您的帖子循环中运行。在该文件中,会有如下代码:

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

    您需要直接找到放置此内容的位置,但该 while 循环中的某处将是您的摘录(很可能看起来像 get_the_excerpt(); 或类似名称)。之后就可以放置edit_post_link()函数了。

    包装在一个条件中以检查用户是否已登录并有权查看帖子,这将打印出您正在寻找的“编辑帖子”链接:

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

    您将其放置在何处取决于您的主题以及它的构建方式,但只要环顾四周,您就可以找到它。随意编辑您的问题,如果它不适合您,请放入您尝试编辑的代码部分。

    如果您将主主题与子主题一起使用,那么您应该在主主题中查找相关的代码部分,并将文件复制到您的子主题中以进行更改。

    【讨论】:

    • 好的,提姆很棒的建议。我设法在我的functions.php文件中得到了一些工作,但它需要在定位上工作。太好了,您建议使用儿童主题(在我阅读您的最后一段之前一直很担心)。与使用 functions.php 文件相比,按照您建议的方式执行此操作有什么好处?
    • 我不会考虑通过 functions.php 做这种事情,因为它更像是一种“主题化”方面而不是逻辑——functions.php 旨在包含修改不同方面的逻辑WordPress 是如何工作的。通过该逻辑执行此操作是可能的,但更复杂 - 正如您所发现的,控制定位更难。主题模板正是针对这种情况:将事物准确地放置在您想要的位置。
    • 好的,我找到了将脚本放在 wp 函数。唯一的问题是它正在打印出功能代码。认为我需要将其包装在自己的 php 调用中。
    • 哦,是的,就是这样。我已经用新信息更新了我原来的问题。
    • 伟大的工作。是的,任何 PHP 代码都必须在 &lt;?php?&gt; 之间,否则 PHP 将永远看不到它,它会被打印为文本 :)
    【解决方案2】:

    你好,这是一个简单的任务

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

    将此代码放在 if admin login 条件中 with in the loop..

    【讨论】:

    • 将尝试尝试此解决方案。谢谢
    • 抱歉,您的建议没有成功。我通过修改我的子主题functions.php文件找到了另一个解决方案。
    【解决方案3】:

    在我使用 GeneratePress 主题的特定 Wordpress 安装中,我将此 php 代码放入我的子主题 functions.php 文件中以解决问题:

    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;
    

    只需要重新定位编辑按钮现在出现在作者旁边的位置,我希望它位于摘录文本下方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多