Reputation: 379
PHP is not my area of expertise and I have been banging my head against a wall for 2-3 days now trying to figure this out.
I need to run a custom WordPress query, to display some posts on a page, and in order to do it I have installed the PHP Everywhere plugin to convert PHP to shortcodes.
I have this loop (it could probably use some work too) and it works fine on its own, but the way the plugin works I need to run multiple instances of this loop with different category IDs, to show posts from different categories on the same page.
This is the loop I have now:
<?php
if ( get_query_var( "paged" ) ) { $paged = get_query_var( "paged" ); }
elseif ( get_query_var( "page" ) ) { $paged = get_query_var( "page" ); }
else { $paged = 1; }
$the_query = new WP_Query("cat=20&posts_per_page=5&paged=" . $paged);
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="post_square">
<a href="<?php the_permalink() ?>" class="post_image" style="background-image: url(<?php the_post_thumbnail_url(); ?>);"></a>
<div class="post_text">
<div class="post_title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
<div class="post_meta">By <?php $author = the_author_meta("display_name"); ?> / <?php echo get_the_date("F j, Y"); ?></div>
</div>
</div>
<?php endwhile; ?>
<?php
next_posts_link( "Older Entries", $the_query->max_num_pages = 3 );
previous_posts_link( "Newer Entries" );
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( "Sorry, no posts matched your criteria" ); ?></p>
<?php endif; ?>
The plugin requires me to format my PHP as follows, to run multiple instances on the same page:
<?php
if($instance=="1"){
echo("Instance 1");
}
if($instance=="2"){
echo("Instance 2");
}
?>
My problem is re-writing my original PHP code to fit within the echo of the second loop.
I tired this code below, but it's not working:
<?php
if ( $instance=="1" ) {
if ( get_query_var( "paged" ) ) { $paged = get_query_var( "paged" ); }
elseif ( get_query_var( "page" ) ) { $paged = get_query_var( "page" ); }
else { $paged = 1; }
$the_query = new WP_Query("cat=20&posts_per_page=5&paged=" . $paged);
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div class="post_square">
<a href="'. the_permalink() .'" class="post_image" style="background-image: url('. the_post_thumbnail_url(); .');"></a>
<div class="post_text">
<div class="post_title"><a href="'. the_permalink() .'">'. the_title(); .'</a></div>
<div class="post_meta">By '. $author = the_author_meta("display_name"); .' / '. echo get_the_date("F j, Y"); .'</div>
</div>
</div>';
endwhile;
next_posts_link( "Older Entries", $the_query->max_num_pages = 3 );
previous_posts_link( "Newer Entries" );
wp_reset_postdata();
else:
_e( "Sorry, no posts matched your criteria" );
endif;
}
if ( $instance=="2" ) {
echo 'this is two';
}
?>
I get 3 syntax error warnings, inside Dreamweaver, but I don't know PHP well enough to know which '.' it's referring to:
I'm at a loss. I've tried numerous ways to do this, but it's beyond me...
Upvotes: 0
Views: 121
Reputation: 2654
Look like you have to remove ;
character after ()
in line 10, 12, 13. The fixed code is:
<?php
if ( $instance=="1" ) {
if ( get_query_var( "paged" ) ) { $paged = get_query_var( "paged" ); }
elseif ( get_query_var( "page" ) ) { $paged = get_query_var( "page" ); }
else { $paged = 1; }
$the_query = new WP_Query("cat=20&posts_per_page=5&paged=" . $paged);
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div class="post_square">
<a href="'. the_permalink() .'" class="post_image" style="background-image: url('. the_post_thumbnail_url() .');"></a>
<div class="post_text">
<div class="post_title"><a href="'. the_permalink() .'">'. the_title() .'</a></div>
<div class="post_meta">By '. $author = the_author_meta("display_name") .' / '. echo get_the_date("F j, Y") .'</div>
</div>
</div>';
endwhile;
next_posts_link( "Older Entries", $the_query->max_num_pages = 3 );
previous_posts_link( "Newer Entries" );
wp_reset_postdata();
else:
_e( "Sorry, no posts matched your criteria" );
endif;
}
if ( $instance=="2" ) {
echo 'this is two';
}
?>
Don't use an echo-ed functions of WordPress inside your echo. You can read more about the different between the_permalink
and get_the_permalink
here. So the better, the working code is:
<?php
if ( $instance=="1" ) {
if ( get_query_var( "paged" ) ) { $paged = get_query_var( "paged" ); }
elseif ( get_query_var( "page" ) ) { $paged = get_query_var( "page" ); }
else { $paged = 1; }
$the_query = new WP_Query("cat=20&posts_per_page=5&paged=" . $paged);
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div class="post_square">
<a href="'. get_the_permalink() .'" class="post_image" style="background-image: url('. get_the_post_thumbnail_url() .');"></a>
<div class="post_text">
<div class="post_title"><a href="'. get_the_permalink() .'">'. the_title() .'</a></div>
<div class="post_meta">By '. $author = get_the_author_meta("display_name") .' / '. get_the_date("F j, Y") .'</div>
</div>
</div>';
endwhile;
next_posts_link( "Older Entries", $the_query->max_num_pages = 3 );
previous_posts_link( "Newer Entries" );
wp_reset_postdata();
else:
_e( "Sorry, no posts matched your criteria" );
endif;
}
if ( $instance=="2" ) {
echo 'this is two';
}
?>
Upvotes: 2
Reputation: 20016
Here's what you want:
echo '<div class="post_square">
<a href="'. the_permalink() .'" class="post_image" style="background-image: url('. the_post_thumbnail_url() .');"></a>
<div class="post_text">
<div class="post_title"><a href="'. the_permalink() .'">'. the_title() .'</a></div>
<div class="post_meta">By '. $author = the_author_meta("display_name") .' / '. get_the_date("F j, Y") .'</div>
</div>
</div>';
Upvotes: 1