wordpress二十一如何使文章可点击?



嗨!

我使用的是wordpress的Twenty 21 Theme我修改了一下。事实是,我希望文章是完全可点击的,而不仅仅是标题和"查看其余部分"。按钮。

我知道我必须编辑content-single.php,因为我将我的主页定义为最新的文章:

<a href="<?php the_permalink() ?>">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header alignwide">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
<?php twenty_twenty_one_post_thumbnail(); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php
the_content();
wp_link_pages(
array(
'before'   => '<nav class="page-links" aria-label="' . esc_attr__( 'Page', 'twentytwentyone' ) . '">',
'after'    => '</nav>',
/* translators: %: Page number. */
'pagelink' => esc_html__( 'Page %', 'twentytwentyone' ),
)
);
?>
</div><!-- .entry-content -->
<footer class="entry-footer default-max-width">
<?php twenty_twenty_one_entry_meta_footer(); ?>
</footer><!-- .entry-footer -->
<?php if ( ! is_singular( 'attachment' ) ) : ?>
<?php get_template_part( 'template-parts/post/author-bio' ); ?>
<?php endif; ?>
</article>
</a>

但是<a href="<?php the_permalink() ?>">不工作…

我该怎么做呢?我已经不习惯PHP了^^'

提前感谢!

编辑:

所以这个函数是应用到帖子本身,但我想把它应用到hompeage帖子列表中的帖子。

所以我这样做了:

function clickable_article( $content ) {
if ( is_front_page() ) {
?>        
<script>
jQuery( document ).ready( function( $ ) {
// add a cursor 
$( '.post' ).css("cursor", "pointer");
// on content click go to article url
$( '.post' ).on( 'click', function( e ) {
var url = window.location.href; 
window.location = url;
} );
} );
</script>
<?php
}
return $content;
}
add_filter( 'the_content', 'clickable_article' );

现在文章是可点击的,但它只是完全崩溃了我的网页导航…似乎是window.location = url;

在child-theme functions.php中尝试这样做:

function clickable_article( $content ) {
if ( is_single() ) {

?>

<script>
jQuery( document ).ready( function( $ ) {
// add a cursor 
$( '.entry-content' ).css("cursor", "pointer");
// on content click go to article url
$( '.entry-content' ).on( 'click', function( e ) {
var url = window.location.href; 
window.location = url;
} );
} );
</script>

<?php

}

return $content;

}
add_filter( 'the_content', 'clickable_article' ); 

试试这个:

function clickable_article( $content ) {
if ( is_front_page() ) {
?>
<script>
jQuery( document ).ready( function( $ ) {
// add a cursor 
$("article").each(function() {
var copyUrl = $(this).find(".entry-title").attr("href");

$(this).click(function() {
window.location = copyUrl;
});
});
});
</script>
<?php
}
return $content;
}
add_filter( 'the_content', 'clickable_article' );

终于成功了!

要想让所有的文章都能在你的Wordpress主页上点击你需要:

  1. 设置主页显示最近的文章
  2. 确保你有"阅读更多"链接
  3. 将此代码粘贴到function.php:
function clickable_article( $content ) {
if ( is_front_page() ) {
?>        
<script>
jQuery( document ).ready( function( $ ) {
$( '.post' ).unbind().click(function() {
var url = $(this).find("a").attr("href"); 
console.log(url);
window.location = url;
} );
})
</script>
<?php
}
return $content;
}
add_filter( 'the_content', 'clickable_article' );

你需要.unbind()事件,因为帖子有几个链接(图片,标题,阅读更多…)所以窗口。位置不会被多个url淹没而导致浏览器崩溃。

最新更新