Syntax error in php echo concatenation - php

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>'.'';

Related

Add a div inside wordpress loop?

I am a relatively new developer, and just started with wordpress. I want for each post to be wrapped in a class of post. I keep getting this error no matter what i do. If i echo the div or add post_class.
<div class="posts">
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 4
);
// Variable to call WP_Query.
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// Start the Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div class='post'>'
the_post_thumbnail();
the_title('<h2>','</h2>');
the_excerpt();
echo '</div>'
endwhile;
else:
// If no posts match this query, output this text.
_e( 'Sorry, no posts matched your criteria.', 'textdomain' );
endif;
wp_reset_postdata();
?>
</div>
You need to do this:
echo '<div class="post">';
...
echo '</div>';

Why is my Wordpress Post Loop not working?

Okay, I am trying run the wordpress post loop to print out the post titles and content using the_title() and the_content(). But It is not printing out the posts on the page?
Any ideas?
index.php
<div class="blog-container">
<!-- The POST LOOP -->
<?php
if(have_posts()) :
while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else :
echo '<p>No content found</p>';
endif;
?>
</div>
EDIT Okay, I'm getting ONLY THE content printed out not the post titles, here is an image: http://imgur.com/a/DiBqG.
EDIT 2 Inside wordpress admin post should look like: http://imgur.com/a/1DQAp
You don't define the query and use the Wordpress build in functions in the wrong way. Here is the correction.
<?php
// The Query
$the_query = new WP_Query( );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
add below code for fetch post data
<?php
$post_args=array(
'type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'ASC',
);
$post_my_query = null;
$post_my_query = new WP_Query($post_args);
if( $post_my_query->have_posts() )
{
while ($post_my_query->have_posts()) : $post_my_query->the_post();
?>
<h2><?php echo get_the_title( $post_my_query->ID );?></h2>
<?php
echo get_the_content( $post_my_query->ID );
endwhile;
}
wp_reset_query($post_my_query);
?>

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; ?>

Access the_post() contents in a custom sidebar

I have this code in single-legislacion.php page:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php endwhile; ?>
<?php endif; ?>
<?php
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $thePostID,
'post_status' => 'inherit',
'numberposts' => 1
);
$attachments = get_posts( $args );
if ( $attachments ) :
foreach ( $attachments as $attachment ) : ?>
<div class="BaseIco">
<a class="IcoDescargas" href="<?php echo wp_get_attachment_url( $attachment->ID, true ); ?>">
<img src="<?php echo get_template_directory_uri(); ?>/images/ico_descargas.png"><br>
Descargar PDF
</a>
</div>
<?php endforeach;
endif;
?>
Because I am using Table of Content Plus plugin and have not found a way to display the generated TOC outside of the_content() itself, the only solution I have is to display it through a widget on a sidebar. Now, my question is: can I access the_post() content, like attachments for example, in a custom sidebar? How?
Also if any knows any variant to show the TOC outside the content, I'll be graceful if can share it.
No, I don't think so. the_post() only works inside of the loop, sidebars are usually rendered outside of it.
However, you can use get_post with global $post, i.e.:
global $post;
$p = get_post($post->ID);

PHP concatenation syntax error when using css for animation effect

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).

Categories