Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Having a site being able to reference itself with custom field WordPress

  • SOLVED

Hello,

I am attempting to add custom field to a post, this custom field is not used as on the post, but rather as an element within a summary page.

The use case is that is a the post is a recipe. The custom field is a highlight of the summary and has a button that links to the post.

At the moment, I am inserting the shortlink into the custom field but this is very cumbersome I was hoping there was an easier way, like a short code.

Here is an example of the custom field code:

<p>These lamb and veggie kabobs are delicious with the pairing of mint pesto sauce.</P
<ul>
<li>Rating: 4</>

<li>Yield: 16 servings</li>

<li>Prep Time: 10 minutes</li>

<li>Cook Time: 20 minutes</li>

<li>Total Time: 30 minutes</li>

[small_button text="Read More..." title="Cool Button" href="http://domain.com/?p=19373" align="right"]




Thoughts?

Answers (1)

2011-11-17

John Cotton answers:

It seems an odd approach to me. For a start, you'd have to know the post id to be able to retrieve the post meta, so why bother storing it in the custom field? You can just use the id you're using for look up to create a permalink and save have 10's, 100's or 1000's of copies of the same code in the database.

Secondly, you might not want to now, but later you'd might like to look up recipes by cook time or yeild or whatever.

So why not store those individual pieces of data in custom fields, retrieve those and make you HTML at run time. That has the distinct advantage of allowing you to modify your HTML at some later date without having to go into the database and change each old record!


Greg Ellett comments:

Hi John,

Great point. The issue really is around reference elements around the other pages the site that uses the custom fields rather than content or excerpts.

So that if you are looking at the collection of recipes, the template doesn't pull the content for each, just the custom fields.

I would love to know if there was a better way, but the documentation of the theme points as this is the only way.

I was really just hoping to reference the post its in a shortcode, but that is also a challenge.


John Cotton comments:

<blockquote>So that if you are looking at the collection of recipes, the template doesn't pull the content for each, just the custom fields. </blockquote>

Well that's fine (and anyway, you could always tweak the theme!).

Create a custom field for each of these values in the recipe post:

tip - eg These lamb and veggie kabobs are delicious with the pairing of mint pesto sauce.
rating
yield
prep-time
cook_time
(if Total Time is just cook + prep then you don't need this one, otherwise do that one too).

And your theme will pull back all those...

If it doesn't (ie you only have the option to pull back one custom field), just go in and change the code! Don't know how? Stick the code up and we can help you :)



Greg Ellett comments:

I can do that, but if i create recipe_url what is the value. I would assume the short link, and then I just run this from database script?


John Cotton comments:

Sorry, I don't know what you mean....


John Cotton comments:

Can you post the bit of the theme that's doing this so that I can see what's going on?


Greg Ellett comments:

Sure,

So the recipe is here

http://cblivedev4.com/articles/chef-erik%E2%80%99s-lamb-veggie-kabobs-with-mint-pesto

the summary page that uses the custom fields can be found here

http://cblivedev4.com/recipe-test/recipe-test-g



John Cotton comments:

Do you know where the PHP is that makes this page:

http://cblivedev4.com/recipe-test/recipe-test-g

?


Greg Ellett comments:

<?php
/**
* @package WordPress
* @subpackage U-Design
*/
/**
* Template Name: Page Template 4
*/

get_header();

$content_position = ( $udesign_options['pages_sidebar_4'] == 'left' ) ? 'grid_16 push_8' : 'grid_16';
?>

<div id="content-container" class="container_24">
<div id="main-content" class="<?php echo $content_position; ?>">
<div class="main-content-padding">

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="entry">
<?php the_content(__('<p class="serif">Read the rest of this page &raquo;</p>', 'udesign'));
wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
<?php ( $udesign_options['show_comments_on_pages'] == 'yes' ) ? comments_template() : '';
endwhile; endif; ?>
<div class="clear"></div>
<?php edit_post_link(esc_html__('Edit this entry.', 'udesign'), '<p class="editLink">', '</p>'); ?>
</div><!-- end main-content-padding -->
</div><!-- end main-content -->

<?php if( sidebar_exist('PagesSidebar4') ) { get_sidebar('PagesSidebar4'); } ?>

</div><!-- end content-container -->

<div class="clear"></div>

<?php


Greg Ellett comments:

sorry wrong template...


<?php
/**
* @package WordPress
* @subpackage U-Design
*/
/**
* Template Name: Portfolio page 4 Columns
*/

get_header();

global $post;
// get the page id outside the loop (check if WPML plugin is installed and use the WPML way of getting the page ID in the current language)
$page_id = ( function_exists('icl_object_id') && function_exists('icl_get_default_language') ) ? icl_object_id($post->ID, 'page', true, icl_get_default_language()) : $post->ID;
$portfolio_cat_ID = $udesign_options['portfolio_cat_for_page_'.$page_id]; // Get the portfolio category specified by the user in the 'U-Design Options' page
$current_categoryID = $_GET['cat'];
$categories = get_categories( 'child_of='.$portfolio_cat_ID );
$query_string_prefix = ( get_option('permalink_structure') != '' ) ? '?' : '&amp;';
if ( preg_match( '/\?/', get_permalink() ) ) $query_string_prefix = '&amp;';
$portfolio_items_per_page = $udesign_options['portfolio_items_per_page_for_page_'.$page_id];
$portfolio_do_not_link_adjacent_items = $udesign_options['portfolio_do_not_link_adjacent_items_'.$page_id];
$portfolio_title_posistion = $udesign_options['portfolio_title_posistion'];

?>

<div id="content-container" class="container_24">
<div id="main-content" class="grid_24">
<div class="main-content-padding">

<?php // BEGIN the actual page content here...
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post portfolio-page" id="post-<?php the_ID(); ?>">
<?php if ( get_the_content() ) : ?>
<div class="entry">
<?php the_content(__('<p class="serif">Read the rest of this page &raquo;</p>', 'udesign'));
wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
<?php endif; ?>
</div>
<?php endwhile; endif; ?>


<?php
// Check if a category has been assigned as Portfolio section
if( $portfolio_cat_ID ) :
if ( $categories ) : ?>
<div id="category-links" class="grid_22">
<ul>
<li><?php esc_html_e('Categories', 'udesign'); ?>: &nbsp;&nbsp;&nbsp;</li>
<?php // Generate the link to "All" categories:
if ( $current_categoryID ) : ?>
<li><a href="<?php echo the_permalink(); ?>"><?php esc_html_e('All', 'udesign'); ?></a></li>
<?php else : ?>
<li><a href="<?php echo the_permalink(); ?>" class="current"><?php esc_html_e('All', 'udesign'); ?></a></li>
<?php endif;
// Generate the link to the rest of categories:
foreach( $categories as $category ) :
if ( $current_categoryID == $category->cat_ID ) : ?>
<li><a href="<?php echo the_permalink().$query_string_prefix.'cat='.$category->cat_ID; ?>" class="current"><?php echo $category->cat_name; ?></a></li>
<?php else : ?>
<li><a href="<?php echo the_permalink().$query_string_prefix.'cat='.$category->cat_ID; ?>"><?php echo $category->cat_name; ?></a></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div><!-- end category-links -->
<?php endif;

if ( !$current_categoryID )
$current_categoryID = $portfolio_cat_ID;

//adhere to paging rules//adhere to paging rules
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // applies when this page template is used as a static homepage in WP3+
$paged = get_query_var('page');
} else {
$paged = 1;
}
// Switch the focus to the chosen portfolio category and its subcategories
query_posts( array(
'cat' => $current_categoryID,
'posts_per_page' => $portfolio_items_per_page,
'paged' => $paged
)
);

// start Portfolio items' loop ?>
<div class="clear"></div>
<div class="portfolio-items-wrapper">
<?php $counter = 0;
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div class="one_fourth<?php echo ( ++$counter == 4 )?' last_column':''; ?>">
<?php if( $portfolio_title_posistion == 'above' ): ?>
<h2><?php the_title(); ?></h2>
<?php endif; ?>
<div class="thumb-holder-4-col pngfix">
<div class="portfolio-img-thumb-4-col">
<?php // Get Portfolio Item Thumbnail
get_portfolio_item_thumbnail( '4', '176', '116', $portfolio_do_not_link_adjacent_items, true ); ?>
</div><!-- end portfolio-img-thumb-4-col -->
</div><!-- end thumb-holder-4-col -->

<?php $portfolio_item_description = get_post_meta($post->ID, 'portfolio_item_description', true);
if ( $portfolio_item_description ) :
if( $portfolio_title_posistion == 'below' ): ?>
<h2><?php the_title(); ?></h2>
<?php endif; ?>
<div class="clear"></div>
<?php echo do_shortcode( __($portfolio_item_description) ); ?>
<?php endif; ?>

</div><!-- end one_fourth -->
<?php if ( $counter == 4 ) : ?>
<div class='clear'> </div>
<?php $counter = 0;
endif;

endwhile; ?>

<div id="paginationPortfolio" class="grid_23">
<?php // Pagination
if(function_exists('wp_pagenavi')) :
wp_pagenavi();
else : ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link() ?></div>
<div class="alignright"><?php next_posts_link() ?></div>
</div>
<?php endif; ?>
</div>
<?php endif;
//Reset Query
wp_reset_query();
// end Portfolio items' loop ?>
</div><!-- end portfolio-items-wrapper -->
<?php
else : ?>
<div class="grid_22 prefix_1 suffix_1">
<h2><?php esc_html_e('Portfolio section for this page has not been found!', 'udesign'); ?></h2>
<p><?php _e("<strong>Reason:</strong> No category has been assigned as Portfolio section for this page yet. In order to fix this, go to the theme's options page and assign a category for this page.", 'udesign'); ?></p>
</div>
<?php
endif; ?>

<div class="clear"></div>
<?php edit_post_link(esc_html__('Edit this entry.', 'udesign'), '<p class="editLink">', '</p>'); ?>

</div><!-- end main-content-padding -->
</div><!-- end main-content -->
</div><!-- end content-container -->

<div class="clear"></div>

<?php

get_footer();


John Cotton comments:

So you want button at the bottom of the description?

After this line

<?php echo do_shortcode( __($portfolio_item_description) ); ?>

insert this line


<a class="pngfix small-dark-button align-btn-right" href="<?php the_permalink(); ?>" title="Cool Button"><span class="pngfix">Read More...</span></a>


John Cotton comments:

(...and then take the button code out of your description custom field...)


Greg Ellett comments:

Thanks!