How to change Wordpress logo when scrolling (understrap-child theme) - wordpress

I'm building a website using understrap-child theme.
What I would like to achieve is my navbar logo (added through Appearance -> Customize) to change into another one when scrolling the page.
In functions.php I've already added a section in order to be able to add a second logo, which I've successfully added in the Appearance -> Customize section.
Here's the code in functions.php:
//ADDING NEW LOGO FIELD
function your_theme_customizer_setting($wp_customize) {
// add a setting
$wp_customize->add_setting('your_theme_hover_logo');
// Add a control to upload the hover logo
$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'your_theme_hover_logo', array(
'label' => 'Upload Hover Logo',
'section' => 'title_tagline', //this is the section where the custom-logo from WordPress is
'settings' => 'your_theme_hover_logo',
'priority' => 8 // show it just below the custom-logo
)));
}
add_action('customize_register', 'your_theme_customizer_setting');
get_theme_mod( 'your_theme_hover_logo' );
Now that I have two logos set, I would like to know how to replace the first logo with the second one when scrolling my page.
Here's my header.php section with the navbar:
<!-- ******************* The Navbar Area ******************* -->
<div id="wrapper-navbar">
<a class="skip-link sr-only sr-only-focusable" href="#content"><?php esc_html_e( 'Skip to content', 'understrap' ); ?></a>
<nav id="main-nav" class="navbar navbar-expand-md navbar-dark fixed-top" aria-labelledby="main-nav-label">
<h2 id="main-nav-label" class="sr-only">
<?php esc_html_e( 'Main Navigation', 'understrap' ); ?>
</h2>
<?php if ( 'container' === $container ) : ?>
<div class="container">
<?php endif; ?>
<!-- Your site title as branding in the menu -->
<?php if ( ! has_custom_logo() ) { ?>
<?php if ( is_front_page() && is_home() ) : ?>
<h1 class="navbar-brand mb-0"><a rel="home" href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" itemprop="url"><?php bloginfo( 'name' ); ?></a></h1>
<?php else : ?>
<a class="navbar-brand" rel="home" href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" itemprop="url"><?php bloginfo( 'name' ); ?></a>
<?php endif; ?>
<?php } else {
the_custom_logo();
} ?><!-- end custom logo -->
Please notice that I've already added a Query in order to change the navbar background color when scrolling...and that works!
</script>
<script>jQuery(window).scroll(function(){
jQuery('nav').toggleClass('scrolled', jQuery(this).scrollTop() > 80);
});</script>
with its CSS rules:
#media (min-width: 992px) {
.navbar.scrolled {
background-color: $secondary;
border-bottom: solid rgba(37, 36, 36, 0.055);
I really hope you can help me solve my problem!
THANK YOU SO MUCH!!

Related

How can I wrap the content using a permalink in a foreach loop

How can I wrap the content in the overlay div with a permalink that corresponds to the category. I want the image to be clickable and also pull the category name with the matching image. I want this to be dynamic in the event new categories are added.
<?php
/**
*
* Catergory Display
*
*/
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
) );
?>
<div class="container">
<div class="row">
<?php
foreach( $categories as $category ) {
$image = get_field('category_image', 'category_' . $category->term_id);
$size = 'featured-thumbnail'; // (thumbnail, medium, large, full or custom size)
$cat_link = get_category_link($category->cat_ID);
?>
<div class="category-holder col-lg-4 col-md-6 col-sm-12 ">
<div class="category-display-img">
<?php echo wp_get_attachment_image( $image, $size ); ?>
<a href="<?php echo esc_url( get_permalink($cat_link) )?>">
<div class="overlay"><?php echo '<a class="" href="'.$cat_link.'">'.$category->name.'</a>'; // category link ?></div>
</a>
</div>
</div>
<?php
// echo '<pre>';
// print_r($cat_link);
// wp_die();
} ?>
</div>
</div>
You can wrap the anchor tag around the div with the overlay class.
Delete this part
<a href="<?php echo esc_url( get_permalink($cat_link) )?>">
<div class="overlay"><?php echo '<a class="" href="'.$cat_link.'">'.$category->name.'</a>'; // category link ?></div>
</a>
And use this in its place
<a href="<?php echo $cat_link ?>">
<div class="overlay">
<?php echo $category->name ?>
</div>
</a>

Changing Logo based on pages in WordPress

I'm trying to change the header logo on my site, depending on the page the person is on. I don't know PHP, but I've found where the Logo is defined in header.php, and am trying to rewrite it to be dynamic. When I use my code, the site breaks, so obviously I'm doing something wrong.
Here is the code of my header.php file.
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head><meta charset="<?php bloginfo( 'charset' ); ?>">
<!-- Set the viewport width to device width for mobile -->
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.css" integrity="sha256-0LjJurLJoa1jcHaRwMDnX2EQ8VpgpUMFT/4i+TEtLyc=" crossorigin="anonymous" />
<?php
wp_head();
?>
<style>
.chosen-container .chosen-results {
height: 100%;
overflow-y: hidden;
max-height: inherit;
}
.chosen-container-active .chosen-single, .chosen-container-single .chosen-single {
display: none;
}
.chosen-container-single .chosen-drop {
display: contents;
}
</style>
</head>
<body <?php body_class(); ?>>
<div class="<?php $themnific_redux = get_option( 'themnific_redux' );
if (empty($themnific_redux['tmnf-uppercase'])) {} else {if($themnific_redux['tmnf-uppercase'] == '1') echo 'upper '; }
if (empty($themnific_redux['tmnf-bottombar-dis'])) {} else {if($themnific_redux['tmnf-bottombar-dis'] == '1') echo 'bottombar_dis '; }
if ( is_active_sidebar( 'tmnf-sidebar' ) ) {echo 'tmnf-sidebar-active ';} else { echo 'postbarNone ';};
if (empty($themnific_redux['tmnf-header-layout'])) {} else {echo esc_attr($themnific_redux['tmnf-header-layout']);}
?>">
<div id="header" class="tranz" itemscope itemtype="http://schema.org/WPHeader">
<div class="container_head">
<a class="screen-reader-text ribbon skip-link" href="#content_start"><?php esc_html_e('Skip to content','citygov');?></a>
<div class="clearfix"></div>
<div id="titles" class="tranz2">
<?php if(empty($themnific_redux['tmnf-main-logo']['url'])) { ?>
<h1 class="logo"><?php bloginfo('name');?></h1>
<?php }
else { ?>
<a class="logo" href="<?php echo esc_url(home_url('/')); ?>">
<img class="tranz" src="<?php echo esc_url($themnific_redux['tmnf-main-logo']['url']);?>" alt="<?php bloginfo('name'); ?>"/>
</a>
<?php } ?>
</div><!-- end #titles -->
<div class="header-right for-menu">
<input type="checkbox" id="showmenu" aria-label="<?php esc_html_e('Open Menu','citygov');?>">
<label for="showmenu" class="show-menu ribbon" tabindex="0"><i class="fas fa-bars"></i> <span><?php esc_html_e('Menu','citygov');?></span></label>
<nav id="navigation" class="rad tranz" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation" aria-label="<?php esc_html_e( 'Main Menu', 'citygov' ); ?>">
<?php get_template_part('/includes/navigation'); ?>
</nav>
</div><!-- end .header-right -->
<div class="clearfix"></div>
<div id="bottombar" class="bottomnav tranz" role="navigation" aria-label="<?php esc_html_e( 'Quick Links', 'citygov' ); ?>">
<?php if(empty($themnific_redux['tmnf-menu-label'])) {} else { ?>
<p class="menu_label"><?php echo esc_attr($themnific_redux['tmnf-menu-label']); ?></p>
<?php }?>
<div class="header-right">
<?php get_template_part('/includes/add-navigation'); ?>
<?php get_template_part('/includes/uni-social' );?>
</div>
</div><!-- end #bottombar -->
<div class="clearfix"></div>
</div><!-- end .container -->
</div><!-- end #header -->
<?php ?>
<div class="wrapper p-border">
You can do this by simply put below code in your theme's functions.php file.
after putting this code, you can now see the new settings added into back-end admin area.
Go to the wordpress admin area then go to Appearance > Customize > Site Identity.
You can now see there are two new settings.
1)Upload Logo (replaces text) => You can add second logo for other pages
2) Page IDs. => You can add comma separated page ids in this text box for displaying second logo in that pages.
function jay_customize_register( $wp_customize ) {
$wp_customize->add_setting( 'jay_logo' );
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'jay_logo', array(
'label' => __( 'Upload Logo (replaces text)', 'jay' ),
'section' => 'title_tagline',
'settings' => 'jay_logo',
) ) );
$wp_customize->add_setting('page_ids');
$wp_customize->add_control('text_setting', array(
'label' => 'Page IDs',
'section' => 'title_tagline',
'type' => 'text',
'settings' => 'page_ids',
));
}
add_action( 'customize_register', 'jay_customize_register' );
function change_logo_on_single($html) {
$pageIDs = (get_theme_mod('page_ids'))? explode(',',get_theme_mod('page_ids')) : array();
if(in_array(get_the_ID(),$pageIDs)){
$html = preg_replace('/<img(.*?)\/>/', '<img src="'.get_theme_mod('jay_logo').'" class="custom-logo" alt="" itemprop="logo" />', $html);
}
return $html;
}
add_filter('get_custom_logo','change_logo_on_single');

Wordpress how differentiate thumbnails post from even to odd?

I've done a wordpress script that displays a grid of thumbnails posts making this code:
<div class="col-sm-12 col-md-12" style="background-color: #ccc; padding-top: 30px;">
<article <?php post_class( 'article' ); ?> id="post-<?php the_ID(); ?>">
<?php
$child_pages = new WP_Query( array(
'post_type' => 'page', // set the post type to page
'posts_per_page' => 10, // number of posts (pages) to show
'post_parent' => 17, // enter the post ID of the parent page
'no_found_rows' => true, // no pagination necessary so improve efficiency of loop
'order_by' => 'title',
'order' => 'asc',
) );
if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
?>
<div class="col-md-6 thumbnail-frontpage clearfix">
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<a href = "<?php the_permalink(); ?>" > <img src = "<?php echo $image[0]; ?>" onmouseover="this.src='<?php the_field('rollover_image'); ?> ' " onmouseout="this.src='<?php echo $image[0]; ?>'" /> <?php the_title(); ?> </a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<!-- <p class=""><?php the_content( ); ?></p> -->
</article>
</div>
Now I'd like to differentiate the color of the thumbnails border separating odds from evens.
How can i do it using while conditional code?
If "border color differentiation" is your only purpose here, I suggest you use CSS, not PHP.
.thumbnail-frontpage:nth-child( even ) {
border: 1px solid red;
}
.thumbnail-frontpage:nth-child( odd ) {
border: 1px solid blue;
}
You can look it up here: http://www.w3schools.com/cssref/sel_nth-child.asp
you want like this
<?php
$child_pages = new WP_Query( array(
'post_type' => 'page', // set the post type to page
'posts_per_page' => 10, // number of posts (pages) to show
'post_parent' => 17, // enter the post ID of the parent page
'no_found_rows' => true, // no pagination necessary so improve efficiency of loop
'order_by' => 'title',
'order' => 'asc',
) );
$i=0;
if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
if($i%2==0){
$thumbnail = 'thumbnail';
}else{
$thumbnail = 'single-post-thumbnail';
}
?>
<div class="col-md-6 thumbnail-frontpage clearfix">
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), $thumbnail ); ?>
<a href = "<?php the_permalink(); ?>" > <img src = "<?php echo $image[0]; ?>" onmouseover="this.src='<?php the_field('rollover_image'); ?> ' " onmouseout="this.src='<?php echo $image[0]; ?>'" /> <?php the_title(); ?> </a>
</div>
<?php
$i++;
endwhile;
endif;
wp_reset_postdata();
?>
<!-- <p class=""><?php the_content( ); ?></p> -->
</article>
</div>

Wordpress/woocommerce css error

I've followed: https://wordpress.stackexchange.com/questions/67247/how-to-display-product-specific-to-a-category-with-woocommerce-plugin
This code is placed in CustomPageT1, and has this code:
<?php /* Template Name: CustomPageT1 */
get_header(); ?>
<div id="content" class="site-content container">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'product_cat' => 'skydd-mot-djur', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="post-18 product type-product status-publish product_cat-skydd-mot-manniskor first instock shipping-taxable purchasable product-type-simple" style="width: 25%;float:left">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>" class="woocommerce-LoopProduct-link">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" class="woocommerce-placeholder wp-post-image" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This page is created by adding /* Template Name: CustomPageT1 */ and then add it from the "add page" function. Then copy-paste from page.php into this file.
I can edit and all, the logo, menu, and some design works great. Without the Woocommerce one, in style.css I can get the css. But from the theme it doesnt fetch the css.
Which means that it looks bad, without the css.
Anyone knows why?
Because you have created a page without a link to the header file.
By creating a page with this code
/* Template Name: CustomPageT1 */
means create a page with template CustomPageT1.
The new page has no means to connect to the CSS file.
create the header-CustomPageT1.php file with the link to the CSS file andd upload to it's corresponding theme folder.
Also change this code: get_header() to get_header(CustomPageT1)

Displaying Custom Post Type Meta Data on WordPress Home Page

I really hope you can help out here.
I'm creating a theme for a client and I have created a custom post type called 'Testimonials' and now I would like to show the testimonials on the home page but I'm having trouble outputting the data for the meta information.
I have created a function in my functions.php file called 'show_testimonials' and I'm calling that in my index.php file (I haven't separated everything in to their constituant parts yet, e.g header.php, footer.php etc). This function is meant to show the posts and meta information from the 'Testimonials' custom post type but it doesn't.
I can see that I am pulling the correct meta data in to my '$meta' variable.
Below is the code for the 'show_testimonials' function.
function show_testimonials() {
$loop = new WP_Query(
array(
'post_type' => 'shv_testimonials',
'order_by' => 'ID',
)
);
if ($loop->have_posts()) {
$output = '<ul class="testimonial-list">';
while($loop->have_posts()) {
$loop->the_post();
$meta = get_post_meta(get_the_id(), '');
print_r($meta);
$output .= '
<li>
<a href="' . get_permalink() . '">
' . get_the_title() . ' | ' .
$meta['shv_testimonial_author'][0] . '
</a>
</li>
';
}
}
return $output;
}
Below is my index.php file.
<!DOCUMENT html>
<!--[if IE 6]>
<html id="ie6" <?php language_attributes(); ?>>
<![endif]-->
<!--[if IE 7]>
<html id="ie7" <?php language_attributes(); ?>>
<![endif]-->
<!--[if IE 8]>
<html id="ie8" <?php language_attributes(); ?>>
<![endif]-->
<!--[if !(IE 6) | !(IE 7) | !(IE) ]><!-->
<html <?php language_attributes(); ?>>
<!--<![endif]-->
<head>
<meta charset="<?php bloginfo('charset');?>" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>
<?php
global $page, $paged;
wp_title('«', true, 'right');
bloginfo('name');
?>
</title>
<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<?php if ( is_singular() && get_option('thread_comments')) wp_enqueue_script('comment-reply'); ?>
<?php wp_enqueue_script('jquery'); ?>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div id="page">
<header id="masthead" role="banner">
<div id="header-strip">
<nav id="secondary-navigation" role="navigation">
<?php wp_nav_menu(array(
'menu' => 'Secondary Navigation',
'container' => false
)); ?>
</nav>
<?php get_search_form(true); ?>
</div>
<div id="branding-nav">
<?php
if (is_front_page()) { ?>
<div id="logo" role="banner">
<img src="<?php echo bloginfo('template_directory') . '/images/logo.png'; ?>" alt="<?php bloginfo('name'); ?>" />
</div>
<?php } else { ?>
<div id="logo" role="banner">
<img src="<?php echo bloginfo('template_directory') . '/images/logo.png'; ?>" alt="<?php bloginfo('name'); ?>" />
</div>
<?php }
?>
<nav id="primary-navigation" role="navigation">
<?php wp_nav_menu(array(
'menu' => 'Primary Navigation',
'container' => false
)); ?>
</nav>
</div>
</header>
<div id="content">
<?php
if(is_front_page()) { ?>
<div id="feature">
<section id="introduction">
<?php
echo "<h1 class='title'>" . $options['shv_introduction_heading'] . '</h1>';
echo "<p class='short-description'>" . $options['shv_introduction_short_description'] . '</p>';
?>
<nav id="call-to-action" role="navigation">
<ul>
<li id="book-now">
Book Now
</li>
<li id="learn-more">
Learn More
</li>
</ul>
</nav>
</section>
<section id="gallery">
</section>
</div>
<?php } else { ?>
<section id="entries">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> role="main">
<header>
<aside class="post-image">
<?php
if (has_post_thumbnail()) {
the_post_thumbnail();
} else { ?>
<img src="<?php echo get_bloginfo('template_directory') . '/images/no-image.png'; ?>" title="<?php the_title(); ?>" />
<?php }?>
</aside>
<h1 id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</h2>
<time datetime="YYYY-MM-DD"></time>
</header>
<section class="post-content">
<p><?php echo get_the_excerpt(); ?></p>
Read More
</section>
<section class="meta">
<p><?php the_category(',') ?></p>
<p><?php the_tags(""); ?></p>
</section>
</article>
<?php endwhile; else: ?>
<div class="error">
<h2>Oooops!</h2>
<p>Sorry something went wrong! No posts matched your criteria. Please try again.</p>
</div>
<?php endif; ?>
<?php posts_nav_link(' ⏼ ', __('« Newer Posts'), __('Older Posts »')); ?>
<aside role="sidebar">
<h2>Some Widget in The Sidebar</h2>
</aside>
</section>
<?php } ?>
</div>
<footer role="foottext">
<div id="footer-top">
<section id="social">
<div id="twitter">
<?php show_tweets(); ?>
</div>
<div id="share">
<h2>Share</h2>
<div id="icons">
<a href="http://twitter.com/home?status=Shiverschool Theatre in Education+http://www.shiverschool.co.uk/" title="Tweet About Us">
<img src="<?php echo get_bloginfo('template_directory') . '/images/twitter-share-icon.png'; ?>" alt="Tweet About Us" />
</a>
<a href="http://www.stumbleupon.com/submit?url={http://www.shiverschool.co.uk}&title={Shiverschool Theatre in Education}" title="Recommend us on StumbleUpon">
<img src="<?php echo get_bloginfo('template_directory') . '/images/stumbleupon-share-icon.png'; ?>" alt="Recommend us on StumbleUpon" />
</a>
<a href="http://digg.com/submit?url=http://www.shiverschool.co.uk/" title="Digg Us">
<img src="<?php echo get_bloginfo('template_directory') . '/images/digg-share-icon.png'; ?>" alt="Digg Us" />
</a>
<a href="http://www.facebook.com/sharer.php?u=http://www.shiverschool.co.uk&t=Shiverschool Theatre in Education" title="Recommend us on Facebook">
<img src="<?php echo get_bloginfo('template_directory') . '/images/facebook-share-icon.png'; ?>" alt="Recommend us on Facebook" />
</a>
<a href="https://plusone.google.com/_/+1/confirm?hl=en&url=http://www.shiverschool.co.uk&title=Shiverschool Theatre in Education" title="Recommend us on Google+">
<img src="<?php echo get_bloginfo('template_directory') . '/images/google-share-icon.png'; ?>" alt="Recommend us on Google+" />
</a>
</div>
</div>
</section>
<section id="testimonials">
<div id="testimonial">
<?php show_testimonials(); ?>
</div>
<div id="mascot">
<img src="<?php echo bloginfo('template_directory') . '/images/mascot.png' ?>" alt="Shiverschool Mascot" />
</div>
</section>
</div>
<?php wp_footer(); ?>
</footer>
</div>
</body>
</html>
Here is the full 'functions.php' file.
<?php
// Add JavaScript files
function add_scripts() {
wp_enqueue_script('modernizr_script', get_template_directory_uri() . '/js/modernizr.js', __FILE__);
wp_enqueue_script('global_functions_script', get_template_directory_uri() . '/js/global-functions.js', __FILE__);
}
add_action('wp_enqueue_scripts', 'add_scripts');
// Setup and load the theme options page and related code
require(get_template_directory() . '/inc/theme-options.php');
//Get theme options
$options = get_option('shv_theme_options');
// Add support for custom backgrounds
add_custom_background();
// Add support for post thumbnails and admin bar
add_theme_support('post-thumbnails');
add_theme_support('admin-bar');
// This theme uses wp_nav_menu() in two locations
if (function_exists('register_nav_menus')) {
register_nav_menus(array(
'primary' => 'Primary Navigation',
'secondary' => 'Secondary Navigation'
));
}
/*
* Register custom post types
*/
//Testimonials Post Type
class SHV_Testimonials_Post_Type {
//Create construct function
public function __construct() {
$this->register_post_type();
$this->metaboxes();
}
//Register post type
public function register_post_type() {
$args = array(
'labels' => array(
'name' => 'Testimonials',
'singular_' => 'Testimonial',
'add_new' => 'Add New Testimonial',
'all_items' => 'All Testimonials',
'add_new_item' => 'Add New Testimonial',
'edit_item' => 'Edit Testimonial',
'new_item' => 'New Testimonial',
'view_item' => 'View Testimonial',
'search_items' => 'Search Testimonials',
'not_found' => 'No Testimonials Found',
'not_found_in_trash' => 'No Testimonials Found in Trash'
),
'query_var' => 'testimonials',
'rewrite' => array(
'slug' => 'testimonials',
),
'public' => true,
'supports' => array(
'title'
)
);
register_post_type('shv_testimonials', $args);
}
//Build metaboxes
public function metaboxes() {
//Add new meta boxes testimonial and testimonial author
add_action('add_meta_boxes', 'add_testimonial_meta_box');
add_action('add_meta_boxes', 'add_testimonial_author_meta_box');
//Run add_meta_box functions
function add_testimonial_meta_box() {
add_meta_box('shv_testimonial', 'Testimonial', 'testimonial', 'shv_testimonials');
}
function add_testimonial_author_meta_box() {
add_meta_box('shv_testimonial_author', 'Testimonial Author', 'testimonial_author', 'shv_testimonials');
}
//Create form elements and pull through any data associated with each meta box
function testimonial($post) {
$testimonial = get_post_meta($post->ID, 'shv_testimonial', true); ?>
<p>
<label for="shv_testimonial">Please enter your testimonial</label>
<textarea class="large-text" name="shv_testimonial" id="shv_testimonial" cols="50" rows="5"><?php echo esc_attr($testimonial); ?></textarea>
</p>
<?php }
function testimonial_author($post) {
$author = get_post_meta($post->ID, 'shv_testimonial_author', true); ?>
<p>
<label for="shv_testimonial_author">Please enter a testimonial author name and school, e.g. John Doe, St Ambrose Barlow</label>
<input type="text" class="widefat" name="shv_testimonial_author" id="shv_testimonial_author" value="<?php echo esc_attr($author); ?>" />
</p>
<?php }
//Save meta box input
add_action('save_post', 'save_meta_data');
function save_meta_data($id) {
if (isset($_POST['shv_testimonial'])) {
update_post_meta(
$id,
'shv_testimonial',
strip_tags($_POST['shv_testimonial'])
);
}
if (isset($_POST['shv_testimonial_author'])) {
update_post_meta(
$id,
'shv_testimonial_author',
strip_tags($_POST['shv_testimonial_author'])
);
}
}
}
}
add_action('init', 'register_testimonials');
function register_testimonials() {
new SHV_Testimonials_Post_Type();
}
function show_tweets() { ?>
<img src="<?php echo get_bloginfo('template_directory') . '/images/twitter-bird-icon.png'; ?>" alt="Tweets" />
<?php
//Get theme options
$options = get_option('shv_theme_options');
include_once(ABSPATH . WPINC . '/feed.php');
$username = $options['shv_twitter_username'];
$rss = fetch_feed('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=' . $username);
if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity(1);
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items(0, $maxitems);
endif;
?>
<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo $item->get_permalink(); ?>'>
<?php echo $item->get_title(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php }
function show_testimonials() {
$loop = new WP_Query(
array(
'post_type' => 'shv_testimonials',
'order_by' => 'ID',
)
);
if ($loop->have_posts()) {
$output = '<ul class="testimonial-list">';
while($loop->have_posts()) {
$loop->the_post();
$meta = get_post_meta(get_the_id(), '');
print_r($meta);
$output .= '
<li>
<a href="' . get_permalink() . '">
' . get_the_title() . ' | ' .
$meta['shv_testimonial_author'][0] . '
</a>
</li>
';
}
}
return $output;
}
?>
Here's a link to the development site where you can see that I'm outputting the correct meta data using 'print_r($meta);'. http://dev.garethdaine.com/shiverschool
I hope someone can point me in the right direction. Cheers in advance.
The output of show_testimonials is just not printed anywhere, try echo show_testimonials() in your template or echo $output; instead of return $output; in your show_testimonials function.

Resources