需要在特色图像中添加锚标签以在新选项卡中打开

时间:2017-02-09 作者:James Mascarenhas

我想在代码段中添加target=“\\u blank”属性,以便当有人单击帖子的特色图片时,它会在新选项卡中打开。以下是代码

<?php 
                        $content = get_the_content();
$content = preg_replace(\'/(<)([img])(\\w+)([^>]*>)/\', "", $content);
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]&gt;\', $content);
echo $content;

                        ?>
                        <?php if ( has_post_thumbnail() ) { the_post_thumbnail(\'thumbnailnew2\'); } ?>
                        <?php wp_link_pages( array( \'before\' => \'<div class="page-link"><span>\' . __( \'Pages:\', \'parabola\' ) . \'</span>\', \'after\' => \'</div>\' ) ); ?>
                        </div><!-- .entry-content --> 
                        <?php }  

            endif; 
         cryout_post_after_content_hook();  ?>
我测试了几个小时,发现一切都归结为这一行代码

<?php if ( has_post_thumbnail() ) { the_post_thumbnail(\'thumbnailnew2\'); } ?>
但如果我试图对其进行更改,则会导致错误,我不是WordPress开发人员,但我可以阅读代码。这是我在上述代码中所做的更改。

<?php if (has_post_thumbnail()) : ?>    
    <a href="<?php the_permalink() ?>" title="<?php the_title_attribute() ?>" target="_blank"><?php the_post_thumbnail

(\'thumbnailnew2\'); ?></a>
感谢您的支持

根据Jack的建议编辑#1代码

<?php 
                        $content = get_the_content();
$content = preg_replace(\'/(<)([img])(\\w+)([^>]*>)/\', "", $content);
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]&gt;\', $content);
echo $content;

                        ?>
                        <?php if (has_post_thumbnail()) : ?>    
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" target="_blank"><img src="<?php the_post_thumbnail_url(\'thumbnailnew2\'); ?>" alt="<?php the_title_attribute(); ?>"/></a>
                        <?php wp_link_pages( array( \'before\' => \'<div class="page-link"><span>\' . __( \'Pages:\', \'parabola\' ) . \'</span>\', \'after\' => \'</div>\' ) ); ?>
                        </div><!-- .entry-content --> 
                        <?php }  

            endif; 
         cryout_post_after_content_hook();  ?>

    </article><!-- #post-<?php the_ID(); ?> -->
使用Jack提供的更新代码编辑#2完整代码

<?php
/**
 * The default template for displaying content
 *
 * @package Cryout Creations
 * @subpackage Parabola
 * @since Parabola 1.0
 */

$options= parabola_get_theme_options();
foreach ($options as $key => $value) {
     ${"$key"} = $value ;
} 

?><?php cryout_before_article_hook(); ?>

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <?php parabola_comments_on(); ?>
        <header class="entry-header demo">          
            <h2 class="entry-title">
                <a href="<?php the_permalink(); ?>" target="_blank" title="<?php printf( esc_attr__( \'Permalink to %s\', \'parabola\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
            </h2>
            <?php cryout_post_title_hook(); 
            ?><?php if ( \'post\' == get_post_type() ) : ?>
            <div class="entry-meta">
                <?php parabola_posted_on(); 
                    cryout_post_meta_hook();  ?>
            </div><!-- .entry-meta -->
            <?php endif; ?>


        </header><!-- .entry-header -->
            <?php cryout_post_before_content_hook(); ?>  
            <?php if ( is_archive() || is_search() || is_page() ) : // Display excerpts for archives, search and page templates ?>

                        <?php if ($parabola_excerptarchive != "Full Post" ){ ?>
                        <div class="entry-summary">
                        <?php parabola_set_featured_thumb(); ?>
                        <?php the_excerpt(); ?>
                        </div><!-- .entry-summary -->
                        <?php } else { ?>
                        <div class="entry-content demo111">
                        <?php the_content(); ?>
                        <?php wp_link_pages( array( \'before\' => \'<div class="page-link"><span>\' . __( \'Pages:\', \'parabola\' ) . \'</span>\', \'after\' => \'</div>\' ) ); ?>
                        </div><!-- .entry-content --> 
                        <?php }   ?>

        <?php else : 
                if (is_sticky() && $parabola_excerptsticky == "Full Post")  $sticky_test=1; else $sticky_test=0;
                if ($parabola_excerpthome != "Full Post" && $sticky_test==0){ ?>


                        <div class="entry-summary">
                        <?php parabola_set_featured_thumb(); ?>
                        <?php the_excerpt(); ?>
                        </div><!-- .entry-summary -->
                        <?php } else { ?>
                        <div class="entry-content demo2222">

                        <?php 
    $content = get_the_content();
    $content = preg_replace(\'/(<)([img])(\\w+)([^>]*>)/\', "", $content);
    $content = apply_filters(\'the_content\', $content);
    $content = str_replace(\']]>\', \']]&gt;\', $content);
    echo $content;
    if (has_post_thumbnail()) { ?>    
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" target="_blank"><img src="<?php the_post_thumbnail_url(\'thumbnailnew2\'); ?>" alt="<?php the_title_attribute(); ?>"/></a>
                        <?php wp_link_pages( array( \'before\' => \'<div class="page-link"><span>\' . __( \'Pages:\', \'parabola\' ) . \'</span>\', \'after\' => \'</div>\' ) ); ?>
                        </div><!-- .entry-content --> 
                        <?php }
    cryout_post_after_content_hook(); ?>

    </article><!-- #post-<?php the_ID(); ?> -->


<?php cryout_after_article_hook(); ?>
编辑#Samuel建议后最终解决

这段代码应该工作得很好,只是缺少了一个}和endif;在代码的页脚中,例如cryout\\u post\\u after\\u content\\u hook();}endif;?>(行:71)

在塞缪尔的建议和杰克·约翰逊的惊人努力下,问题得以解决。

1 个回复
最合适的回答,由SO网友:Johansson 整理而成

the_post_thumbnail() 将返回指向您的图像的完整链接。相反,使用the_post_thumbnail_url(); 要仅获取URL,请执行以下操作:

<?php if (has_post_thumbnail()) : ?>    
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" target="_blank"><img src="<?php the_post_thumbnail_url(\'thumbnailnew2\'); ?>" alt="<?php the_title_attribute(); ?>"/></a>
这将很好地为您服务。别忘了分号。

Update 1

你有一个不必要的endif; 导致语法错误。用以下代码替换整个代码:

<?php 
    $content = get_the_content();
    $content = preg_replace(\'/(<)([img])(\\w+)([^>]*>)/\', "", $content);
    $content = apply_filters(\'the_content\', $content);
    $content = str_replace(\']]>\', \']]&gt;\', $content);
    echo $content;
    if (has_post_thumbnail()) { ?>    
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" target="_blank"><img src="<?php the_post_thumbnail_url(\'thumbnailnew2\'); ?>" alt="<?php the_title_attribute(); ?>"/></a>
                        <?php wp_link_pages( array( \'before\' => \'<div class="page-link"><span>\' . __( \'Pages:\', \'parabola\' ) . \'</span>\', \'after\' => \'</div>\' ) ); ?>
                        </div><!-- .entry-content --> 
                        <?php }
    cryout_post_after_content_hook(); ?>

相关推荐