Loop through php code (with a custom field) - php

I use a wordpress plugin in which i can add custom data for each category
Now because of some error it doesn't always grab the correct data. to fix that i wrote the following:
<?php if ( in_category( 3 ) ) :
?>
<?php the_field( "categoryselect", "category_3" ); ?>
<?php endif; ?>
Now that works, but is there a way to loop through all my categories with this?
(basicly i need the "3" and the number in "category_3" to loop through all my categories)
i couldnt get it to work, so was going to hard code it, but thought i would ask for help before hardcoding it :P
Thnx in advance
Edit, with help from Ketan, i came to this solution that works for my problem
<?php
$category = get_queried_object();
$testtest = $category->term_id;
if ( in_category( $testtest ) ) :
?>
<?php the_field( "categoryselect", "category_".$testtest ); ?>
<?php endif; ?>

You can do something like this:
<?php
for($i = 0; $i < categories_count; $i++) {
if ( in_category( $i ) ) :
the_field( "categoryselect", "category_".$i );
endif;
}
?>

Related

Unlimited posts in custom layout PHP

Currently I am working on a Wordpress website with the posts showing in two columns running with a PHP code. The problem I am facing is that the posts showing on the website are limited to 10. Does anyone know where in the code I can make a change to show unlimited posts? Or do I need a whole new PHP code for this?
Would like to hear from you!
This is the code I am using
<?php $i = 0; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
if($i == 0) {echo '<div class="ng-row">';}?>
<div class="half">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php $i++; if($i == 2) {$i = 0;echo '</div>';}?>
<?php endwhile; else: ?>
<?php if($i > 0) {echo '</div>';}?>
<?php endif; ?>
I've tried to change the numbers but it didn't work out well..
For an unlimited return set the posts_per_page to -1
add_action( 'pre_get_posts', 'set_posts_per_page' );
function set_posts_per_page( $query ) {
// This will set the query to return all results
$query->set( 'posts_per_page', -1 );
return $query;
}
Although be aware that this can be very bad for performance depending on the amount of posts. It should really only be done if you have a limited result set and know what that is.

Php code inside php code in wordpress using custom fields

I have a code which returns gallery number 1 with the set of images.
<?php echo photo_gallery(1); ?>
But I need to change number "1" to number "2", "3" and so on with custom fields in every post using this code
<?php the_field('number'); ?>
inside the first code
I need something like this:
<?php echo photo_gallery( <?php the_field('number'); ?> ); ?>
Result:
<?php echo photo_gallery(2); ?>
or
<?php echo photo_gallery(3); ?>
If you really have a custom field number, your code should be the following:
<?php echo photo_gallery( get_the_field( 'number' ) ); ?>
if you want to get the post and then after give the number then follow the code like
$cnt=1;
$queryarry=array( 'post_type' => 'post' ,'posts_per_page' => 9);
while ( have_posts() ) : the_post();
echo photo_gallery($cnt);
$cnt++;
endwhile;
try this

Php continue not working if not empty?

I am having a loop and that loop has only sticky posts in it. So my logic works like this:
"If Sticky Posts are "EMPTY" break the loop". That code works as expected and looks like this:
<?php //we will get "Sticky Posts" only with this loop and exlude Featured Category
$category = get_cat_ID('Featured');
$col = 1; //Let's create first column
$sticky = get_option( 'sticky_posts' );
$args = array(
/* Add whatever you need here - see http://codex.wordpress.org/Class_Reference/WP_Query */
'paged' => $paged,
'category__not_in' => array($category),
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);
/*Below is IMPORTANT PART*/
if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) :
$wp_query->the_post();if(empty($sticky))break;?>
<div <?php post_class('col'.$col); ?> id="post-<?php the_ID(); ?>">
<?php if ($col == 1) echo '<div class="row">';//If column 1 create first row ?>
<?php if ($col == 2) echo '<div class="row2">';//If column 2 create second row ?>
<h3 class="mytitle"><?php the_title(); ?></h3>
<div class="entry">
<?php if ( has_post_thumbnail() ):?>
<div class="featured_img">
<?php
the_post_thumbnail();
echo '<div class="featured_caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
?>
</div><!--/featured_img-->
<?php endif; ?>
<?php // let's enable more link on pages...
global $more;
$more = 0;
?>
<?php the_content(__('Read more','override')); ?>
<div class="clear"></div>
<div class="custom_fields"><?php the_meta(); ?></div><br/>
<p class="postmetadata">
<?php _e('Filed under:','override'); ?> <?php the_category(', ') ?> <?php _e('by','override'); ?> <?php the_author(); ?><br/><?php the_tags(__('Tags:','override'), ', ', '<br />'); ?>
<?php _e('Posted on: ','override'); ?><?php the_time(get_option('date_format')); ?><br/>
<?php if ( comments_open() ) {
comments_popup_link(__('No Comments »','override'), __('1 Comment »','override'), __('% Comments »','override'));}
else {
_e('Comments are disabled!','override');
}
?>
<?php edit_post_link(__(' Edit','override'), __(' |','override'), ''); ?>
</p>
</div><!--/entry-->
</div><!--/post_class-->
<?php /*Enable Two Column Layout*/
if($col==1) {
$col=2;
echo "</div>";
}
else if($col==2) {
$col=1;
echo "</div>";
}
endwhile; ?>
<?php endif; ?><!--END if THE LOOP (Sticky)-->
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
Now before this working code I tried a different logic that goes like this:
"If NOT EMPTY continue the loop" so now everything in my code stays the same except:if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) :
$wp_query->the_post();if(empty($sticky))break;?> so now that code becomes:if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) :
$wp_query->the_post();if(!empty($sticky))continue;?>
Now this is where i got confused because if(!empty($sticky))continue; part does not work as expected because my loop CONTINUES (returns other posts) even if there are no "Stickies". I thought that loop will STOP if there are no stickies but it is not the case. My var_dump($sticky)
shows this if there are sticky postsarray(1) { [0]=> int(214) } and shows this if there are no stickiesarray(0) { }.
My question is: Why the loop continues to return other posts if using if(!empty($sticky))continue; (i thought it will return ONLY "Stickies" if they exist and return NOTHING if they are not here. )
Thank you!!
First off, let me poit out that your logic doesn't quite agree with your code :).
From what I understand from your code, you want to iterate all posts WP_Query() returned, but only render sticky ones. Your if is inside the wile loop, so you have to check if the current post is sticky or not. However, if(empty($sticky)) doesn't do that. It checks if there are any sticky posts at all. A way to check the current post would be if(is_sticky(the_ID())).
Now, concerning continue:
From the php manual:
continue is used within looping structures to skip the rest of the
current loop iteration and continue execution at the condition
evaluation and then the beginning of the next iteration.
So as you can see, continue doesn't stop the loop, but rather attempts to start the next iteration ignoring the rest of the code for the current step. Which is what you want, if the current post is not sticky, in other words if(!is_sticky(the_ID())).
However, I think you don't really need any check at all, since you already specified that you want WP_Query() to fetch only stickies ('post__in' => $sticky).
See also: this WordPress Answers topic.
I don't know well the wordpress code, but in that code, the $sticky variable is never updated in the while loop. So maybe you have to add $sticky = get_option( 'sticky_posts' ); right before the if condition.

How To Show Number of Posts in WordPress? Not Working

I can’t figure out why this won’t work? I have more than 15 posts but they’re getting cut off the page.
The site: http://gruntsandco.com/
My loop:
<?php $latest = new WP_Query('showposts=15'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
Loopageness
<?php endwhile; ?>
You can try 'posts_per_page' attribute for WP Query.
<?php $latest = new WP_Query('posts_per_page=15'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
Loopageness
<?php endwhile; ?>
If you want to show all posts just set this parametr to -1.

How can I combine this php statement to get the results of multiple variable inputs?

This is my WordPress query but is not a wordpress related question. It shows the posts that have meta_key as extra1 and meta_value as test
<?php $customkey1 = extra1; ?>
<?php $customvalue1 = test; ?>
<?php query_posts('meta_key=' . $customkey1 . '&meta_value=' . $customvalue1 . ''); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; ?>
My question is how can I still show the posts that have extra1 as metakey and test as metavalue but also the posts that have extra2 as metakey and test2 as metavalue in the same query. A combination of two or more variables.
So you want to combine the results of two queries? Check this out You may just need to make two query objects and combine them.
Something like (note, I do not have WordPress installed, nor use WordPress. But assuming that its API is not a lie):
<?php
// The Query
$the_query = new WP_Query( $args );
$the_second_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// The Second Loop
while ( $the_second_query->have_posts() ) : $the_second_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
Note: This is hideous, ugly, and will work but in most cases should be considered WRONG by professionals. More elegantly, at least I would use a function to parse the post. Most elegantly, I would do what has been suggested elsewhere, encapsulate the combined query in a MySQL bit. But given the op's knowledge, this seems to be the "best approach" for quickly solving this problem, hopefully only once. (Repeated application of this approach will turn it into a messy nightmare)

Categories