• -I am trying to use 1 template (inner.php) for multiple Pages.
    -I would like each page to pull in information from multiple posts.
    -One thing I am including is ‘header image’ which is an image and caption.
    -I am using wP_Query to grab & displays posts with certain categories or tags

    I am wondering if I can use Conditional Statements within a query?

    Or, is there something I can add to the code below to have the results be based on the post tag matching up with the Page ID or Title?

    <div id="headerImage">
    			<?php
    	$headerImage = new WP_Query();
    	$headerImage->query('category_name=headerImage&showposts=1');
    	while($headerImage->have_posts()) : $headerImage->the_post();
    ?>
    
                <div class="post" id="post-<?php the_ID(); ?>" style="background:white;">
    
                <div class="entry">
                    <?php the_content(''); ?>
    
            	</div>
    
    		<?php endwhile; ?>
    	</div>

    Something like this…
    $headerImage->query('category_name=headerImage&tag=('the_ID()')showposts=1');

Viewing 10 replies - 1 through 10 (of 10 total)
  • Something like this in a Page Template:

    <?php
    if (is_page()) {
    $cat_id = get_cat_ID('headerImage');
    $cat_id = get_cat_ID('cat1');
    $tag_name = $posts[0]->ID; //use page id for tag name
    $tag = get_term_by('name', $tag_name, 'post_tag');
    $args=array(
      'cat' => $cat_id,
      'tag__in'=> array($tag->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>

    Thread Starter trulyandy

    (@trulyandy)

    Thanks, MichaelH.

    I played with the code you provided a bit and got it to work to a certain degree.

    The ‘headerImage’ category posts which all contain 1 image and a caption, are getting displayed in the proper place. However, the tag aspect of it appears to be ignored all together — with the template displaying the most recent post in the ‘headerImage’ category instead of the most recent post tagged with the page ID of the page you are on.

    Here is the code I am working with

    <?php
    if (is_page()) {
    $cat_id = get_cat_ID('headerImage');
    $tag_name = $posts[0]->ID; //use page id for tag name
    $tag = get_term_by('name', $tag_name, 'post_tag');
    $args=array(
      'cat' => $cat_id,
      'tag_in'=> array($tag->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
       <?php the_content(''); ?>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>

    Thread Starter trulyandy

    (@trulyandy)

    Hmmm… Very close – any ideas what I am missing here?

    after this line

    $tag = get_term_by('name', $tag_name, 'post_tag');

    put

    echo "<pre>"; print_r($tag); echo "</pre>";

    to make sure you are getting the expected tag

    Thread Starter trulyandy

    (@trulyandy)

    Hi MichaelH.
    Thanks for the reply. I tried that out, and the <pre></pre> tags come up empty, with no tag or any other information inside of them.

    So I imagine that means it is not getting the tag properly at all.

    Any ideas?

    One question for you, is in the code where you put `$tag_name = $posts[0]->ID; //use page id for tag name
    ` are you saying I should change that line manually, or is the comment saying that I should tag the post with the Page ID that I want it to appear on? I assumed the latter, but want to verify.

    Other than that, what do you think? Appreciate the help!

    One question for you, is in the code where you put `$tag_name = $posts[0]->ID; //use page id for tag name

    You said you were using a template “inner.php” for multiple pages so I took that to mean you wrote a page, then assigned inner.php as your Page Template. And in that case $posts[0]->ID would hold the query page’s ID. And we’re having “results be based on the post tag matching up with the Page ID “, correct?

    Thread Starter trulyandy

    (@trulyandy)

    Hi MichaelH,
    Yes, that is all correct.

    I created multiple Pages, and assigned them all the inner.php template.

    I am hoping the header will display the correct image based on the Page ID matching the Tag I added to the ‘header image Post’

    thank you so much for walking me through this!

    -Andy

    If you are doing something to your header then you’d likely put code in your header.php rather than the Page Template.

    Review:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

    Thread Starter trulyandy

    (@trulyandy)

    Hi MichaelH –
    Sorry to be confusing – I am referring to it as a ‘header’image – as it will be at the top of the main content area on each page. However, it is not in the header part of the webpage. I suppose I should have chosen a different word to refer to it as. Any suggestions?

    So the previous issue remains….the code does not appear to be picking up the tag correctly.

    I feel like we were very close and appreciate all of the help thus far. Any continued tips or suggestions would be great!

    -Andy

    Thread Starter trulyandy

    (@trulyandy)

    Hey Michael — any thoughts?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘WP_Query & Conditional Statements OR how to pull in a post based on the page id’ is closed to new replies.