WordPress 自定义文章模板和相关文章

图片[1]-Wordpress 自定义文章模板和相关文章-LyleSeo

给WordPress自定义文章页模板和添加相关文章代码,对于网站的体验和SEO来说都有好处,本文就分享一下WordPress相关文章的代码实现方式,新手的话建议使用主题自带功能或者插件实现。

自定义文章模板和增加相关文章

1、我们以Disto主题为例,在Wordpress主题下新建 newpost.php(文件名可以自定义) 的文件,代码如下:

<?php
/*
  Template Name: Page Fullwidth
 */
?>
<?php get_header();
$jelly_header_id = get_the_post_thumbnail_url(get_the_ID(), 'disto_large_slider_image');
$jl_rm_title= get_post_custom_values('jl_rm_title', get_the_ID());
if($jl_rm_title){}else{
?>

<?php }?>

   
    <div class="container">
        <div class="row main_content">
            <!-- begin content -->
                    <div class="content_page_padding">
                        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                        <?php the_content(); ?>
                        <?php endwhile; // end of the loop.    ?>
                        <?php endif; ?>
                    </div>
                    <?php if ( function_exists( "get_related_posts_thumbnails" ) ) { get_related_posts_thumbnails(); } ?>
                    <div class="brack_space"></div>
                    <?php wp_link_pages( array( 'before' => '<ul class="page-links">', 'after' => '</ul>', 'link_before' => '<li class="page-link">', 'link_after' => '</li>' ) ); ?>
            
        </div>
    </div>  
  
 

<?php get_footer(); ?>

2、在主题下找到 functions.php 在内面加入以下代码

/*Add post*/

function register_custom_post_template() {
    add_filter('theme_post_templates', function($post_templates) {
        $post_templates['newpost.php'] = 'New-Post'; 
        return $post_templates;
    });
}
add_action('init', 'register_custom_post_template');


/*end add post*/

注意: $post_templates[‘newpost.php‘] = ‘New-Post’; 与新建的php一致。

3、如何调用新模板

在发布文章时,Post Attributes 选择新的模板即可。

图片[2]-Wordpress 自定义文章模板和相关文章-LyleSeo

4、如果想自定义模板更多样化,可以安装插件:Advanced Custom Fields ,自定义字段。

Advanced Custom Fields

5、增加文章模板内的相关文章。

在Wordpress 插件内搜索并安装:Related Post Thumbnails ,

Related Post Thumbnails

根据插件提示,将代码放至自定义模板内即可,相关配置,可以自定查看插件

<?php if ( function_exists( "get_related_posts_thumbnails" ) ) { get_related_posts_thumbnails(); } ?>
图片[5]-Wordpress 自定义文章模板和相关文章-LyleSeo

以上为自定义文章模板的所有教程,如果不懂,可以自定了解或搜索相关资料。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享