I've almost finished my first custom theme for a website. I've created some custom post type to manage a portfolio and a staff member section. No problem with the code, but the client asked if is it possible to change the staff images when an user hover on it. Is there any way to achieve this in wordpress?
Here is a sample of my code
<div class="team-boxed">
<?php
$args = array(
'post_type' => 'team'
);
$team = new WP_Query($args);
?>
<div class="intro">
<h2 class="text-center text-uppercase" id="">Team</h2>
</div>
<div class="row people">
<?php if( $team->have_posts() ): while( $team->have_posts() ): $team->the_post(); ?>
<div class="col-md-6 col-lg-4 item">
<div class="box">
<div class="rounded-circle" style="background-image:url('<?php the_post_thumbnail_url('full'); ?>')" id="staff-pic"></div>
<h3 class="team-name"><?php the_title(); ?></h3>
<p class="description text-center"><?php echo get_the_content(); ?></p>
<div class="social"><i class="fab fa-facebook"></i><i class="fab fa-twitter"></i><i class="fab fa-instagram"></i></div>
</div>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
Simplest way would be to output that like this. If you have ALT image stored in custom meta for example you can use that as SECOND IMAGE.
<img src="FIRST IMAGE" onmouseover="this.src='SECOND IMAGE'" onmouseout="this.src='FIRST IMAGE'" />
Other options include outputting some additional CSS or JS. But you can also use general JS with data attributes maybe to handle that with more options, animations etc.
Related
Hellow,
im trying to code a custom theme for my wordpress site. im trying to output 5 of my posts and i want to wrap them into a link. however when i foreach() through the posts and want to wrap my html in an tag with the permalink it wont work.
The code i wrote:
<?php
global $post;
$myposts = get_posts( array(
'posts_per_page' => 5,
) );
if ( $myposts ) {
foreach ( $myposts as $post ) :
setup_postdata( $post ); ?>
<div class="container-fluid">
<a class="post-link" href="<?php echo the_permalink();?>">
<div class="post-post p-5">
<div class="row">
<div class="col-md-12">
<?php the_category('<p></p>'); ?>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h3><?php the_title(); ?></h3>
</div>
</div>
<div class="row">
<div class="col-md-12">
<small><?php the_author(); ?></small>
</div>
</div>
</div>
</a>
</div>
<?php
endforeach;
wp_reset_postdata();
}
?>
But the code which ends up on the site is complete junk and it closes the a tag before the next div. what did i do wrong?
The code which lands on the site:
<div class="container-fluid">
<a class="post-link" href="http://localhost/wptheme/index.php/2021/01/11/oeoeoeoeoeoe/">
</a>
<div class="post-post p-5">
<a class="post-link" href="http://localhost/wptheme/index.php/2021/01/11/oeoeoeoeoeoe/">
</a>
<div class="row">
<a class="post-link" href="http://localhost/wptheme/index.php/2021/01/11/oeoeoeoeoeoe/">
</a>
<div class="col-md-12">
<a class="post-link" href="http://localhost/wptheme/index.php/2021/01/11/oeoeoeoeoeoe/">
</a>
Allgemein </div>
</div>
<div class="row">
<div class="col-md-12">
<h3>öööööö</h3>
</div>
</div>
<div class="row">
<div class="col-md-12">
<small>root</small>
</div>
</div>
</div>
</div>
WordPress' the_permalink() function prints the link with the tags.
Use get_the_permalink() function instead of the_permalink() for "echo"ing the link wherever needed.
It looks like you are trying to place an anker inside an anker this is not allowed, the output of your html is likely parsed / interpreted by your browser try looking at the sourcecode instead of the html after parsing by your browser. in chrome you can do so by visiting view-source:http://yourdomain.com
Not sure for other browsers, you can try right clicking in the page and select view source
I have a static slick slider which I use to run testimonials on, instead of being hard typed I want to pull them from WordPress custom post type which I have set up can someone point me in the right direction:
<section class="testimonials">
<div class="container text-center">
<div class="row">
<div class="col-md-12">
<div class="slick-testimonial">
<div class="item">
<div class="testimonial">
<img src="<?php bloginfo('template_url'); ?>/images/icons/testimonals.png" class="center-block">
<h1>What our customers say</h1>
<h3>"Fantastic service"</h3>
<p>Review Text Here</p>
<p class="name">Customer Name 1</p>
</div>
</div>
<div class="item">
<div class="testimonial">
<img src="<?php bloginfo('template_url'); ?>/images/icons/testimonals.png" class="center-block">
<h1>What our customers say</h1>
<h3>"Excellent"</h3>
<p>Review Text Here</p>
<p class="name">Customer Name 2</p>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
i believe i need to pull up an array for the custom post type and in the item pull the title name and text but not sure how to write it thanks.
You just need to interact with WordPress' WP_Query Class. It's the de facto way to get a handful of posts.
I'd also consider restructuring your slider a bit so the "What our Customers Say" is outside the individual slide item, but I digress:
All you have to do is set up a new WP_Query and replace the item div with a simple while loop:
<?php
$slider_args = array(
'post_type' => 'testimonials',
'posts_per_page' => 10
);
$slider_query = new WP_Query( $slider_args );
?>
<section class="testimonials">
<div class="container text-center">
<div class="row">
<div class="col-md-12">
<div class="slick-testimonial">
<?php
if( $slider_query->have_posts() ){
while( $slider_query->have_posts() ){ $slider_query->the_post(); ?>
<div class="item">
<div class="testimonial">
<img src="<?php bloginfo('template_url'); ?>/images/icons/testimonals.png" class="center-block">
<h1>What our customers say</h1>
<h3><?php the_title(); ?></h3>
<p><?php the_content(); ?></p>
<p class="name"><?php echo get_post_meta( get_the_ID(), 'reviewer_name', true ); ?></p>
</div>
</div>
<?php }
} else { ?>
<div class="item">
<div class="testimonial">
<h3>No Testimonials Found</h3>
</div>
</div>
<?php }
wp_reset_postdata();
?>
</div>
</div>
</div>
</div>
</section>
Note that this code makes a few assumptions. You'll need to replace the post_type with the name of the CPT you have registered, and you can adjust how many to get with the posts_per_page argument. I picked 10 just because.
This also assumes you're saving the Reviewer Name in a meta field named reviewer_name, but it should be enough to get you started
I'm having a problem where after I have ran a while loop to get my thumbnail images my advanced custom fields that run after the loop don't display.
I'm assuming the fix is similar to here Stackoverflow question
However I cant for the life of me work it out.
Here's the section with the loop. I'm using it inside a div so I can change the background of the div.
Anyone know the problem?
gist here
<section id="portfolio">
<h3>Projects</h3>
<div class="card-container">
<?php global $query_string;
query_posts ('posts_per_page=3');
while(have_posts()) : the_post(); ?>
<div class="outer">
<div class="target">
<div class="card">
<div class="front">
<div class="thumbnail" <?php
if ( $id = get_post_thumbnail_id() ) {
if ( $src = wp_get_attachment_url( $id ) )
printf( ' style="background-image: url(%s);"', $src );
}
?>>
</div>
<h5><?php the_title(); ?></h5><?php the_excerpt('Read More'); ?>
View Site
</div>
<div class="back">
<h5><?php the_title(); ?></h5>
<hr>
<p class="return"><?php the_content() ?></p>
View Site
</div>
</div>
</div>
</div>
<?php endwhile; ?>
View More
</div>
</section>
This pastie has both the functions.php and home.php code for review. For reference, my goal is to make the image follow the same rules found in this jsfiddle.
My goal with the function is to pull the first image from a post and put it above the summary on my blog's homepage. I've been able to pull the image from the post, but it isn't following the responsive class I used in line 60. Is it that I am pulling the image itself and not the URL? How can I adjust my function to make this work? Outside of database connections and form validation, I am a complete php beginner.
Please see jsfiddle and pastie for code
Use Post_Thumbnails instead.
In your functions.php add the following:
add_theme_support( 'post-thumbnails' );
This will enable the Featured Image meta box in the sidebar of your posts and pages. You will need to set this for posts. Instead of adding an image to your post's content, you can set your desired image as the featured image.
Update your home.php to the following:
<?php get_header(); ?>
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Blog</h1>
<ol class="breadcrumb">
<li>Home
</li>
<li class="active">Blog</li>
</ol>
</div>
</div>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="row">
<div class="col-md-1 text-center">
<p><?php the_time('l, F jS, Y'); ?></p>
</div>
<?php if ( has_post_thumbnail() ) : ?>
<div class="col-md-5">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'large', array( 'class' => 'img-responsive img-hover' ) ); ?>
</a>
</div>
<?php endif; ?>
<div class="col-md-6">
<h3>
<?php the_title(); ?>
</h3>
<p>by <?php the_author_posts_link(); ?>
</p>
<p><?php the_excerpt(); ?></p>
<a class="btn btn-primary" href="<?php the_permalink(); ?>">Read More <i class="fa fa-angle-right"></i></a>
</div>
</div>
<hr>
<?php endwhile; ?>
<div class="navigation"><p><?php posts_nav_link('','« Newer Posts','Older Posts »'); ?></p></div>
<?php else: ?>
<p><?php _e('Sorry, there are no posts.'); ?></p>
<?php endif; ?>
</div>
<?php get_footer(); ?>
has_post_thumbnail() checks to see if the current $post has a featured image set.
the_post_thumbnail() outputs the image tag. This functions takes a size as it's first parameter. We are setting this to large as it should satisfy your layout.
The second parameter is an array of attributes. We are able to add additional classes to the image using this parameter.
I did this to show 5 posts of a diferent category in my wordpress page:
<?php $archive_query = new WP_Query('category_name=anc&showposts=5');
while ($archive_query->have_posts()) : $archive_query->the_post(); ?>
<div class="collection">
See All
</div>
<div class="row">
<div class="col s12">
<div class="card">
<div class="card-image">
<img src="<?php echo catch_that_image(); ?>">
<span class="card-title"><?php the_title(); ?></span>
</div>
<div class="card-content">
</div>
<div class="card-action">
Ver más
</div>
</div>
</div>
</div>
<?php endwhile; ?>
It worked, however, now my another pages don´t work, they show posts from that category when i click on those pages instead of showing their respectives posts, what could be the issue?
You can reset data in the global $post object after endwhile;
<?php
while( $your_query->have_posts() ):
...
endwhile;
wp_resest_postdata(); ?>
I came to a solution, after the following line:
<?php endwhile;?>
I wrote this:
<?php wp_reset_query();?>
It resets the query so like that the main loop ( because it´s depend of the main loop ) can work perfectly, i hope this helps everyone, this code is an easy way to show any post of a different category.