I have to add some simple HTML to the homepage of a client's WordPress website. At first, I added the code to the header.php and everything looked and worked how it should; but, then I realised that it was showing on every page when I just need it on the home page.
So, I tried adding the same HTML after the get_header in the homepage.php and then in the index.php but nothing shows at all when I add the code to these pages.
No matter where I put the HTML in homepage.php or index.php it will not show. I'm new to WordPress as you have no doubt gathered.
Do I need to make a template or use some code to introduce the HTML in either of these two pages? Why does it show in the header.php?
I tried using with no joy:
if ( is_home() ) {
The home page code is here if it helps:
<?php
/**
Template Name: Homepage
*
*/
get_header(); ?>
<?php $general_options = get_option( 'meanthemes_theme_general_options' ); ?>
<?php $content_options = get_option ( 'meanthemes_theme_content_options' ); ?>
<?php if( isset( $general_options[ 'hide_slider' ] ) ) {
// do nothing
} else {
?>
<section class="hero flexslider">
<article class="wrapper">
<div class="flex-container"></div>
<ul class="slides">
<?php
$bannerPortfolio = $general_options[ 'swap_slider' ];
if(!$bannerPortfolio) {
query_posts( array ( 'posts_per_page' => 3 ) );
while ( have_posts() ) : the_post();
?>
<li>
<h2>
<?php the_title(); ?>
</h2>
<div>
<?php echo excerpt_clip(get_the_excerpt(), '28'); ?> <?php _e('[...]','meanthemes'); ?>
</div>
<time class="time" datetime="<?php the_time('Y-m-d', '', ''); ?>"><?php the_time('jS M Y') ?></time>
</li>
<?php endwhile; wp_reset_query(); ?>
<?php
} else {
?>
<?php
query_posts( array( 'post_type' => 'portfolio', 'order' => 'desc', 'posts_per_page' => '3' ) );
?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<li>
<hgroup>
<h2>
<?php the_title(); ?>
</h2>
<div>
<?php echo excerpt_clip(get_the_excerpt(), '48'); ?> <?php _e('[...]','meanthemes'); ?></div>
</hgroup>
</li>
<?php endwhile; ?>
<?php } ?>
</ul>
</article><!-- / article.wrapper -->
</section>
<script>
jQuery(window).load(function() {
jQuery('.flexslider').flexslider({
animation: "fade", // animation type fade or slide
slideshowSpeed: <?php echo sanitize_text_field( $general_options ['slider_timer'] ); ?>, // sections between transitions
directionNav: true, // show previous and back
controlNav: false,
controlsContainer: '.flex-container'
});
});
</script>
<?php } ?>
<?php if( $general_options[ 'show_portfolio' ] ) { ?>
<section class="wrapper portfolio">
<article>
<hgroup>
<h2><?php echo balanceTags( $content_options['we_do'] ); ?></h2>
</hgroup>
<div><?php echo balanceTags( $content_options['we_do_summary'] ); ?></div>
</article>
<?php
query_posts( array( 'post_type' => 'portfolio', 'posts_per_page' => '4', 'orderby' => 'date', 'order' => 'desc' ) );
?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<aside>
<?php the_post_thumbnail('portfolio-thumb'); ?>
<hgroup>
<h3><?php the_title(); ?></h3>
</hgroup>
<div><?php echo excerpt_clip(get_the_excerpt(), '14'); ?><?php _e('[...]','meanthemes'); ?></div>
</aside>
<?php endwhile; wp_reset_query(); ?>
</section>
<?php } ?>
<script>
jQuery(document).ready(function() {
jQuery('body').addClass("home");
});
</script>
<?php get_footer(); ?>
Howlin's example should work. Another solution is to create a front-page.php in the theme-directory, copy the content from the page.php to it, and add your specific HTML-Code for the homepage (frontpage).
Visit http://codex.wordpress.org/Template_Hierarchy to understand which template is invoked in which case.
Visit http://codex.wordpress.org/Conditional_Tags for Conditional Tags, which you can use e.g. in theheader.php
You might need to try is_front_page instead.
if(is_front_page()){
is_home works if you have blog posts displaying on the homepage and is_front_page if the homepage is set as a static page.
Related
I'm creating a home page that will pull in different posts to display them in a custom format. I can get most of the information I want but I haven't been able to to get the featured image for any of them. I set a featured image for all three posts and I'm using my own theme that's a child of the twentysixteen theme. I get has_post_thumbnail returning false on all of them even though I've uploaded a featured image for each post. I only have this on local right now but here's the code I'm using to get the posts:
<?php
global $post;
$myposts = get_posts('numberposts=3&category=1');
foreach($myposts as $post) :
setup_postdata( $post );?>
<div class="article-box">
<div class="article-box-image">
<?php if (has_post_thumbnail($post->ID)) { ?>
has one
<?php } else { ?>
not working
<?php } ?>
<img src="<?php the_post_thumbnail_url(); ?>">
</div>
<div class="article-box-title">
<?php the_title(); ?>
</div>
<div class="article-box-excerpt">
<?php the_excerpt(); ?>
</div>
<div class="article-box-edit">
<?php edit_post_link('Edit'); ?>
</div>
</div>
<?php endforeach; ?>
Use a wp query:
<?php
$args = array(
'cat' => '1',
'posts_per_page' => '3',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="article-box">
<div class="article-box-image">
<?php //check for thumbnail
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
//Give up and start new life picking apples
} ?>
</div>
<div class="article-box-title">
<?php the_title(); ?>
</div>
<div class="article-box-excerpt">
<?php the_excerpt(); ?>
</div>
<div class="article-box-edit">
<?php edit_post_link('Edit'); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
And then double check that thumbnails are supported in your theme's functions.php
add_theme_support( 'post-thumbnails' );
I finally realized that the "preview" post parameters were the problem. I was viewing the page through preview page. I don't know why images won't work in preview mode but apparently it won't.
Hi all I am a new of wordpress I have problame with pagination, I used plugin Wp-pagnavi ,When I click link to page 2 it same as page 1,How to to fix it.
You can see it at
http://westecmedia.com/?page_id=758
And this my code in page-event.php
<?php
/*
* Template Name: Page - Events Page
*/
?>
<?php get_header(); ?>
<div id="content-events">
<div id="head-event"><h3>EVENTS</h3></div>
<div id="main-event">
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 300);
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php endwhile; endif; ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query(); ?>
</div>
</div>
<?php get_footer(); ?>
Help me please :(
Include paged, documentation: https://codex.wordpress.org/Pagination
<?php
$args = array(
'cat' => '5',
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post();
/* Do whatever you want to do for every page... */
endwhile;
wp_pagenavi();
wp_reset_query(); // Restore global post data
?>
Also don't use query_posts to fetch data in WordPress, consider using https://codex.wordpress.org/Class_Reference/WP_Query.
Please ask WordPress related question here: http://wordpress.stackexchange.com
I hope this helps.
Below is my wordpress loop and i have tired to take-off the Page title but its not happning.
Please suggest me the correct loop.
<div class="single-full-width-container single-page-header-container">
<header class="container">
<?php the_title( '<h1 class="single-page-heading">', '</h1>' ); ?>
<ul class="single-page-breadcrumbs">
<?php
if(function_exists('bcn_display') && !is_front_page())
{
bcn_display();
}
?>
</ul>
</header>
</div>
<div class="container">
<div class="row">
<main role="main" class="shortcode-container span12">
<div class="row">
<div class="span12">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
</div>
<!-- .entry-content -->
<?php endwhile; ?>
</div>
</div>
</main>
</div>
I have tried with other loop without the "title" tag but its still coming at top.
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
Remove the line
<?php the_title( '<h1 class="single-page-heading">', '</h1>' ); ?>
I think you're not editing the proper file.
There is a line in your file:
<?php get_template_part( 'content', 'page' ); ?>
This part is intended to import the content-page.php file as far as I know. Maybe this file puts the title on the top of the article. Try to edit that file if you find anything like that. If there is no content-page.php it falls back to import content.php.
(For more details check this: http://codex.wordpress.org/Function_Reference/get_template_part )
If I'm right the part of the file you cited above makes no effect after the second appearance of this line:
<?php while ( have_posts() ) : the_post(); ?>
Because the first appearance looped through the whole collection of the posts so the have_posts() function returns false after that. So the whole section until <?php endwhile; ?> (lines 22-33) is always skipped.
I hope I was right and could help you with your task :)
I am trying to add a Slider on my home page using a template from themeforest but I don't get any support there.
I am adding the following code to my header:
<?php get_template_part ( 'includes/featured/featured-call'); ?>
and this code calls featured-call.php and from there another files is called, flexislider.php that looks like this:
<section>
<div class="spaced-wrap clearfix">
<div class="flexslider-container clearfix">
<div class="flexslider-loader">
<div class="flexslider">
<ul class="slides">
<?php
$captioncodes="";
$count=0;
query_posts( array( 'post_type' => 'mtheme_featured', 'showposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC') );
?>
<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>
<?php
$image_id = get_post_thumbnail_id(($post->ID), 'full');
$image_url = wp_get_attachment_image_src($image_id,'full');
$image_url = $image_url[0];
$custom = get_post_custom(get_the_ID());
$featured_description="";
$featured_link="";
if ( isset($custom["featured_bigtitle"][0]) ) $featured_bigtitle=$custom["featured_bigtitle"][0];
if ( isset($custom["featured_description"][0]) ) { $featured_description=$custom["featured_description"][0]; }
if ( isset($custom["featured_link"][0]) && $custom["featured_link"][0]<>"" ) {
$featured_link=$custom["featured_link"][0];
} else {
$featured_link = get_post_permalink();
}
//$textblock=$featured_description;
$title=get_the_title();
$text=$featured_description;
$permalink = $featured_link;
$count++;
?>
<li>
<a href="<?php echo $permalink; ?>">
<img src="<?php echo $image_url; ?>" alt="<?php the_title(); ?>" />
</a>
<?php
$titlecode ='<div class="flex-title">' .$title . '</div>';
$captioncodes ='<div class="flex-caption">' . $text . '</div>';
$bigtitle='<div class="flex-bigtitle">'.$featured_bigtitle.'</div>';
echo '<div class="flex-caption-wrap">';
echo $titlecode;
echo $captioncodes;
echo $bigtitle;
echo '</div>';
?>
</li>
<?php
endwhile; endif;
?>
</ul>
</div>
</div>
</div>
</div>
The problem I have is that once this works, it loads the sliders as posts to the home page and instead of the page I had selected (Home). The page loads fine if I delete the "get_template_part" from header.php, otherwise the sliders come as posts and I don't see the page I selected from reading on wordpress.
My website is http://van-london.com/
I made it!
All I needed was this code after the "get_template_part"
<?php wp_reset_query(); ?>
Done! :)
First time creating/implementing custom post types in Thesis, not first time using custom post types.
I used the Reed Write plugin to create the custom post types. The site is using Thesis 1.8.5.
On the following page (http://www.snyderleadership.com/press-releases/) I have the main content getting dropped in with the contents of the custom post type after it.
I used the custom_functions.php file to create a custom page template and call the db for the contents of the custom post type. Here is my code:
/* CUSTOM PRESS RELEASE TEMPLATE - ADDED by BRETT ATKIN */
function press_releases_page() {
if (is_page('press-releases') || is_page('583')) { ?>
<div id="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post_box">
<div class="headline_area"><h1><?php the_title(); ?></h1></div>
<div class="format_text">
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif ?>
<?php
$original_query = $wp_query;
$wp_query = null;
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="press-wrapper">
<div id="press-image">
<?php echo wp_get_attachment_image(get_post_meta($post->ID, 'release_image', true)); ?>
</div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo get_post_meta($post->ID, 'release_date', true); ?></p>
<p class="press-link"><?php echo get_post_meta($post->ID, 'release_title', true); ?></p>
<p class="press-author"><?php echo get_post_meta($post->ID, 'release_author', true); ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_postdata(); ?>
</div><!-- end press-releases div -->
</div>
</div>
</div><!-- end content -->
<?php echo thesis_sidebars(); ?>
<?php } }
add_action('thesis_hook_custom_template', 'press_releases_page');
It seems like everything is working correctly, just not pulling in the data for the custom post type.
Having done this on other sites (using custom themes), I'm not sure if I did something wrong here or if it is a Thesis issue.
Any help would be great.
Thanks
Brett
Here is the final working code. Thanks to the help from of a local WP Guru friend and maiorano84. We didn't figure out the cause, but we did fine a solution.
function press_releases_page() {
if (is_page('press-releases') || is_page('583')) { ?>
<div id="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post_box">
<div class="headline_area"><h1><?php the_title(); ?></h1></div>
<div class="format_text">
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif ?>
<?php
$original_query = $wp_query;
$wp_query = null;
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php $results = get_post_custom(); ?>
<div id="press-wrapper">
<div id="press-image"><?php echo wp_get_attachment_image($results['release_image'][0] ); ?></div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo $results['release_date'][0] ?></p>
<p class="press-link"><?php echo $results['release_title'][0] ?></p>
<p class="press-author"><?php echo $results['release_author'][0] ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_query(); wp_reset_postdata(); ?>
</div><!-- end press-releases div -->
</div>
</div>
</div><!-- end content -->
<?php echo thesis_sidebars(); ?>
<?php } }
remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
add_action('thesis_hook_custom_template', 'press_releases_page');
Try changing this:
<div id="press-releases">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="press-wrapper">
To this:
<div id="press-releases">
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div id="press-wrapper">
UPDATE:
The only other thing I can see is that you're not resetting your post data, and that the $wp_query variable is actually a Wordpress Global. Try using the reset functions and changing your WP_Query instance name like this:
<?php
wp_reset_query();
wp_reset_postdata();
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
UPDATE 2:
Now that you have your wrappers being written to your document, you need to normalize your information. Try to avoid using Metadata instead of normal post attributes. I imagine your metadata 'release_image' is a link to an image somewhere on your server:
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<div id="press-wrapper">
<div id="press-image">
<img src="<?php echo get_post_meta(get_the_ID(), 'release_image', true); ?>" />
</div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo the_date(); ?></p>
<p class="press-link"><?php the_title(); ?></p>
<p class="press-author"><?php the_author(); ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_postdata(); ?>