• Resolved Bill Weye

    (@billhector)


    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

Viewing 15 replies - 1 through 15 (of 15 total)
  • This article describes exactly what you want, I think, but then with the use of tags, not custom fields.

    Peter

    Maybe something like this:

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

    Related;
    Tag parameters

    Thread Starter Bill Weye

    (@billhector)

    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.

    Thread Starter Bill Weye

    (@billhector)

    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.

    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.

    Thread Starter Bill Weye

    (@billhector)

    pretty cool … Never used a pastebin before …

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

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

    Thread Starter Bill Weye

    (@billhector)

    Okay … no errors, but I am also not getting any posts showing up … (https://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.

    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.

    Thread Starter Bill Weye

    (@billhector)

    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:
    https://www.cchange.net/2009/10/28/back-to-the-future-pasture-local-wheat-and-water-power/

    but nothing is coming up …

    hummm …

    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(); ?>
    Thread Starter Bill Weye

    (@billhector)

    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.

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

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

    thanks in advance.

    Never mind used this instead…
    https://www.ads-software.com/support/topic/306790

    Just in case anyones tryna do the same thing …

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘dyanmic landing page template based on custom field’ is closed to new replies.