0

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:

enter image description here

I'm at a loss. I've tried numerous ways to do this, but it's beyond me...

4
  • Can you be more specific than "not working?" What exactly happens? Commented Jan 11, 2018 at 18:14
  • You can't have a variable assignment as part of an echo. At a minimum you'll need to remove $author = from your echo inside the while loop and just leave the WP author_meta call. Commented Jan 11, 2018 at 18:25
  • @ScottCWilson I added a screenshot of the warnings I get in DW - unexpected '.' Commented Jan 12, 2018 at 15:04
  • 1
    alse, those ; shouldn't be in that echo. There's just a lot of minor mistakes in there Commented Jan 12, 2018 at 15:06

2 Answers 2

2

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

Update

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';
    }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! This works and is pulling in the posts' info, but for some reason it completely ignores the html structure of where I want the info to show up. It's giving me the post URLs, author meta, image URL, but lists them as one continuous string, instead of placing them where I did in the "echo" structure
@Manly your problem is the_permalink() is echo a string, so you are echo a echo. The simple fix is change the_permalink() to get_the_permalink(), the_post_thumbnail_url() to get_ the_post_thumbnail_url(). Change echo get_the_date("F j, Y") to get_the_date("F j, Y") too.
You are a lifesaver, thank you! I wish I could upvote this again :)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.