put calculated value in byline
-
Mike
Next challenge for me … I am trying to do is to put a calculated value in the byline of tiles (using the tile designer byline content). The calculation is age and I have defined a function to work out the age from a meta date and the current date.
I don’t know if this is possible …
or is is possible to calculate it for each post returned within the php and then output it to the tiles maybe storing it in the interim within the post meta data?
-
Hi @carosea,
WP Tiles has a filter for adding custom byline tags:
wp_tiles_byline_tags. You could use that to add a filter to display on the byline?For example:
add_filter( 'wp_tiles_byline_tags', function( $tags, $post ) { $tags['age'] = my_age_function( $post ); return $tags; }, 10, 2 );Then, in your byline template you can display `%age%!
Cheers,
MikeMike
Firstly I would like to thank you for all the time you spend answering queations and supporting people like me! I do still need a bit more help though please.I have set up the query to select those posts where the meta key birthday value is current day and call the-wp-tiles to display a grid of those with a birthday today. (code in my customised content.php is here).
`<?php $query = new WP_Query( array(
‘meta_key’ => ‘birthday’,
‘meta_value’ => date( ‘F d’ )
));
$opts = array(
‘byline_template’ => “%title%”,
‘byline_template_textonly’ => false,
‘byline_opacity’ => ‘0.6’,
‘byline_color’ => ‘#000000’,
‘byline_height’ => 30,
‘byline_height_auto’ => false,
‘text_color’ => false,
‘image_text_color’ => ‘#FFFF99’,
‘ image_size’ => ‘original’,
‘post_type’=>’post’,
‘orderby’=>’title’,
‘order’=>’ASC’,
‘posts_per_page’=>’auto’,
‘ pagination’ => none,
‘category’=>’join’,
‘extra_classes’=> array(my-custom-styled-tiles),
‘post_status’=>’publish’); ?>
<?php the_content(); ?>
</div><!– .entry-content –>
<?php the_wp_tiles( $query, $opts); ?>
<?php wp_reset_postdata(); ?>`On this particular page where I use the-wp-tiles query I am only selecting those whose birthday it is and would like to say on these bylines how old they are (or even just in a caption to go under the tile??). I don’t want it on any other page that the same tiles appear in various groupings but will just print the age variable on the text on some posts.
I store the birthdate as a post tag in the format ‘d-m-Y’ (eg 22-03-1996). I have tried to create a function in functions.php to calculate the age in years for example but seem to have problems getting it to read the tag ‘birthdate’ from the post as a variable:function get_age( $birthdate ) { list($day, $month, $year) = explode( "-", $birthdate); // Find the differences $y_diff = date( "Y" ) - $year; $m_diff = date( "m" ) - $month; $d_diff = date( "d" ) - $day; // check if another year has been reached if ( $d_diff < 0 || $m_diff < 0 ) $y_diff --; return $y_diff;So firstly this function code to calculate the age isn’t working (something wrong with my code) it won’t seem to pick up my tag field variable $birthdate.
Secondly when I manage to get this bit of coding correct I need to pass this info to the-wp-tiles query so that for each post it returns (whose birthday it is today) it also returns the age and puts it onto the byline or as a tile caption.Would appreciate any help you can offer.
Hi caroseo!
So firstly this function code to calculate the age isn’t working (something wrong with my code) it won’t seem to pick up my tag field variable $birthdate.
If you use the
my_age_functionsnippet from my last post, you have to grab the tag using the$postpassed in the second param.Secondly when I manage to get this bit of coding correct I need to pass this info to the-wp-tiles query so that for each post it returns (whose birthday it is today) it also returns the age and puts it onto the byline or as a tile caption.
Again, if you’re using the custom byline tag snippet above, you can set the
byline_templateparameter in your options array to include%age%:<h1>Today I turn %age%</h1>Lastly: correct me if I’m wrong, but I think in your birthday fn you should check if the day diff AND the month diff are negative?
if ( $m_diff < 0 && $d_diff < 0 )Cheers,
MikeMike
I have returned to this – which I think is what you originally suggested – the wp byline template part is fine and prints an age correctly I just think my problem is still getting the variable $birthdate to be used correctly – it doesn’t like it – if I hard code $birthdate as YYYY-MM-DD into
list($year, $month, $day) = explode( "-", '1998-03-26');it works fine for that one example but I can’t get it to read the variable being passed through – or at least it maybe passing it but the strtotime function returns 0 (and therefore this example has an age of 2015) .. it is probably something fundamental but is my call function my_age_function ( $birthdate ) correct ?- if have a tag on the post eg 1998-03-26 ?function my_age_function( $birthdate ) { // Explode the date into meaningful variables list($year, $month, $day) = explode( "-", strtotime('$birthdate')); // Find the differences $y_diff = date( "Y" ) - $year; $m_diff = date( "m" ) - $month; $d_diff = date( "d" ) - $day; // If the day has not occured this year if ( $d_diff < 0 || $m_diff < 0 ) $y_diff --; return $y_diff; } add_filter( 'wp_tiles_byline_tags', function( $y_diff, $post ) { $y_diff['age'] = my_age_function( $post ); return $y_diff; }, 10, 2 );
The topic ‘put calculated value in byline’ is closed to new replies.