Forums

[resolved] dyanmic landing page template based on custom field (16 posts)

  1. BillHector
    Member
    Posted 3 weeks ago #

    Okay, I've read a lot of posts about using custom fields that gets close to this, but not quite. Unless I missed something ...

    I want to have a custom field with the key "landingtag". The value could be anything, but will always correspond to a post tag (not sure if that makes a difference).

    Then in my new page template, below the content output for that page, I want to list related posts that belong to that page based on the key value defined from "landingtag".

    Bottom line, by simply using this new page template and defining "landingtag", a query will pull all the posts from the variable that is landingpage value. That's the key, in the query I need the posts to get to be a variable, thus making the whole process dynamic. One template, many possible landing pages.

    Thanks for any help or direction.

    Bill

  2. pboosten
    Member
    Posted 3 weeks ago #

    This article describes exactly what you want, I think, but then with the use of tags, not custom fields.

    Peter

  3. MichaelH
    moderator
    Posted 3 weeks ago #

    Maybe something like this:

    <?php
    $landingtag = get_post_meta($post->ID, 'landingtag', true);
    if ($landingtag){
    //do query with tag__in = $landingtag
    }
    ?>

    Related;
    Tag parameters

  4. BillHector
    Member
    Posted 3 weeks ago #

    Thanks, Peter, but what I really want is not quite related posts ... I want related posts based on another tag that is defined (by the custom field), not related to the post (which there is none, because I'm working with a page). BUT! this is a great reference for me and I'm sure I'll use it again.

    Michael, I'll try this out in a few minutes ... more later with results.

    thanks.

  5. BillHector
    Member
    Posted 3 weeks ago #

    Okay, the above solution didn't work .. this is the code I used:

    <?php
    $landingtag = get_post_meta($post->ID, 'landingtag', true);
    if ($landingtag){
    query_posts('tag__in=$landingtag');
    }
    
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    		<div class="entry">
            <?php the_excerpt(); ?>
    	</div><!--CLOSE entry -->
    	<?php endwhile; ?>
    		<?php else : ?>
    			<h3>Not Found</h3>
    <p>Sorry, but you are looking for something that isn't here.</p>
    		<?php endif; wp_reset_query(); ?>

    on the query, I tried both '' and "" because that variable is being evaluated. Any ideas?

    Again, I am trying to find all posts that have the custom field named "landingtag", with the value defined in that page, so that all posts that have "landingtag" and value defined as "group of posts" output.

    does this make sense?

    thanks for any help.

  6. MichaelH
    moderator
    Posted 3 weeks ago #

    Please paste all the code from the theme template file (your page template) that displays the page and contains the code above, into a pastebin such as wordpress.pastebin.ca, and report the link back here. Maybe someone can spot your problem. Thanks.

  7. BillHector
    Member
    Posted 2 weeks ago #

    pretty cool ... Never used a pastebin before ...

    People can find full code for the above page here:
    http://wordpress.pastebin.ca/1653215

    again, thanks for any help or direction people can provide.

  8. MichaelH
    moderator
    Posted 2 weeks ago #

  9. BillHector
    Member
    Posted 2 weeks ago #

    Okay ... no errors, but I am also not getting any posts showing up ... (http://www.cchange.net/?page_id=1292) I am going to take a wild guess and say it's because the posts I'm looking to query will have more than just the 'landingtag' ... meaning, there won't ever be a post that is true, give me all posts that have just and only 'landingtag'.

    could that be it?

    thank you.

  10. MichaelH
    moderator
    Posted 2 weeks ago #

    Oops, did you fix this line?

    $landingtag = get_post_meta($post->ID, 'cf1', true);

    to be

    $landingtag = get_post_meta($post->ID, 'landingtag', true);

    Having more that one custom field on that Page shouldn't be the problem nor should having multiple tags on the posts.

  11. BillHector
    Member
    Posted 2 weeks ago #

    well, the query is working now, but not returning any results ... I have triple checked everything: spelling of the custom field and tag on both the post and page, which seem to be the only non-code issues possible.

    right now this query with the above page linked should be returning one post:
    http://www.cchange.net/2009/10/28/back-to-the-future-pasture-local-wheat-and-water-power/

    but nothing is coming up ...

    hummm ...

  12. MichaelH
    moderator
    Posted 2 weeks ago #

    I add that code to the page.php template, created a page with a custom field. I assigned tags, equal to the custom field value I assigned that page, to several posts, and the page displays along with the 'related by custom filed/tag' posts.

    Using the WordPress Default theme, here's the page.php template:

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    
    get_header(); ?>
    
    	<div id="content" class="narrowcolumn" role="main">
           <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div class="post" id="post-<?php the_ID(); ?>">
    		<h2><?php the_title(); ?></h2>
    			<div class="entry">
    				<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    
    				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
    			</div>
    		</div>
    		<?php endwhile; endif; ?>
    	<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    	</div>
    
    <?php
    $landingtag = get_post_meta($post->ID, 'cf1', true);
    if ($landingtag){
        $args = array(
        'tag' => $landingtag,
        'showposts' => -1,
    	  'caller_get_posts' => 1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_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>
            <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?> </small><?php
            the_content();
          endwhile;
        }
    }
    ?>
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>
  13. BillHector
    Member
    Posted 2 weeks ago #

    I figured out the problem!!

    duh, I had to give the tag slug, not the tag itself, in the custom field of the page.

    Hero work, Michael! Thank you.

  14. katmassive
    Member
    Posted 1 week ago #

    Thanks guys this helped me alot after hours of tryna get it done myself.

  15. katmassive
    Member
    Posted 1 week ago #

    k one minor hiccup how do i get it to show the loop a minimum of 4 times...

    thanks in advance.

  16. katmassive
    Member
    Posted 1 week ago #

    Never mind used this instead...
    http://wordpress.org/support/topic/306790

    Just in case anyones tryna do the same thing ...

Reply

You must log in to post.

About this Topic