简体   繁体   English

如何为WordPress主题中的帖子提供CSS

[英]How to give css for posts in a wordpress theme

I new to wordpress development , I have developed a custom wordpress theme but i am not able to give css to the posts.Posts are rendering in very normal way but i want to give some css , please suggest me how i can do this . 我是WordPress开发的新手,我开发了一个自定义的WordPress主题,但是我无法将CSS提交给posts.Posts以非常普通的方式呈现,但是我想给一些CSSs,请提出我如何做到这一点。

帖子以基本方式出现

   <?php get_header(); ?>

  <div id="primary" class="content-area">
   <main id="main" class="site-main" role="main">
    <?php
    // Start the loop.
    while ( have_posts() ) : the_post();

        // Include the page content template.
        get_template_part( 'template-parts/content', 'page' );

        // If comments are open or we have at least one comment, load up the comment template.
        if ( comments_open() || get_comments_number() ) {
            comments_template();
        }

        // End of the loop.
    endwhile;
    ?>

</main><!-- .site-main -->  
<?php get_footer(); ?>

I have added images how posts are appearing and code of page.php of my wordpress theme. 我添加了图片,这些内容是如何显示的以及wordpress主题的page.php代码。 Seeking some help 寻求帮助

Add a css directory inside the your theme directory let create a file name "poststyle.css" inside that css directory and then you can link the css file in header.php 在主题目录中添加一个css目录,然后在该css目录中创建一个文件名“ poststyle.css”,然后您可以在header.php中链接该css文件

    <link rel="stylesheet" href="<?php echo esc_url( get_template_directory_uri() ); ?>/css/poststyle.css" media="screen" title="post-style" charset="utf-8">

OR  
    <?php
     wp_enqueue_style( 'post-styles' , get_template_directory_uri() . 'css/poststyle.css' );
    ?>

but before that u need to find out the class or id or div name so that you can give or define the styles to that corresponding div's or classes inside "poststyle.css" 但在此之前,您需要找出类或id或div名称,以便您可以在“ poststyle.css”中为相应的div或类赋予或定义样式

Now just take this example 现在以这个例子为例

<?php
/**
 * The template for displaying pages
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages and that
 * other "pages" on your WordPress site will use a different template.
 *
 * @package WordPress
 * @subpackage Twenty_Fifteen
 * @since Twenty Fifteen 1.0
 */

get_header(); ?>

<div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">
        <?php  while ( have_posts() ) : the_post(); ?>
            <div class="title"><?php the_title( '<h1>', '</h1>' ); ?></div>
                 <div class="pic">      <?php echo get_the_post_thumbnail( get_the_ID() , 'thumbnail' , array('class' => 'img-thumbnail') ); ?></div>
              <div class="content"> <?php the_content() ; ?></div>
        <?php endwhile; // end of the loop. ?>
    </div><!-- #content -->
</div><!-- #primary -->

<?php get_footer(); ?>

Now you can give styles to class="site-content" or class="content-area" , class="title", class="content" 现在,您可以为class =“ site-content”或class =“ content-area”,class =“ title”,class =“ content”指定样式


But Incase of your post show template what you have doing is that you are importing another template which is inside template-parts directory content.php file where all the post content showing codes are there 但是如果您的帖子显示模板是您正在做的,那就是您要导入另一个模板,该模板位于template-parts目录content.php文件中,其中所有显示代码的帖子内容都在其中

You have to follow the the wordpress coding standard to create theme and plugins. 您必须遵循wordpress编码标准才能创建主题和插件。 To add css and js just create two folder inside your theme named 'js' and 'css'. 要添加css和js,只需在主题“ js”和“ css”内创建两个文件夹。 Then add this code inside the functions.php 然后将此代码添加到functions.php中

if(!function_exists('theme_style')):
    function theme_style(){
        wp_enqueue_script('main-js', get_template_directory_uri().'/js/main.js',array(),false,true);
        wp_enqueue_style( 'main-css', get_template_directory_uri(). '/css/main.css', array(),false,'all' );       
    }
    add_action('wp_enqueue_scripts','theme_style');
endif;

You can change the file name main.js and main.css 您可以更改文件名main.js和main.css

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM