• Resolved Connor Crosby

    (@ccmovies)


    I’m not sure if this is the best way to do this. Essentially I have two services I offer which I’ve made as two custom post types. The “archive” page (which I’ve just created as a regular Page in WordPress) is identical except for a few things. I figured rather than have two separate Pages with the same code I would create a Template page that has the same code. The problem is I have a WP_query that needs different post_type names.

    So I figured the best way to do this was create an if statement depending on which service it is and have variables for each service. But it doesn’t seem to be working. This is what I have:

    <?php	
    $post_type = 'video';
    $work_link = the_field('video_link');
    $thumb = the_post_thumbnail( 'video-thumb' ); ?>

    And:

    <?php
    $query = new WP_Query( array(
    'post_type' => ' . $post_type . ',
    'posts_per_page' => -1
    ) ); ?>

    Any help would be appreciated. I’m not an expert when it comes to PHP so please spell it out for me.

    Bonus if anyone knows how I can return the $thumb variable (and allow WordPress to execute the PHP code). Is it as simple as <?php return $thumb; ?>. Again, no PHP expert.

    Thanks!

    • This topic was modified 7 years, 8 months ago by Connor Crosby.
Viewing 12 replies - 1 through 12 (of 12 total)
  • Have you tried

    <?php
    $query = new WP_Query( array(
    'post_type' => $post_type,
    'posts_per_page' => -1
    ) ); ?>
    Thread Starter Connor Crosby

    (@ccmovies)

    Doesn’t work. Probably because it has to be in quotes.

    Edit: It does work! I had mistyped the post_type – it is videos not video.

    • This reply was modified 7 years, 8 months ago by Connor Crosby.

    No idea then, but I think your code concatenation was wrong, at least in Genay doesn’t recognize de variable

    $query = new WP_Query( array(
    'post_type' => '' . $post_type . '',
    'posts_per_page' => -1
    ) ); ?>

    If you want to se my code where I did something similar:

     $argsc      = array(
                    'post_type' => 'felicette_pl_type',
                    'posts_per_page' => 12,
                    'paged' => $paged,
                    'tag' => array($tagargfelic)
                   
                );
               
                $your_query = new WP_Query($argsc);
                while ($your_query->have_posts()):
                    $your_query->the_post();
    			 endwhile

    Why not use the built-in archive for the post type and just create post type archive templates? Then you wouldn’t have to mess around with custom queries. Unless you have the need for using pages, like the need for editor content, I would recommend that approach.

    As for your current approach, it’s hard to debug with partial code and no explanation of what isn’t working. And I really can’t figure out what you are asking with the $thumb var, maybe explain what you are trying to do there.

    That being said, your logic with the queries seems to be on the right track. How are you controlling the variable value between pages? If you post the entire archive template this might be easier to assist you with.

    Thread Starter Connor Crosby

    (@ccmovies)

    @burria In case you didn’t see my edit I fixed the issue.

    @csloisel yes, exactly, I need the editor content to display other information – otherwise I would just use an archive.

    Basically, depending on which page is showing, I would like the variable to change. So if the videos page is showing then $thumb should show <?php the_post_thumbnail( 'video-thumb' ); ?> However now that I write this I realize I can just do something like <?php the_post_thumbnail( $thumb ); ?> and have the variable = video-thumb. Thoughts?

    Yes I would just switch the thumb variable value the same way you are switching the post type variable.

    • This reply was modified 7 years, 8 months ago by csloisel.
    Thread Starter Connor Crosby

    (@ccmovies)

    For $work_link I need it to be the_field('video_link'); and not just video_link – how do I do this? $work_link = the_field('video_link'); and <?php $work_link ?> doesn’t seem to be working.

    If you are trying to print out the variable you need to tell it to do so. Try <?php echo esc_url( $work_link ); ?>

    • This reply was modified 7 years, 8 months ago by csloisel.
    • This reply was modified 7 years, 8 months ago by csloisel.

    Try <?php echo esc_url( $work_link ); ?>

    Thread Starter Connor Crosby

    (@ccmovies)

    Doesn’t seem to work. Keep in mind that $work_link is a custom field (which links to a YouTube link).

    Basically I’m looking for a piece of code that let’s me make an variable = PHP code so that when I call $work_link it will execute the PHP code the_field('video_link'); and grab the custom field from the post rather than just echo the_field('video_link');. Make sense?

    Thread Starter Connor Crosby

    (@ccmovies)

    I ended up finding an alternate route. Thanks though for your help!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Use $variable to fill in wp_query post_type’ is closed to new replies.