Showing only part of the custom meta in wordpress - php

I have a wordpress code that displays a check list for each wordpress user. The code works fine and I am able to get it to display on a page.
<?php
$args = array( 'post_type' => 'todo_listing', 'posts_per_page' => 3,'order'=>'ASC' );
$loop = new WP_Query( $args );
$_meta_val_arr=array(10=>"All Item",0=>"Cat0",
1=> "cat1",
2=>"cat2",
);
?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li class="todo" id="<?php echo get_the_ID();?>" itemage="<?php echo get_post_meta(get_the_ID(),'_todotime',true)?>"><a href="javascript:;"
<?php if($all_meta_for_user[get_the_ID()][0]){
?>
class="strike"
<?php
}
?>
>
<?php if($all_meta_for_user[get_the_ID()][0]){?>
<span class="check_box cb"></span>
<?php }else{?>
<span class="uncheck_box cb"></span>
<?php }?>
<p><?php the_title(); ?></p></a>
<?php
endwhile;
?>
I just want to show those items with with Class ="strike" which is saved as meta [1012] => Array ( [0] => 1 ) where 1012 is the todo id.

Related

the_field - ACF not working in 'while' loop

Main string of code (that doesn't work):
<span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>
<?php
$args = array(
'post_type' => 'tariffs',
'posts_per_page' => 3,
'type_of_site' => 'landing_page_type',
);
$webTariffs = new WP_Query($args);
while ($webTariffs->have_posts()) {
$webTariffs->the_post();?>
<div class="pricing-item">
<div class="pricing-item-header">
<h3><?php the_title() ?></h3>
<span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>
</div>
</div>
<?php } ?>
The ways i tried to solve this problem:
put insted of the_field('tariff_price_kn') - echo('hi') - the code worked
add $post_id -> the_field('tariff_price_kn', $post_id)
$currencyKune = get_field('tariff_price_kn'); And then echo $currencyKune
P.S 3) i don't know exactly where should i put $currencyKune = get_field('tariff_price_kn'), so i put it before while at first time - doesn't work and at the second time i put it after $webTariffs->the_post();
Try this :
<?php
$args = array(
'post_type' => 'tariffs',
'posts_per_page' => 3
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-sm-6">
<h2 class="the-title"><?php the_field('tariff_price_kn', $post->ID); ?> + <?php the_title() ;?> </h2>
</div>
<?php endwhile; else: ?> Nothing here <?php endif; ?>
<?php wp_reset_query(); ?>

WordPress: Count child pages

On a page in WordPress, I want to display all childrens of that page. This works at the moment like this:
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<div id="parent-<?php the_ID(); ?>" class="parent-page">
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; ?>
What I want is that the loop counts my posts and prints the number in the div, starting with 1. For example:
<div class="child1">
Title of first child
</div>
<div class="child2">
Title of second child
</div>
<div class="child3">
Title of third child
</div>
What are your suggestions?
Just create a $count variable and increment it each time through the loop.
<?php $count = 1; ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<div id="parent-<?php the_ID(); ?>" class="parent-page child<?php echo $count++; ?>">
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; ?>

Get categories for a single post in a custom post type

I'm trying to get an unformatted list (preferably a list of slugs) of the categories for a single post in a custom post type loop. This list will eventually serve as a class for a div ($CATEGORYSLUGSWILLEVENTUALLYGOHERE).
I've found a few different methods of getting a list of ALL of the categories for a custom post type, but not the ones specific to a single one. Here's what I have so far:
<?php $projects_loop = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'menu_order' ) ); ?>
<?php while ( $projects_loop->have_posts() ) : $projects_loop->the_post(); ?>
<div class="box <?php $CATEGORYSLUGSWILLEVENTUALLYGOHERE; ?>">
<div class="port-item-home">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endwhile; ?>
And here's what I've tried so far to get the category list:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'taxonomy' => 'project-type'
);
$categories = get_categories( $args );
echo '<p> '.print_r(array_values($categories)).'something</p>'
?>
I have it returning the array - but the array is showing that it'll display all categories instead of the ones pertaining to that specific post.
I also tried:
<?php
//list terms in a given taxonomy (useful as a widget for twentyten)
$taxonomy = 'project-type';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo $tax_term->name;
}
?>
And that also displays all categories instead of the ones pertaining to the post.
Any suggestions??
Got it! Found this article that helped me out:
https://wordpress.org/support/topic/how-to-get-the-category-name-for-a-custom-post-type
<!-- The Query -->
<?php
$args = array(
'post_type' => 'my_post_type',
'posts_per_page' => -1,
'orderby' => 'menu_order' );
$custom_query = new WP_Query( $args );
?>
<!-- The Loop -->
<?php
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
$terms_slugs_string = '';
$terms = get_the_terms( $post->ID, 'my_post_type' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term_slugs_array = array();
foreach ( $terms as $term ) {
$term_slugs_array[] = $term->slug;
}
$terms_slugs_string = join( " ", $term_slugs_array );
}
?>
<div class="box<?php echo $terms_slugs_string ?>">
<div class="port-item-home">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
</a>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</div>
<?php endwhile; ?>

Flex Slider Integration with WordPress

[SOLVED]
I'm trying to integrate Flex Slider (flexslider.woothemes.com) with WordPress. I've included all JS/CSS files needed.
Then, I added this code to show the featured images of a certain category - the famous WP_Query with $args set to a certain category. Then using echo the_post_thumbnail(); I'm showing the posts' images. It's working fine. I just need to make the images linked to post URLs (imgs are hrefed). Please provide help and thanks in advance.
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'one',
'posts_per_page' => 5
);
// The Query
$the_query = new WP_Query( $args );
// Check if the Query returns any posts
if ( $the_query->have_posts() ) {
// Start the Slider ?>
<div class="flexslider">
<ul class="slides">
<?php
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php }
// The Slide's Image
echo the_post_thumbnail();
</li>
<?php endwhile; ?>
</ul><!-- .slides -->
</div><!-- .flexslider -->
<?php
// Reset Post Data
wp_reset_postdata();
?>
New code that's working:
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'one',
'posts_per_page' => 5
);
// The Query
$the_query = new WP_Query( $args );
// Check if the Query returns any posts
if ( $the_query->have_posts() ) {
// Start the Slider
?>
<div class="flexslider">
<ul class="slides">
<?php
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php echo the_post_thumbnail(); ?>
</a>
</li>
<?php endwhile; ?>
</ul><!-- .slides -->
</div><!-- .flexslider -->
<?php }
// Reset Post Data
wp_reset_postdata();
?>
If I understand what you're asking, I think the answer is
echo '<a href="'. the_permalink() .'">';
echo the_post_thumbnail();
echo '</a>';

Display div only if exists other posts with same custom field value

I've got a wordpress theme with a set of custom fields.
One of these is named "author".
On single.php I've got a div which show the other posts with the same custom field value.
I would like to display this div only if exists other posts with the same custom field value, else I would like to display nothing.
Thanks for your help!!
This is my actual code:
<?php
$myquery = array(
'meta_key' => 'autore',
'meta_value' => $autore,
'showposts' => 2,
'post__not_in' => array($post->ID)
);
if ( $myquery->have_posts() ) : ?>
<div class="related">
<h3>Altre di <?php the_field('autore'); ?></h3>
<ul>
<?php while ( $your_query->have_posts() ) : $your_query->the_post(); ?>
<?php
echo '<li>'; ?>
<?php
$fotorel = get_field('foto_homepage');
list($width, $height) = getimagesize("$fotorel");
$relheight = $height / 2;
?>
<div class="related-foto" style="background:url(<?php the_field('foto_homepage'); ?>) no-repeat center center; height:<?php echo $relheight.'px' ?>"></div>
<?php the_title(); ?>
<?php echo '</li>';?>
<?php endwhile; ?>
<?php else : // What to do if there are no posts from that author
endif;?>
</ul>
</div>
<?php wp_reset_query(); ?>
Here an example:
http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
Using the condition <?php if ($pageposts): ?> you can print your div or not.
I'm not sure how you are querying for the posts in the custom fields but the $wp_query has built in conditionals for handling queries that don't return posts.
Updated code sample:
$args = array(
'meta_key' => 'autore',
'meta_value' => $autore,
'showposts' => 2,
'post__not_in' => array($post->ID)
);
$your_query = new WP_Query( $args );
if ( $your_query->have_posts() ) : ?>
<div id="your-div">
while ( $your_query->have_posts() ) : $your_query->the_post();
// Do stuff
endwhile;
else : // What to do if there are no posts from that author
endif;

Categories