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! :)
Related
I want to add a link when my post gets click on. The link has to go to the category of the post. I've got this far but now I'm stuck. Can anyone show me how I can do this?
<?php
$args = array(
'category_name' => 'portriats',
'posts_per_page' => 1
);
$qry = new WP_Query($args);
if ( $qry->have_posts() ) :
while ( $qry->have_posts() ) : $qry->the_post();
$postcat = get_the_category( $post->ID );
?>
<div class="hometile">
<a href="<?php get_category_link( $postcat) ?>">
==> **I need to get the category of the post and then let PHP print the link of the categorey in the href**
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php
endwhile;
endif;
?>
It will display primary category of post with link
<?php $qry = new WP_Query($args ); ?>
<?php if ( $qry->have_posts() ) : ?>
<?php while ( $qry->have_posts() ) : $qry->the_post(); $postcat = get_the_category();?>
<div class="hometile">
<a href="<?php echo get_category_link( $postcat[0]->term_id ); ?>">
<?php echo $postcat[0]->name; ?>
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
I have the code to display the latest posts on my website, but I wonder if there is a way to make a list of the latest posts, displaying only one post per category. Let's say I have 7 categories, so only 7 posts will be displayed on the page. What should I do?
<?php if ( ! is_single() ) { ?>
<div class="post-container">
<?php } ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
//Post Title code
//Post Thumbnail code
//Post Content / Excerpt code
//Post Meta code
</article> <!-- /post -->
<?php if ( ! is_single() ) { ?>
</div>
<?php
<?php } ?>
It's very easy to add latest post from each category.
First of all get all the categories of blog by using below code:
$categories = get_categories();
Then use foreach ( $categories as $category ) {} to tell WordPress to run through each of these categories in turn and run the code inside the braces.
Now you need to define the arguments for your query. Inside the braces, add this:
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Next, insert your query, using the WP_Query class:
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<?php the_excerpt( __( 'Continue Reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); ?>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
This will display each category posts in your home page. Please try to use it and let me know if you have any issue.
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.
I'm trying to display 3 posts below my single post view (I have custom post types set up so want this query to work on all single post pages regardless of the post type).
However with the code below I don't get any related posts displayed. When I removed .'&exclude=' . $current I get 3 related posts display but with the current post being one of them. Hence why I added 'exclude' but I don't see why its not displaying any when I add this in.
Any help would be appreciate.
Thanks
<?php
$backup = $post;
$current = $post->ID; //current page ID
global $post;
$thisPost = get_post_type(); //current custom post
$myposts = get_posts('numberposts=3&order=DESC&orderby=ID&post_type=' . $thisPost .
'&exclude=' . $current);
$check = count($myposts);
if ($check > 1 ) { ?>
<h1 id="recent">Related</h1>
<div id="related" class="group">
<ul class="group">
<?php
foreach($myposts as $post) :
setup_postdata($post);
?>
<li>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark">
<article>
<h1 class="entry-title"><?php the_title() ?></h1>
<div class="name-date"><?php the_time('F j, Y'); ?></div>
<div class="theExcerpt"><?php the_excerpt(); ?></div>
</article>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php
$post = $backup;
wp_reset_query();
?>
</div><!-- #related -->
<?php } ?>
Instead of using get_posts(), you could use WP_Query
<?php
// You might need to use wp_reset_query();
// here if you have another query before this one
global $post;
$current_post_type = get_post_type( $post );
// The query arguments
$args = array(
'posts_per_page' => 3,
'order' => 'DESC',
'orderby' => 'ID',
'post_type' => $current_post_type,
'post__not_in' => array( $post->ID )
);
// Create the related query
$rel_query = new WP_Query( $args );
// Check if there is any related posts
if( $rel_query->have_posts() ) :
?>
<h1 id="recent">Related</h1>
<div id="related" class="group">
<ul class="group">
<?php
// The Loop
while ( $rel_query->have_posts() ) :
$rel_query->the_post();
?>
<li>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark">
<article>
<h1 class="entry-title"><?php the_title() ?></h1>
<div class="name-date"><?php the_time('F j, Y'); ?></div>
<div class="theExcerpt"><?php the_excerpt(); ?></div>
</article>
</a>
</li>
<?php
endwhile;
?>
</ul><!-- .group -->
</div><!-- #related -->
<?php
endif;
// Reset the query
wp_reset_query();
?>
Try out the code above and modify it for your own need. Modified to suit your own markup.
I need to add links to WordPress Posts on a static HTML page. I got some information that has been helpful but need a few more elements to make it complete.
Here is the code I have so far:
<?php
$number = 5;
$wordpress_header = "blog/wp-blog-header.php";
// Include wordpress header
if (file_exists($wordpress_header))
{
include ($wordpress_header);
$myposts = get_posts('numberposts=$number&offset=0&category=0');
echo "<ul class='Bloglinks'>";
foreach(array_slice($myposts, 0, $number) as $post)
{
echo '<li><a href="';
the_permalink();
echo '">';
the_date();
echo " ";
the_title();
echo '</a></li>';
}
echo "</ul>";
}
else
{
echo "Unable to connect to Wordpress header file.";
die();
}
?>
This only shows the titles of the most recent posts. I need to be able to display the following:
<h5>The truth about Lazy Eye</h5>
<p class="blog-info">07.16.10 | <a class="comment-ref">3 Comments</a></p>
<h5>More Adult Learning Problems Linked to Eyes</h5>
<p class="blog-info">06.12.10 | <a class="comment-ref">1 Comments</a></p>
<h5>New Vision Examination Instruments Arrived!</h5>
<p class="blog-info">05.10.10 | <a class="comment-ref">13 Comments</a></p>
You should use the function query_posts() with the functions have_posts() and the_post(). Here is an example for the WordPress API:
//The Query
query_posts('posts_per_page=5');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
That will loop through all posts you have queried. So you can just insert your query from the get_posts() function into the query_posts() function.
EDIT: I think if you want to stick with the get_posts() function, you have to call the setup_postdata() function to get the new post (source code for the API):
<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&offset=1&category=1');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
But I would recommend to take the query_posts() function instead.
<?php
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
$args = array(
// 'cat' => 3, // Only source posts from a specific category
'posts_per_page' => 6 // Specify how many posts you'd like to display
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post(); ?>
<article class="vertical-item content-padding ls">
<div class="item-media">
<img src="<?php the_post_thumbnail() ?>" alt="<?php the_title(); ?>">
</div>
<div class="item-content">
<h4 class="entry-title">
<?php the_title(); ?>
</h4>
<div>
<div class="media-body media-middle greylinks">
<br><a class="small-text" href="#"><?php the_time('l jS F, Y') ?></a>
</div>
</div>
</div>
<div class="item-footer">
<a class="lato lightgrey weight-black" href="<?php the_permalink(); ?>">Read this Article</a>
</div>
</article>
<? }
} else {
echo '<p>There are no posts available</p>';
}
wp_reset_postdata();
?>