PHP concatenation syntax error when using css for animation effect - php

In a Wordpress page template I have the following code in an effort to replicate a successful archive page:
<?php $args = array( 'post_type' => 'casestudies', 'posts_per_page' => 12 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="casestudy">'.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>';
echo '<span class="details"><div class="anchor-hover details-h3">'.the_title().'</div>';
echo '<p class="desc">'.get_post($post_id)->post_excerpt.'</p></span>';
endwhile; ?>
<div class="clear"></div>
I need to compress that into only one echo, in order to have the css animation work. I will also need to wrap the entire display starting with "echo" with this div:
<div<?php post_class('margin') ?> id="post-<?php the_ID(); ?>">
The title and excerpt are only supposed to show when the cursor is hovering over the thumbnail, but I can't get this line of code in without an unexpected syntax error.
I am able to get this to work on the archive for this post type so there should be some way to do it.
Am I asking too much of this kind of code? Is there a reason I can get the animation working on the archive page working but not this one? Thanks

you can't double parse php...
echo '<div class="casestudy">'.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>';
should be
echo '<div class="case study">'.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>';
specifically:
<?php the_permalink() ?>
should be
.'the_permalink().'

It might help you to format your code a bit. It looks like there may be problems with single and double quotes, along with PHP tags inside strings. If you are creating large HTML template fragments, you may want to use something like ob_start() and ob_get_clean()
Here's just a quick example using your code:
<?php
$args = array( 'post_type' => 'casestudies', 'posts_per_page' => 12 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post();
ob_start();
?>
<div class="casestudy">
<a href="<?php the_permalink() ?>" class="anchor- hover">
<?php
echo get_the_post_thumbnail( $post->ID, '180,180' );
?>
</a>
</div>
<span class="details">
<div class="anchor-hover details-h3">
<?php
echo the_title();
?>
</div>
<p class="desc">
<?php
echo get_post($post_id)->post_excerpt;
?>
</p>
</span>
<?php
echo ob_get_clean();
}
?>
<div class="clear"></div>
Side note: you probably shouldn't put an inline element (span) inside a block level element (div).

Related

Add an if alternative statement inside another if statement within a loop with php to create two links (one real and one empty)

First the problem:
My code is broken if I add an if alternative statement inside another if statment in a loop:
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<?php $partner = the_field('industry_partner_links'); ?>
<?php
if ($partner) :
?>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
else :
?>
<a href="#" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
endif;
?>
</li>
My goal is to create a loop in my template to display some image (they have a URL that will direct to an image) based on a custom post in a Wordpress environment BUT, if the image doesn't have the link (ACF), the url will be empty (href=#").
Essentially, I created a section with my sponsors (image wrapped in a link) and I feed those images and URLs from the backend of WordPress using ACF (advanced custom field). Spoiler, it works.
Tha basic code in php:
<?php
$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'partner',
'post_status' => 'publish',
];
if ( $posts = get_posts( $args ) ) {
echo '<ul class="feed-industry-partners">';
foreach ( $posts as $post ) {
setup_postdata( $post );
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
</li>
<?php endif;
wp_reset_postdata();
}
echo '</ul>';
}
?>
Any Idea why it is not working?
From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)

How can I add a scroll/see more feature to my 'Latest Products' on the WooCommerce platform?

I have created a 'Latest Products' loop by inserting the following code into my 'index.php' file:
<section id="recent">
<h1>Recently Added</h1>
<ul class="row-fluid">
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
<li class="span3">
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?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="65px" height="115px" />'; ?>
<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>
</section>
I now wish to be able to apply an arrow on the left and right of this row, where upon selecting the arrow, it will scroll through the images.
I am aware of the following codes:
<?php previous_post_link(); ?>
<?php next_post_link(); ?>
However, upon selection of their links, the user is taken to its dedicated page rather than being able to 'scroll' through the products.
Any suggestions on how I can achieve this?
Maybe you are looking for a carousel? There are a lot of jQuery plugins, i have used this one.
If you choose this solution, you should get the script, put it in your theme, add it, preferably in functions.php (more info here) and then add the carousel itself.
$('.row-fluid').owlCarousel({})
But insteal of all this maybe using a plugin like "WooCommerce Product Slider" or similar will be better. It provides shorcode and widget implementation which should work with most themes and can be used directly in the control panel withouth coding. One more note - if you still prefer to use your own code - maybe it'll be better to put it in a template file instead in index.php

Display (Wordpress) Child Pages with Title, Thumbnail and Excerpt

Apologies in advance for a question, which to an expert, I've no doubt is relatively obvious. I've looked at WordPress Codex for the appropriate information (the_post_thumbnail, the_excerpt, etc) but am not well versed enough in .php yet to implement it properly. Still learning!
I am trying to display, within the of a standard (WP) page, the child-pages, including their Title, Thumbnail and Excerpt. I can get everything to work bar the THUMBNAIL and EXCERPT with the following:
<div class="child-pages">
<?php
$pageChild = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC' ) );
foreach( $pageChild as $page ) {
?>
<!-- loop: child page -->
<div class="child">
<header class="entry-header">
<?php echo '<h3>'.$page->post_title.'</h3>'; ?>
</header><!-- .entry-header -->
<img src="<?php echo the_post_thumbnail_url( $page->ID ); ?>">
<?php echo $page->the_excerpt; ?>
</div>
<?php } ?>
</div>
So far, I can see the links/titles of the correct child-pages, and in the correct order, but not the Thumbnail or Excerpt. Obviously, I am not calling the Thumbnail or the Excerpt properly. Could someone please correct me?
I have also tried these lines, as supported by the twenty-sixteen theme:
<?php twentysixteen_post_thumbnail(); ?>
<?php the_excerpt(); ?>
Any help would be much appreciated!
The thumbnail doesn't show because the function the_post_thumbnail_url(); shows the thumbnail of the current post, the parameter is to specify the size of the image, not the post.
$post->the_excerpt is not necessarily filled. If you look at the add/edit post screen you'll notice two textfields, one for editing the content of the post and one for the excerpt. The excerpt is optional, so the function the_excerpt() shows the content of that field, but when it is empty it will show the first X characters of $post->the_content.
Regarding your second attempt: the functions the_excerpt(), get_permalink() and twentysixteen_post_thumbnail() don't work because you don't set up the post properly.
The easiest way would be to add setup_postdata() to your code:
$pageChild = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC' ) );
foreach( $pageChild as $page ) {
setup_postdata( $page );
// now you can use all the fancy functions like `the_excerpt()`
// add your output here
}
Or you can do it the recommended way, using a WP_Query object:
<?php
$args = array(
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

How to wrap echo div in <a href>

I'm running into two problems transferring a Wordpress archive page into a page template.
<?php $args = array( 'post_type' => 'casestudies', 'posts_per_page' => 12 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="casestudy">'.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>';
First I need to wrap the echo for the thumbnail with a link in order to trigger a css action:
'<a href="'.the_permalink().'" class="anchor-hover">';
When I add that a href line it only prints the link, and doesn't wrap the echo.
The a href should then print the title and excerpt over the thumbnail box:
'<span class="details"><div class="anchor-hover details-h3"><?php the_title(); ?></div>';
'<p class="desc"><?php echo get_post($post_id)->post_excerpt; ?></p></span></a>';
endwhile; ?>
<div class="clear"></div>
</div></div>
In wordpress, you have two type types of functions. One's that return the results, and one's that echo the results.
You are using the_permalink(); which is already echoing out it's result.
You need to be using get_the_permalink(); since you are trying to echo it in your template.
get_permalink worked for that a href, thank you, but how can I get php the_title() and php echo get_post($post_id)->post_excerpt to show up on the cursor hover?
This code should be working if you are in the loop
LINK
The method get in WordPress will give you the string only.
http://codex.wordpress.org/Function_Reference/get_the_excerpt
http://codex.wordpress.org/Function_Reference/get_the_title

Syntax error in php echo concatenation

I have the following code in a Wordpress page template:
<?php $args = array( 'post_type' => 'casestudies', 'posts_per_page' => 12 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="casestudy">'.''.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>'.'';
echo '<span class="details">'.'<div class="anchor-hover details-h3">'.the_title().'</div>';
echo '<p class="desc">'.get_post($post_id)->post_excerpt.'</p>'.'</span>';
endwhile; ?>
<div class="clear"></div>
I need to wrap the entire display starting with "echo" with this div:
<div<?php post_class('margin') ?> id="post-<?php the_ID(); ?>">
The title and excerpt are only supposed to show when the cursor is hovering over the thumbnail, but I can't get this line of code in without an unexpected syntax error.
I am able to get this to work on the archive for this post type so there should be some way to do it.
| HERE | OR RATHER HERE
echo '<div class="casestudy">'.''.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>'.'';

Categories