【问题标题】:Link Wordpress post images to post链接 Wordpress 发布图片以发布
【发布时间】:2013-12-27 09:14:14
【问题描述】:

我正在为我的博客开发一个 wordpress 主题,但遇到了一些困难。

当我发布新帖子时,该帖子的第一张图片会显示在主页上。单击图像时,它会链接到图像文件并显示完整大小。我希望它自动链接到它来自的帖子。我知道您可以在上传或在编辑器中选择图片链接的内容。我希望图像自动链接到他们的原始帖子,因为我有其他人在我的博客上写,我不希望他们每次都这样做。

据我了解,在这种情况下,content.php 文件控制帖子格式。这是我的主题中的文件。或者可以使用函数吗?

    <?php
/**
 * The default template for displaying content single/search/archive
 *
 * @package test
 * @since test 0.1.0
 */
?>
    <!-- START: content.php -->
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="entry-header">
            <?php if ( is_single() ) : ?>
            <h1 class="entry-title"><?php the_title(); ?></h1>
            <?php else : ?>
            <?php the_post_thumbnail(); ?>
            <h1 class="entry-title">
                <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'test' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
            </h1>
            <?php endif; // is_single() ?>
        </header><!-- .entry-header -->
        <?php if ( 'post' == get_post_type() ) : ?>
        <div class="entry-meta">
            <?php posted_on(); ?>
            <?php if ( is_sticky() && is_home() && ! is_paged() ) : ?>
            <span class="label radius secondary"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'test' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php _ex( 'Featured', 'Post format title', 'test' ); ?></a></span>
            <?php endif; ?>
        </div><!-- .entry-meta -->
        <?php endif; ?>
        <?php if ( is_search() ) : // Only display Excerpts for Search ?>
        <div class="entry-summary">
            <?php the_excerpt(); ?>
        </div><!-- .entry-summary -->
        <?php else : ?>
        <div class="entry-content">
            <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'test' ) ); ?>
            <?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'test' ) . '</span>', 'after' => '</div>' ) ); ?>
        </div><!-- .entry-content -->
        <?php endif; ?>

        <footer class="entry-meta">
            <?php if ( 'post' == get_post_type() ) : ?>
            <?php get_template_part('entry-meta', get_post_format() ); ?>
            <?php endif; ?>
        </footer><!-- #entry-meta -->

    </article><!-- #post-<?php the_ID(); ?> -->
    <!-- END: content.php -->

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    https://codex.wordpress.org/Function_Reference/the_post_thumbnail 示例 1. 要将帖子缩略图链接到特定循环中的帖子永久链接,请在主题的模板文件中使用以下内容:

    <?php if ( has_post_thumbnail()) : ?>
      <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
        <?php the_post_thumbnail(); ?>
      </a>
    <?php endif; ?>
    

    【讨论】:

    • 感谢您的回答。我用信息弄清楚了。我将 the_content 更改为 the_excerpt,然后将帖子缩略图移动到标题标签的 href 链接属性中。这允许缩略图和标题成为一个链接。
    猜你喜欢
    • 2010-11-07
    • 2012-07-14
    • 2018-07-28
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多