I'm trying to finish 'themeing' my Wordpress / jQuery / JS slider; I currently have it where it successfully grabs the most recent Wordpress posts, but doesn't fully cycle through them in the main 'featured window'? (JavaScript error?) Also, I've yet to be able to successfully add the 'featured_image' to allow setting the image used in the 'featured window'.
I've gotten this far via some help at the base question: 'Themeing' my Slider in WP with PHP
Below is the code I'm using:
<?php
/**
* #package WordPress
* #subpackage Default_Theme
*/
$pagePtr="home";
include # ("header.php");
//get_header(); ?>
<div id="content">
<?php
/**
* #package WordPress
* #subpackage Default_Theme'
*/
//get_header(); ?>
<div id="content">
<?php if (have_posts()) : ?>
<!--Your slider code goes here-->
<?php
$args = array(
'numberposts' => 5,
'orderby' => 'post_date',
'order' => 'DESC'
);
$posts_array = get_posts( $args );
?>
<div id="featured" >
<ul class="ui-tabs-nav">
<?php
$i = 1;
foreach ($posts_array as $post) : setup_postdata($post);
?>
<li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-<?php echo $i; ?>"><img src="<?php echo $featured_image; ?>" alt=""/><span><?php the_title(); ?><br /><p class="info" style="padding-left:10px;"><?php the_excerpt(); ?></p></span></li>
<?php $i++;
endforeach; ?>
</ul>
<?php
$i = 1;
foreach ($posts_array as $post) : setup_postdata($post);
?>
<!-- First Content -->
<div id="fragment-<?php echo $i; ?>" class="ui-tabs-panel" style="">
<img src="<?php the_post_thumbnail('slider_image'); ?>" alt="" />
<div class="info" >
<h2><a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?><a href="<?php the_permalink(); ?>" >read more</a></p>
</div>
</div>
<?php $i++; endforeach; ?>
</div>
<!--Your slider code goes here-->
<!-- End Featured Lists Image Slider -->
<?php endif; ?>
<?php if($featured_image= get_post_meta($post->ID, "featured_image", true)) { ?>
<div class="postThumbnail"><img src="<?php echo $featured_image; ?>" height="190" width="125" border="0" /></div>
<?php } ?>
The Live Version via my test site: http://tinyurl.com/7q3o97u
Notice how the right side boxes are working correctly, and bringing in the recent posts - but for some reason the main area / left larger window, is not acting same?? How can I get this working properly??
I'm not sure it's the fundamental cause of your problem, but this line:
<img src="<?php the_post_thumbnail('slider_image'); ?>" alt="" />
is causing a problem in the markup. It renders:
<img src="<img width="940" height="340" src="http://www.osmproduction.com/RIF/wp-content/uploads/2012/03/MaxBrooksRecordedAttacks-940x340.png" class="attachment-slider_image wp-post-image" alt="MaxBrooksRecordedAttacks" title="MaxBrooksRecordedAttacks" />" alt="" />
which isn't right. the_post_thumbnail returns a full HTML img tag, not just the image URL. Try replacing
<img src="<?php the_post_thumbnail('slider_image'); ?>" alt="" />
with
<?php the_post_thumbnail('slider_image'); ?>
Related
i just setup isotope manually into my website after that i dynamic it with the help of get_terms and get_ther_terms condition here is the full code
<div class="main_page">
<div class="portfolio_section">
<div class="controls">
<button type="button" class="control" data-filter="all">All</button>
<?php
$cat_list = get_terms('filters');
foreach ($cat_list as $cat) :
?>
<button type="button" class="control" data-filter=".<?php echo $cat->slug;?>"><?php echo $cat->name; ?></button>
<?php endforeach; ?>
</div>
<div class="container pasresnt">
<?php
$mixitup = new WP_Query(array(
'post_type' => 'portfolio',
'posts_per_page' => -1,
));
?>
<?php while ($mixitup->have_posts()) : $mixitup->the_post() ; ?>
<div class="mix <?php
$cat_slug = get_the_terms($post->ID, 'filters');
foreach ($cat_slug as $cat_sl) {
echo $cat_sl->slug;
}
?>">
<?php the_post_thumbnail('portfolio-small'); ?>
</div>
<?php endwhile; ?>
<div class="mix green">
<a href="<?php echo get_template_directory_uri(); ?>/img/mahi.jpg"><img src="<?php echo get_template_directory_uri(); ?>/img/mahi.jpg"
alt=""></a>
</div>
</div>
</div>
</div>
Here is the highlights in normal static image popup come correctly with this code
<a href="<?php echo get_template_directory_uri(); ?>/img/mahi.jpg"><img src="<?php echo get_template_directory_uri(); ?>/img/mahi.jpg"
alt=""></a>
but in dynamic this images popup doesn't work for me .the last static one is as example which is work perfectly but this line code which is the images come from portfolio custom post options this pop up not working
<?php the_post_thumbnail('portfolio-small'); ?>
`
You are using the_post_thumbnail for the href. That outputs the html to display the image, but you only want the URL to put into the href attribute of your link, so use get_the_post_thumbnail_url() instead.
So instead of this:
<?php the_post_thumbnail('portfolio-small'); ?>
You should use this:
<a href="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'portfolio-small'); ?>" target="_blank">
<?php the_post_thumbnail('portfolio-small'); ?>
</a>
The featured thumbnail image is supposed to just show the single thumbnail image of the post but instead it pulls every post image and displays them instead for every post. Instead of just the post thumbnail. This is the code for it and also what it looks like on the page:
<?php $featured_image = new WP_Query('page_id=ID'); ?>
<?php while ($featured_image->have_posts()) : $featured_image->the_post(); ?>
<?php if (function_exists('has_post_thumbnail') && has_post_thumbnail()) { ?>
<?php $img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(302, 170)); ?>
<div class="the-image">
<img src="<?php echo $img_src[0]; ?>" />
</div>
<?php }; ?>
<?php endwhile; ?>
thats the code for servering the image and heres what happens on the main page (I have 2 posts currently and a featured test image for both of them, the scaling works but as you can see, it puts both images, on both posts...)
I'm not sure if it needs to be inside my loop for the title and excerpt, or outside of it (currently inside).
Here is the full code for the recent posts sidebar:
<div class="col-lg-4 d-none d-lg-block">
<h3 style="text-align: center; font-weight: 700;">Recent Posts</h3>
<?php
$result = wp_get_recent_posts(array(
'numberposts' => 10,
'category' => '',
'post_status' => 'publish',
));
foreach( $result as $p ){
?>
<div class="paddingarea text-dark">
<?php if (function_exists('has_post_thumbnail') && has_post_thumbnail()) { ?>
<?php $img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(302, 170)); ?>
<div class="the-image">
<img src="<?php echo $img_src[0]; ?>" />
</div>
<?php }; ?>
<a class="card-title" href="<?php echo get_permalink($p['ID']) ?>" style="font-weight: 600;"><?php echo $p['post_title']?></a><br />
<p class="card-text"><?php echo excerpt(10); ?></p>
</div>
<?php
}
?>
</div>
Try this, it's simple and it's working for me:
<?php
$result = wp_get_recent_posts(array(
'numberposts' => 10,
'category' => '',
'post_status' => 'publish',
));
foreach( $result as $p ){
?>
<div class="paddingarea text-dark">
<div class="the-image">
<img src="<?php echo get_the_post_thumbnail_url($p['ID'], array(302, 170)); ?>" />
</div>
<a class="card-title" href="<?php echo get_permalink($p['ID']) ?>" style="font-weight: 600;"><?php echo $p['post_title']?></a><br />
<p class="card-text"><?php //echo excerpt(10); ?></p>
</div>
<?php
}
?>
I have three separate fields in acf one for each image named slide_one, slide_two and slide_three. Now when I try to get the images to display using the code below nothing is being output. I am doing it this why and not using a loop like you usually as I'm wanting to have a separate text overlay for each image that I will have sliding in separately. This is the first step and it won't work. What an I doing wrong?
<div class="slider">
<ul class="slides">
<?php
$image_one = get_field('slide_one');
if( !empty($image_one ): ?>
<li>
<img src="<?php echo $image_one['url']; ?>" alt="<?php echo $image_one['alt']; ?>" />
</li>
<?php endif; ?>
<?php
$image_two = get_field('slide_two');
if( !empty($image_two ): ?>
<li>
<img src="<?php echo $image_two['url']; ?>" alt="<?php echo $image_two['alt']; ?>" />
</li>
<?php endif; ?>
<?php
$image_three = get_field('slide_three');
if( !empty($image_three ): ?>
<li>
<img src="<?php echo $image_three['url']; ?>" alt="<?php echo $image_three['alt']; ?>" />
</li>
<?php endif; ?>
</ul>
</div>
I am using Advanced Custom Fields plugin in Wordpress and have managed (with very limited PHP knowledge) to use their repeater fields to create an accordion.
Everything is working really well except the image field (company_logo). So long as the user selects an image for this custom field it displays fine but if they do not select an image I get some strange text instead.
Using my current code I'm trying to add in an 'if' statement so if they do not select an image it displays a default image instead. I've stride lots of variations but cannot get it to work.
Can anyone help/point me in the right direction please? Also, if theres a way I can clean this up as I seem to use a lot of
<div id="accordion">
<?php if( have_rows('exhibitor') ): ?>
<?php while( have_rows('exhibitor') ): the_row(); ?>
<h4 class="accordion-toggle"><?php the_sub_field('exhibitor_type'); ?></h4>
<div class="accordion-content">
<?php while( have_rows('table') ): the_row(); ?>
<div class="exhibitor-single">
<p class="table-field">Table <?php the_sub_field('table_no'); ?></p>
<p><?php the_sub_field('company_name'); ?></p>
<?php $image = wp_get_attachment_image_src(get_sub_field('company_logo'), 'logo'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_sub_field('company_logo')) ?>" />
<p><?php the_sub_field('company_website'); ?></p>
</div>
<?php endwhile; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
You should be able to just wrap the image 'section' in this:
<?php
if(get_sub_field('company_logo')) :
$image = wp_get_attachment_image_src(get_sub_field('company_logo'), 'logo');
?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_sub_field('company_logo')) ?>" />
<?php
else :
?>
<!-- Your default image here -->
<img src="" alt="" />
<?php
endif; ?>
I am trying to pull the first image from any new posts to my slider but having one hell of a time trying to get it to work right this is what I have so far
<div id="anicontainer">
<div id="aninav">
<ul>
<li id="featuredtab"> </li>
<li id="latesttab"> </li>
<li id="populartab"> </li>
</ul>
</div>
<!-- Sub Menus container. Do not remove -->
<div id="ani_inner">
<div id="featuredouter" class="innercontent">
<div id="featured" >
<ul class="ui-tabs-nav">
<?php
$ani_featured_slide = get_option('x12_featured_slide');
$my_query = new WP_Query("cat=".$ani_featured_slide."&showposts=5");
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li class="ui-tabs-nav-item" id="nav-fragment-<?php the_ID(); ?>"><img src="<?php bloginfo('template_url'); ?>/thumb.php?src=<?php echo catch_that_image() ?>&w=50&h=44&zc=1&q=100" alt="" /></li>
<?php endwhile; ?>
</ul>
<?php $my_query = new WP_Query("cat=".$ani_featured_slide."&showposts=5");
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<!-- Slide Content -->
<div id="fragment-<?php the_ID(); ?>" class="ui-tabs-panel" style="">
<img src="<?php bloginfo('template_url'); ?>/thumb.php?src=<?php echo catch_that_image() ?>&w=368&h=256&zc=1&q=100" alt="" />
<div class="info" >
<h2><?php the_title(); ?></h2>
<p><?php ani_content_limit(150, "Read More →"); ?></p>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<div id="latest" class="innercontent">
<?php $recent = new WP_Query("showposts=6"); while($recent->have_posts()) : $recent->the_post();?>
<div class="latestpost">
<?php if( get_post_meta($post->ID, "postimage", true) ): ?>
<img src="<?php bloginfo('template_url'); ?>/thumb.php?src=<?php echo catch_that_image() ?>&w=70&h=70&zc=1&q=100" class="latestpostimage" alt="<?php the_title(); ?>" />
<?php else: ?>
<img class="latestpostimage" src="<?php bloginfo('template_url'); ?>/images/postimage.png" alt="<?php the_title(); ?>" /><?php endif; ?><h2><?php the_title(); ?></h2>
<div class="shortstory">
<?php ani_content_limit(55, "Read More →"); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<div id="popular" class="innercontent">
<div id="populartags"><ul><li>
<?php wp_tag_cloud('smallest=9&largest=25&number=40'); ?>
</li>
</ul>
</div>
</div>
Any my function for grabbing the image is this
<?php function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
?>
Am new to php most of this was from google :(
If anyone can see a problem it would mean alot to me cheers
Have you considered using WordPress featured images? It would be much easier to set up for what you are trying to achieve...
In your theme's functions.php file:
add_theme_support( 'post-thumbnails' ); // add theme support
add_image_size( 'slide', 368, 256, true ); // add an image size
See http://codex.wordpress.org/Function_Reference/add_theme_support and http://codex.wordpress.org/Function_Reference/add_image_size for more info on these
Then you would have inside the loop something like:
<?php if(has_post_thumbnail()) {
the_post_thumbnail( 'slide' );
} else {
// no image found
} ?>
More info on this can be found here: http://codex.wordpress.org/Function_Reference/has_post_thumbnail and here: http://codex.wordpress.org/Function_Reference/the_post_thumbnail