I'm working on a wordpress theme, and i want to be able to apply a css change to all pages instead of just one page, for example, the frontpage only is applying changes on the front page, i want to be able to call this in the functions.php, however im not so sure how to call it.
In a nutshell, how to call css in a php function, in the functions.php file
Any Suggestions ?
template-frontpage.php
<?php
/**
* Template Name: Front Page
*
* #package Eli
*/
get_header();
$hero = cs_get_option('hero_image');
?>
<style type="text/css">
.hero:before{
background-color:<?php echo cs_get_option('hero_color_picker');?>;
}
.hero:after{
background-color:<?php echo cs_get_option('hero_color_picker');?>;
}
a{
color: <?php echo cs_get_option('link_color');?>;
}
footer a{
color:<?php echo cs_get_option('link_color');?>; ;
}
</style>
<div class="row">
<div class="hero" style="background-image:url(<?php echo $hero; ?>); width:100%; min-height:350px; background-size: cover;">
<div class="container">
<div class="col-md-12">
<header class="hero-text">
<?php if (get_field('hero_title') ):?>
<h1 style="color:#fff;"><?php the_field('hero_title'); ?></h1>
<?php endif;?>
<?php if (get_field('hero_span') ):?>
<span><?php the_field('hero_span'); ?></span>
<?php endif;?>
<?php if (get_field('hero_span_2') ):?>
<span id="move"><?php the_field('hero_span_2'); ?></span>
<?php endif;?>
</header>
</div>
</div>
</div>
</div>
<section class="section-home">
<div class="row">
<div class="container">
<div class="line"></div>
<?php if (get_field('content_block_left') ):?>
<div id="cbl" class="col-md-4 col-xs-12">
<?php the_field('content_block_left_icon'); ?>
<?php the_field('content_block_left'); ?>
</div>
<?php endif;?>
<?php if (get_field('content_block_left2') ):?>
<div id="cbl" class="col-md-4 col-xs-12 ">
<?php the_field('content_block_left_2_icon'); ?>
<?php the_field('content_block_left2'); ?>
</div>
<?php endif;?>
<?php if (get_field('content_block_left3') ):?>
<div id="cbl" class="col-md-4 col-xs-12">
<?php the_field('content_block_left_3_icon'); ?>
<?php the_field('content_block_left3'); ?>
</div>
<?php endif;?>
</div>
</div>
</section>
<div class="section-about">
<div class="row">
<h1>Beat Your Rivals</h1>
<div class="line"></div>
<div class="container">
<?php if (get_field('image_left') ):?>
<div id="cbl2" class="col-md-6 offset-md-3 col-xs-12">
<img src="<?php echo the_field('image_left'); ?>" width:"400px" height:"300px">
</div>
<?php endif;?>
<?php if (get_field('caption_text') ):?>
<div id="cbl2" class="col-md-6 offset-md-3 col-xs-12">
<?php the_field('caption_text'); ?>
</div>
<?php endif;?>
</div>
</div>
</div>
<?php
$image2 = get_field('test_image');
?>
<div class="section-test" style="background-image:url(<?php echo $image2['url'];?>); width:100%; min-height:300px; background-size: cover;">
<div class="row">
<div class="container">
<?php if (get_field('test_text') ):?>
<div id="cbl3" class="col-md-12 col-xs-12">
<?php the_field('test_text'); ?>
</div>
<?php endif;?>
</div>
</div>
</div>
<div class="about-us">
<div class="row">
<div class="container">
<?php if (get_field('about_us') ):?>
<div class="col-md-12 col-xs-12">
<?php the_field('about_us'); ?>
</div>
<?php endif;?>
</div>
</div>
</div>
<?php
$image3 = get_field('cons_image');
?>
<div class="consult">
<div class="row">
<div class="my-block-left" style="background-image:url(<?php echo $image3['url'];?>); background-size: cover;" >
<div class="container">
<?php if (get_field('consult_us') ):?>
<div class="col-md-12 col-xs-12">
<?php the_field('consult_us'); ?>
</div>
<?php endif;?>
</div>
</div>
</div>
</div>
<?php if (get_field('contact_us') ):?>
<div class="contact-us">
<div class="row">
<div class="container">
<h1 class="contact-h1">Contact Us</h1>
<div class="line"></div>
<div class="col-md-12 col-xs-12">
<?php the_field('contact_us'); ?>
</div>
</div>
</div>
</div>
<?php endif;?>
<?php get_footer(); ?>
Not sure if you want to know how to enqueue css or how to limit css to specific pages so
To include styles use in your function.php
function site_styles() {
wp_register_style( 'global-css', get_template_directory_uri() . '/assets/css/global.css');
wp_register_style( 'frontpage-only-css', get_template_directory_uri() . '/assets/css/frontpage.css');
}
add_action('init', 'site_styles');
than you can decide where to use or not those styles
// is_front_page()
// is_page(XX) - is page id XX
// is_single() && get_post_type()="custom_post_type" - single for custom posts
// more about conditional tags: https://codex.wordpress.org/Conditional_Tags
function enqueue_styles() {
if ( is_front_page() ) {
wp_enqueue_style( 'frontpage-only-css' );
} else {
wp_enqueue_style( 'global-css' );
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_styles' );
Related
So, i was wondering if there's a way to target a specific value in a <?php the_field('title'); ?> loop.
I'm iterating over a result set of people on a site. If title equals CEO it should display in another row.
<?php
/*
Template Name: Personal
*/
?>
<?php get_header(); ?>
<div id="main" class="fullpage" style="background-image: url(<?php echo get_template_directory_uri(); ?>/img/fullpagebg.png);">
<section class="banner innerBanner">
<div class="container">
<div class="bannerContent">
<h1>Vår <strong>personal</strong></h1>
</div>
</div>
</section>
<section class="padtop0">
<div class="container-fluid">
<div class="productsMain">
<div class="row justify-content-center">
<?php
$loop = new WP_Query( array( 'post_type' => 'Persons') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-xl-3 col-lg-4 col-md-6 wow fadeInUp" data-wow-delay=".2s">
<div class="personalbox" style="background-color: #f3f3f3; background-image: url(<?php echo get_field('bild'); ?>); background-size: <?php echo $background_size; ?>;">
<div class="personalbox-description">
<h4 style="color:#000000 !important;"><strong><?php the_field('Namn'); ?></strong></h4>
<h6 style="color:#000000 !important;;"><?php the_field('title'); ?></h6>
<br>
<h6 style="color:#000000 !important;"><strong>Telefon:</strong> <?php the_field('telefon'); ?></h6>
<h6 style="color:#000000 !important;"><strong>Mailt:</strong> <?php the_field('e-post'); ?></h6>
</div>
</div>
</a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
</div>
</div>
</section>
<?php get_footer(); ?>
So I'm converting bootstrap site to Wordpress and post cards don't want to stack horizontally... I know there should be foreach loop which will loop through posts but I don't know how to set it up...
<?php while(have_posts()) {
the_post();
?>
<section id="blog" class="py-3">
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="card-columns">
<div class="card">
<img src="<?php the_post_thumbnail_url('medium')?>" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
<?php the_title(); ?>
</h4>
<small class="text-muted">Written by <?php the_author_posts_link(); ?></small>
<hr>
<p class="card-text"><?php the_excerpt(); ?></p>
</div>
</div>
</div>
</div>
</div>
</section>
<?php } ?>
while is also a loop. Inside the loop, you can just put the repeated code. As I've understood your code than you need to something like this:
<section id="blog" class="py-3">
<div class="container">
<div class="row">
<?php while( have_posts() ) {
the_post();
?>
<div class="col-md-4">
<div class="card-columns">
<div class="card">
<img src="<?php the_post_thumbnail_url('medium')?>" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
<?php the_title(); ?>
</h4>
<small class="text-muted">Written by <?php the_author_posts_link(); ?></small>
<hr>
<p class="card-text"><?php the_excerpt(); ?></p>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</section>
Also, better to move code for one block to another file for example partials/post.php:
<div class="col-md-4">
<div class="card-columns">
<div class="card">
<img src="<?php the_post_thumbnail_url('medium')?>" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
<?php the_title(); ?>
</h4>
<small class="text-muted">Written by <?php the_author_posts_link(); ?></small>
<hr>
<p class="card-text"><?php the_excerpt(); ?></p>
</div>
</div>
</div>
</div>
And update your current file:
<section id="blog" class="py-3">
<div class="container">
<div class="row">
<?php while( have_posts() ) {
the_post();
get_template_part( 'partials/post' );
} ?>
</div>
</div>
</section>
I am new to wordpress development, I am learning how to make a theme from scratch. I don't want to use any plugin to achieve this.
How to create first post/ fifth post/ ninth post full width, rest in three columns and so on.
I tried this but first post is not repeating
<?php get_header() ?>
<div class="container-fluid">
<div class="row">
<?php $i = 0; while ( have_posts() ) : the_post(); ?>
<?php if ($i++ == 0) : ?>
<div class="col-sm-12 blog">
<div class="row">
<div class="col-sm-8 p-0">
<?php the_post_thumbnail()?>
</div>
<div class="col-sm-4 align-self-center">
<div><h3><?php the_title()?></h3></div>
<div><?php the_excerpt()?></div>
</div>
</div>
</div>
<?php else: ?>
<div class="col-sm-4 blog py-3">
<?php the_post_thumbnail()?>
<div><h3><?php the_title()?></h3></div>
<div><?php the_excerpt()?></div>
</div>
<?php endif; ?>
<?php endwhile ?>
</div>
</div>
<?php get_footer() ?>
find my screen here
Your text isn't very clear but i'm assuming you want to have 1 big post and 3 tiny posts pattern.
To achieve this you will need to tell your code that the first one or when the post number is divided by 4 and the remainder is 0, the post should be big.
Code:
<?php get_header() ?>
<div class="container-fluid">
<div class="row">
<?php $i = 0; while ( have_posts() ) : the_post(); ?>
<?php if ($i == 0 || $i % 4 == 0) : ?>
<div class="col-sm-12 blog">
<div class="row">
<div class="col-sm-8 p-0">
<?php the_post_thumbnail()?>
</div>
<div class="col-sm-4 align-self-center">
<div><h3><?php the_title()?></h3></div>
<div><?php the_excerpt()?></div>
</div>
</div>
</div>
<?php else: ?>
<div class="col-sm-4 blog py-3">
<?php the_post_thumbnail()?>
<div><h3><?php the_title()?></h3></div>
<div><?php the_excerpt()?></div>
</div>
<?php endif; ?>
<?php $i++;endwhile ?>
</div>
</div>
<?php get_footer() ?>
Hope this help you.
<?php get_header() ?>
<div class="container-fluid">
<div class="row">
<?php $i = 0; while ( have_posts() ) : the_post(); ?>
<?php if ($i % 4 == 0) : ?>
<div class="col-sm-12 blog">
<div class="row">
<div class="col-sm-8 p-0">
<?php the_post_thumbnail()?>
</div>
<div class="col-sm-4 align-self-center">
<div><h3><?php the_title()?></h3></div>
<div><?php the_excerpt()?></div>
</div>
</div>
</div>
<?php else: ?>
<div class="col-sm-4 blog py-3">
<?php the_post_thumbnail()?>
<div><h3><?php the_title()?></h3></div>
<div><?php the_excerpt()?></div>
</div>
<?php endif; ?>
<?php
$i++;
endwhile ?>
</div>
</div>
<?php get_footer() ?>
I am in the process of creating a theme and I can't get the author to be displayed on the single.php page. It's displayed lower on the site but doesn't seem to show in the first call under the headline.
<header class="intro-header" style="background-image: url('http://i.imgur.com/ZyZMhQv.jpg')">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="site-heading">
<h1><?php the_title(); ?></h1>
<hr class="small">
<span class="subheading">Posted by <?php the_author(); ?> on <?php the_time('F jS, Y'); ?></span>
<div id="avatar" class="row text-center"><?php echo get_avatar( $post->post_author, 92 ); ?> </div>
</div>
</div>
</div>
</div>
</header>
The second time it's called within the loop it works fine.
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default panel-body">
<?php while(have_posts()) : the_post(); ?>
<p> Posted by <?php the_author(); ?> on <?php the_time('F jS, Y'); ?> </p>
<p> <?php the_content(''); ?> </p>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
Why doesn't it show up the first time it's called?
as per the codex - it must be within the loop.
Picked up the code from other theme i've been training on, where it was working perfectly fine. However, here, i can't get it to display content. No trace of posts on index page at all. And on other pages, it only shows page titles.
website here:
soloveich.com
index page content code
<?php
get_header(); ?>
<div class="body">
<div class="row-fluid">
<div class="span2"></div>
<div class="span8" id="conty">
<div id="title"><h4>Your Company Name</h4></div>
<div id="content">
<?php while ( have_posts() ) : the_post() ?>
<?php get_template_part( 'entry' ); ?>
<?php endwhile; ?>
</div>
</div>
<div class="span2"></div>
</div>
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="<?php bloginfo('template_url');?>/js/bootstrap.min.js"></script>
</body>
</html>
other pages code
<?php
get_header(); ?>
<div class="body">
<div class="row-fluid">
<div class="span2"></div>
<div class="span8" id="conty">
<div id="title"><h4><?php the_title(); ?></h4></div>
<div id="content">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<?php the_content(); ?>
</div>
</div>
<div class="span2"></div>
</div>
</div>
</div>
</body>
</html>