Skip to content

Instantly share code, notes, and snippets.

@kuanhsuh
Last active May 19, 2019 04:35
Show Gist options
  • Save kuanhsuh/0cb559a3708da9039241a72408bbc7c6 to your computer and use it in GitHub Desktop.
Save kuanhsuh/0cb559a3708da9039241a72408bbc7c6 to your computer and use it in GitHub Desktop.
function.php load styles and script
// get feature image inside loop
<?php
if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('full', array('class' => 'img-fluid d-block')); // show featured image
} else { ?>
<img src="https://via.placeholder.com/1200x750" alt="" class="img-fluid d-block">
<?php } ?>
// get images from files
<?php bloginfo('template_directory');?>/assets/img/searchhome.png
// function.php
/**
* CUSTOM Functions
* Load Styles
*/
function wpb_adding_styles()
{
wp_register_style('vendor', get_template_directory_uri() . '/assets/css/vendor.css');
wp_register_style(
'studiod',
// current theme, might be the child theme
get_stylesheet_uri(),
[ 'vendor' ]
);
wp_enqueue_style('studiod');
}
add_action('wp_enqueue_scripts', 'wpb_adding_styles');
function wpb_adding_scripts()
{
wp_register_script('build', get_template_directory_uri() . '/assets/js/build.js', array('jquery'), false, true);
wp_enqueue_script('build');
}
add_action('wp_enqueue_scripts', 'wpb_adding_scripts');
// include custom jQuery
function include_custom_jquery()
{
wp_deregister_script('jquery');
wp_enqueue_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'include_custom_jquery');
function add_image_class($class){
$class .= ' img-fluid';
return $class;
}
add_filter('get_image_tag_class','add_image_class');
function new_excerpt_more( $more ) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
function custom_excerpt_length() {
return 100;
}
add_filter('excerpt_length','custom_excerpt_length');
// Create custom template
<?php
/* Template Name: Home Page */
get_header();?>
<?php
get_footer();
// Custom Query Loops
<?php
$args = array(
'cat' => 2,
'posts_per_page' => 6,
'orderby' => 'rand',
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
while ($my_query->have_posts()): $my_query->the_post(); ?>
<div class="col-sm-6 mt-3 img-container">
<a class="d-block h-100" style="overflow: hidden;" href="<?php the_permalink()?>">
<?php
if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('full', array('class' => 'img-fluid img-scale, h-100')); // show featured image
} ?>
<h5 class="text-uppercase text-center mt-2 lead">
<?php the_title(); ?>
</h5>
</a>
</div><!-- row -->
<?php endwhile;
}
wp_reset_query(); ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment