Expiring date for content wordpress - wordpress

http://wordpress.org/extend/plugins/simple-timed-plugin/
Im using this function to dissapear(not delete) expired posts:
<?php if (function_exists('simple_timed_content')) : ?>
<?php if (simple_timed_content("offdate=20120309 offtime=2350")) : ?>
Some content goes here
<?php endif; ?> <?php endif; ?>
I want to use as dates from custom fields like this:
<?php if (simple_timed_content("offdate='$end_date' offtime='$end_time'")) : ?>
where : `
$end_date=date( 'Ymd', strtotime( get_post_meta( $Post->ID, "_EventEndDate", true), time()));`
and
$end_time=date( 'HM', strtotime( get_post_meta( $Post->ID, "_EventEndDate", true), time()));`
Assuming allways that _EventEndDate value is : Ymd HM (20120309 2350)
How would be the complete code for this to work?
My other option is to use this code i found :
www.rockia.com/2010/01/modify-you-wordpress-theme-to-enable-an-expiration-for-your-posting
but it doesnt seem to work with my
www.wordpress.org/extend/themes/bombax
where loop is in single.php file
If someone could help I would appreciate it. Im new to php.
Thank you in advance

I have different way to accomplish this try below
Edit your theme and replace your current WordPress loop by this "hacked" loop:
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
$disappeartime = get_post_custom_values('disappear');
if (is_array($disappeartime)) {
$disappearstring = implode($disappeartime);
}
$secondsbetween = strtotime($disappearstring)-time();
if ( $secondsbetween > 0 ) {
// For exemple...
the_title();
the_excerpt();
}
endwhile;
endif;
?>
To create a post with date/time disappear, just create a custom field. Give it disappear as a key and your date/time (format: mm/dd/yyyy 00:00:00) as a value.
The post will not show after that time stamp.

Related

ACF fields on WooCommerce Product not displaying

I am customizing an old theme called AmazeTheme which was not specifically made with WooCommerce support. And an woocommerce.php was at theme's root. However with help of plugins like Simply Show Hooks and Show current template I am able to see where to put which info.
But I am having an weird problem. The ACF fields I have added are not visible at all. I even tried to assign the field group inside product loop section.
I also tried the following method.
add_action( 'woocommerce_after_single_product_summary', 'view_acf_field_for_single_product', 10 );
function view_acf_field_for_single_product(){
if (function_exists('the_field')){
the_field('shop_support');
}
}
And inside the loop of single-product.php
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'single-product' );
$dump = get_fields();
?>
<?php endwhile; // end of the loop. ?>
And then did var_dump($dump) at desired place of the file.
This site's php version is 5.6.40 and WooCommerce version is 3.4.8
WP version is 4.9.18
I have looked up many solutions. Also tried the WooCommerce Hooks but still no clue why ACF not showing.
Can you try this code:
add_action( 'woocommerce_after_single_product_summary', 'view_acf_field_for_single_product', 10 );
function view_acf_field_for_single_product(){
global $post;
if (function_exists('the_field')){
the_field('shop_support', $post->ID);
}
}
Not tested.
For some weird reasons, this approach worked for me (path/to/theme/woocommerce/single-product.php):
<?php if (get_field_objects()):
foreach ( get_field_objects() as $field_id => $field ) :
$value = trim($field['value']);
if (!empty($value)) :
?>
<div class="product_field" id="field-<?php echo $field_id; ?>">
<?php the_field($field_id); ?>
</div>
<?php endif; ?>
<?php endforeach; // end of the loop. ?>
<?php endif; ?>
Also the following one for Category (path/to/theme/woocommerce/archive-product.php) pages:
<?php
$extra_info = get_field_object( 'category_details', get_queried_object() );
if ( !empty($extra_info) ) :
?>
<div class="category_field" id="field-<?php echo $extra_info['key']; ?>">
<?php echo $extra_info['value']; ?>
</div>
<?php endif; ?>
Even at these locations, get_fields() did not work.

wordpress, get second newest post

I need to get second newest post from wordpress, and I cant do this. I found I lot of tutorials how to get several newest posts, but no how to get only one post that is second in the order (or fifth, or tenth).
I have something like this:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if(get_posts()[1] == true) : ?>
<?php get_template_part( 'content-second-row', ( post_type_supports( get_post_type(), 'post-formats' ) ? get_post_format() : get_post_type() ) ); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
But, of course it is not working. I tried to get get_post().length. I know, unfortunately I can't use posts id, because there is no one by one.
Please, help me.
Of course, I try to finde something in wordpress codex
try this
get_posts( array('posts_per_page' => 5,'offset' => 1,))
You can change offset to any number you want.

Wordpress: How to sort posts on a custom field

I want to display a list of post titles from a custom taxonomy in a particular order.
I thought the best way to control the order would be to add a custom field and sort on that custom field.
The problem I'm having is that I'm trying to use the built-in functionality of Wordpress and I can't find a way to add sort functionality.
My Scenario looks like this
The calling url is ...com/taxonomy/term
This calls up a template, the filename of which is taxonomy-taxonomyname-term.php
My template is simply the index.php template renamed and edited to contain this loop
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
This displays a list of titles but I can't find a way to control the order of the titles.
The only way I've seen to set the order of a group of posts is to define the order of the posts in the query. But of course in this case I dont have a query because I already have the posts via the calling url.
Is there any way to add sort functionality without adding another query or is the query mandatory.
Suppose your custom fields is my_date
You can create custom query like this.
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
To use it
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
For more info http://wpengineer.com/1915/sort-posts-custom-fields/

Wordpress loop > unique loop renders slightly wrong results

A few things to understand before my question will make sense:
I use a hidden category called 'Unique' to specify if the post will use the single.php or a special one used for the unique ones.
I want the index to act as a single: showing only one post, displaying next/prev post links, and comments also.
I need the index.php to say if the post is in category 15 (unique) than <the_unique_content>, else; <the_default_content>
My loop does all this, but the problem is that if the current post is unique, it also displays 1 additional post below the unique post.
Here is the loop >
<?php $wp_query->is_single = true; ?>
<?php $post_count = 0; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if ($post_count == 0) : ?>
<?php if (in_category('15')) { ?>
<?php the_content(); ?>
<?php } else { ?>
<?php the_content(); ?>
<?php $post_count++; ?>
Thanks for any help!
I don't think you are setting the query correctly to return a single post. Your code is limiting the number of posts through your $post_count variable, but in the case where the post is "unique", it only increments to 1 on the second post.
Here is one way to limit the number of posts to a single post. It involves modifying the loop query to set the number of posts per page to one.
<?php
global $wp_query;
$new_query = array_merge( array( 'posts_per_page' => 1 ), $wp_query->query );
query_posts( $new_query );
if (have_posts()) : while (have_posts()) : the_post(); ?>
etc...

How to Sort Wordpress Posts Horizontally, Calling by Category

I am using the following code to try and display posts from only a certain category horizontally in three rows. I have the horizontal display issue figured out (using css) but with the following code it displays all posts and not posts from specific category.
<?php query_posts('showposts=5'); ?>
<?php query_posts('cat=7'); ?>
<?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
Any help would be greatly appreciated.
You're misunderstanding some concepts in query_posts and get_posts.
query_posts is to be used inside the loop. get_pages isn't. If you want to use query_posts, you don't need to create the get_pages call. Use query_posts or get_pages to accomplish what you're trying to do.
You need to combine your category parameters in query_posts.
<?php
query_posts('showposts=5&cat=7');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
?>
If you want to do the same logic but without The Loop, just call
$posts = get_posts('numberposts=5&offset=0&category=7').
Read the links I provided. They have all information you need to understand how to do what you need.

Resources