I've tried what I could on this and am still stuck, so I'm looking for some help. I'm sure there's something small I'm overlooking or am not aware of, so I'd be grateful for another set of eyes to take a look!
I'm trying to use a switch within a Wordpress while loop to set dimensions on post thumbnails for specific posts. The switch uses an auto-incrementing value ($count). Inside the loop, $count will return the right number for div ID's, but it will not work with the switch. All of the thumbnails go to the size defined before the loop begins (see $thumbsize)
Here's the code:
// Setup loop to pull only posts tagged slider
$max = 6;
$args = array('tag' => 'slider','posts_per_page' => $max);
$featuredPosts = new WP_Query();
$featuredPosts->query($args);
// Defaults for post thumbnail display
$thumbargs = array('class' => 'featured-blocks-img');
$thumbsize = array(640,360);
$count = 0;
// Begin loop
if ($featuredPosts->have_posts()) : while ($featuredPosts->have_posts()) : $featuredPosts->the_post();
$count++;
// Get post category and format for div class name
$category = get_the_category();
$catname = $category[0]->cat_name;
$catdash = 'cat-';
$catdash .= str_replace(' ', '-', $category[0]->cat_name);
$catdash = strtolower($catdash);
// Change post thumbnail size conditionally
switch ($count) {
case 2:
case 5:
case 6:
$thumbsize == array(320,260);
break;
default:
$thumbsize == array(640,360);
} // End switch
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div id="home-featured-post-<?php echo $count;?>" class="featured-blocks-post <?php echo $catdash; ?>">
<h2 class="home-featured-title"><?php the_title(); ?></h1>
<?php the_post_thumbnail($thumbsize, $thumbargs); ?>
</div>
</a>
<?php
endwhile;
endif; // End loop
And here it is in Gist form if that's helpful to anyone: https://gist.github.com/anonymous/8984741
I tried to add comments that would provide some context.
Any ideas of what's happening? I can provide the resulting HTML source if that would help also.
Looks like you aren't actually setting $thumbsize in the below code
$thumbsize == array(320,260);
== is comparing $thumbsize to that array, not creating an array with those values.
You really want it to just look like this:
$thumbsize = array(320,260);
Related
I made the ACF plugin group with files to download. In group I have fields "File 1", "File 2"...etc.
I would like to display all attached files to page. It is possible to display all fields belong to group? I try with basic code, but in this case I have only 1 file.
How can I add iteration to this or display all fields?
<?php
$file = get_field('attachment_1');
if( $file ):
// vars
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
if( $caption ): ?>
<div class="wp-caption">
<?php endif; ?>
<ul>
<li><a href="<?php echo $url; ?>" title="<?php echo $title; ?>">
<span><?php echo $title; ?></span>
</a>
</li>
<ul>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
As all your fields are set up individually, it isn't just a matter of looping through an array of all your fields of the same type (i.e. just your file fields).
There are a few ways that might work for you:
Option 1.
If all the field names for your files follow the same naming pattern and are named sequentially, you could loop using the name.
Example, assuming your fields are named attachment_1 up to attachment_5:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
// display file details as appropriate
}
}
Option 2.
If the file field names do not follow the same pattern, you could loop through an array of the field names.
Example:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
// Create an array with the field names of all your files
// N.B. This also lets you specify the order to process them
$file_fieldnames = array('file_1', 'file2', 'another_file');
foreach ($file_fieldnames as $fieldname) {
$file = get_field($fieldname);
if( $file ){
// display file details as appropriate
}
}
Option 3. If you want to loop through ALL fields on the post/page, you can save the fields into an array.
This might seem like the most generic approach at first, but it is complicated by the fact that you don't know what type each field is in order to know how to process and display them... you first have to work out what field type it is. You could do this by name (similar to above) or you could try to identify what each field by checking the field content.
Note, checking the field content is very risky, as there are other field types that can have similar featured (e.g. a file is not the only type that can have a url) so I wouldn't advise that strategy unless you are 100% sure you'll never change the field group or add another field group to the post/page.
Example:
$fields = get_fields();
foreach ($fields as $fieldname => $content) {
if (is_string ($content)){
// display the string
}
else if (is_array($content) && $content['url']) {
// then you could assume its a file and display as appropriate
}
}
Note that none of this code is tested. However it should give you an idea of the logic behind each option so you can decide what works for you.
UPDATE based on new code provided:
See below based on the code in your JSFiddle. I've ignored the caption outside the file list because it makes no sense - every file can have its own caption.
<?php
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
// now display this file INSIDE the loop so that each one gets displayed:
?>
<li>
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank">
<span><?php echo $title; ?></span>
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
<?php endif; ?>
</li>
<?php
} // end if
} // end for loop
?>
<ul>
If you understand arrays, I'd suggest you add the file details into an array and then do a second loop to display the files... however I'm guessing you're not that proficient with basic coding constructs as you don't understand loops, so I've tried to keep it simple. I strongly recommend that you do some tutorials on programming basics if you are attempting to write code.
I'm using acf plugin on a wordpress site. I am using a repeater field with an image subfield that is displayed on a blogpost. My goal is to only get and display the first and second images from a blog post to be displayed for the homepage.
I tried using this code from the acf site documentation but to no avail the code is not working. Can someone know the issue?
<?php while ($the_query -> have_posts()) : $the_query ->
the_post();
$postqID = get_the_ID();
?>
<?php
$rows = get_field('post_images', $postqID ); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['post_image' ]; // get the sub field value
// Note
// $first_row_image = 123 (image ID)
$image = wp_get_attachment_image_src( $first_row_image, 'full' );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />
I just solved this issue. For those who are also having issues on displaying certain number of rows of images on a repeater field. You can refer my answer.
The code below returns two images from a repeater field. Just changed the condition ($i>1), if you wish to return certain number of image.
<?php
$i = 0;
if(get_field('post_images', $postqID)):
?>
<?php while (has_sub_field('post_images',$postqID)): ?>
<img src="<?php the_sub_field('post_image')['url']; ?>"></img>
<?php
$i++;
if($i > 1)
{
break;
}
?>
<?php endwhile; ?>
<?php endif; ?>
I need to make a grid similar to this:
Few keywords: specific size pattern, bigger points in higher.
All boxes are custom posts - lets call every box a post.
All different sizes posts have different meta_key called $size:
Yellow/big $size= 1;
Orange/medium $size= 2;
Blue/small $size= 3;
All posts have also meta_key value called $points which are totally different for each (can be anything from 0 to 10000). $points is currently used as orderby.
This is how I call all these posts:
$custom_posts= new WP_Query(array(
'post_type' => 'custom-post',
'meta_key' => 'points',
'orderby' => 'meta_value_num'
));
if ( $custom_posts->have_posts() ) :
echo '<div id="post-items"><ul class="row list-unstyled">';
while ( $custom_posts->have_posts() ) : $custom_posts->the_post();
$size= get_post_meta( $post->ID, 'post_size', true );
$points = get_post_meta( $post->ID, 'post_points', true );
echo '<li>';
if($size == 1) {
?>
//Big yellow box HTML
<?php
}
else if($size == 2) {
?>
//Medium orange box HTML
<?php
}
else if($size == 3) {
?>
//Small blue box HTML
<?php
}
echo '</li>';
endwhile;
echo '</ul></div>';
wp_reset_query();
else :
?>
<p class="text-muted"><?php _e( 'No Posts.', 'aa' ); ?></p>
<?php
endif;
QUESTION:
Any ideas how to query them in specific $size pattern also considering $points (higher points in higher, but do not break the order - LIKE ON MY IMAGE)?
Im working my arse off as we speak (it's 3rd day now on this problem!!) & I would appriciate every idea, comment or source!
What could be useful but I haven't found a working solution yet (I'll add more if I come across some):
Using modulus:
PHP loop: Add a div around every three items syntax
UPDATE 1:
I am not a Wordpress expert by any stretch, so there may be an easier way to do this with a foreach loop, but here it is with a while as you have it in the question:
// This will be used as a counter
$i=0;
while ( $custom_posts->have_posts() ) : $custom_posts->the_post();
// Get size based on counter
switch($i) {
case 0:
case 5:
$size = 1;
break;
case 1:
case 2:
case 3:
case 4:
case 6:
case 7:
$size = 2;
break;
default:
$size = 3;
}
$points = get_post_meta( $post->ID, 'post_points', true );
echo '<li>';
if($size == 1) {
?>
//Big yellow box HTML
<?php
}
else if($size == 2) {
?>
//Medium orange box HTML
<?php
}
else if($size == 3) {
?>
//Small blue box HTML
<?php
}
echo '</li>';
// Increment the counter
$i++;
endwhile;
If I understand your question correctly, this should always display the sizes in the order that you have them (1,2,2,2,2,1,2,2,3,3,3,3,3,3, etc.). Also, if there are less than that number of entries, it will still work, and if there are more, size 3 is the default, so anything else after this will display as 3.
Making mobile site with Concrete5 and using page list block with custom template. I'm trying to count sub pages using PHP.
<?php foreach ($pages as $page):
// Prepare data for each page being listed...
$title = $th->entities($page->getCollectionName());
$url = $nh->getLinkToCollection($page);
$target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
$target = empty($target) ? '_self' : $target;
$description = $page->getCollectionDescription();
$description = $controller->truncateSummaries ? $th->shorten($description, $controller->truncateChars) : $description;
$description = $th->entities($description);
$mies = 0;
?>
<li class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c" data-theme="c"><div aria-hidden="true" class="ui-btn-inner ui-li"><div class="ui-btn-text"><a target="<?php echo $target ?>" class="ui-link-inherit" href="<?php echo $url ?>">
<h2 class="ui-li-heading"><?php echo $title ?></h2>
<p class="ui-li-desc"><?php echo $description ?></p>
</a>
</div><span class="ui-icon ui-icon-arrow-r ui-icon-shadow"></span><span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo count($mies) ?></span></div></li>
<?php endforeach; ?>
So, probably need to use Count function(or length?), I don't know. If I am editing wrong place please advice if you have any experience in Concrete5 cms.
If you want to show the corresponding page number in the span element in your code:
<span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo $mies; ?></span>
If you want to show the remaining sub-pages, then in the html code snippet above just replace $mies with count($pages) - $mies like:
<span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo count($pages) -$mies; ?></span>
You would first have to initialise $mies before you start the forloop so it should be something of the form:
<?php
$mies = 0;
foreach ($pages as $page):
//Rest of your code and increment $mies with every iteration
$mies ++; //This ensures that you are counting the corresponding pages
?>
If you want to get the count of total number of sub-pages, you just have to echo out $mies outside the for block may be like:
<?php
endforeach;
echo $mies; //Shows you the total number of pages processed inside the for loop.
//or Just find the length of pages array
echo count($pages);
?>
As far as getting the length of array is concerned you could use count or sizeof. I stumbled upon a SO question about using count or sizeof method for finding the length of an array.
This should get you started in the right direction.
You need the parent ID;
$parentId = Page::getCollectionParentId();
Note that Page::getCollectionParentId() gets the current page's parent ID,so you may want to try;
$parentId = $page->getCollectionParentID();
Then create a new PageList to filter with and filter by the parentId;
Loader::model('page_list');
$pl = new PageList();
$pl->filter(false, '(p1.cParentID IN ' . $parentId . ')');
// Get the total
var_dump($pl->getTotal());
This is untested but the theory makes sense.
This is likely a bit simpler.
$pl->getTotal()
$pl is the PageList object that is set in the controller.
Also, these days you can just use the h() method instead of writing out $th->entities()
Edit: I should clarify that you don't need to do a $pl = new PageList() because $pl is already set to the PageList object in the controller and passed to the view.
I have some php code which echos the width of a specific image size in WordPress. Currently, if the image width is greater than 80, then it echos "frog"...what I would like it to do is count all of the image widths within my while and if the total of those images are greater than 600 (hypothetical number) then echo "frog". The code I am using looks like (I am using this code within my while):
<?php
$image = wp_get_attachment_image_src (get_post_thumbnail_id($post_id), 'gallery-thumbnail');
list($width) = getimagesize($image[0]);
echo $width;
if( $width > 80 ) {
echo "frog";
}
?>
My while is the basic WordPress standard:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- some code here -->
<?php endwhile; else: ?>
<!-- some code here -->
<?php endif; ?>
Any ideas?
Thanks, Josh
Part pseudo code/part solution:
$sumOfWidths = 0;
foreach($images as $image)
{
$sumOfWidths = $sumOfWidths + $image['width'];
}
if($sumOfWidths>600)
{
echo 'frog';
}
Simply put the above code loops through each image and adds the image width to the $sumOfWidths variable.
After the foreach loop, has completed, there should be a number in the $sumOfWidths which you can check and then undertake your logic as you see fit.
Have a look at this code snippet to get you started