Wordpress last class not working - php

I have a gallery which should display 3 columns, the first two columns have a certain class and work absolutely fine but I am trying to add a "last class" to my third column - for some reason it's not working. Find my code below:
<?php
$mod =1;
if ( $images = get_posts(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',)))
{
foreach( $images as $image ) {
$attachmenturl=wp_get_attachment_url($image->ID);
$attachmentimage=wp_get_attachment_image_src( $image->ID, full );
$imageDescription = apply_filters( 'the_description' , $image->post_content );
$imageTitle = apply_filters( 'the_title' , $image->post_title );
if ($mod % 3 == 0) {
$class = "gallery-entry-img-last";
}else{
$class = "gallery-entry-img";
}
echo '<div class="'.$class.'"><div class="gallery-entry-img-l"><a rel="next" href="' . $attachmentimage[0] . '"><span class="rollover" ></span><img src="library/tools/timthumb.php?src=' . $attachmentimage[0] . '&w=270&h=198" alt="" /></div></div>';
}
$mod++;
}
else {
echo "No Image";
}
?>
Some expert advise would be greatly appreciated.

increment your $mod variable inside foreach loop, like:
foreach(.....) {
......
$mod++;
}

Related

How to display an alphabetically sorted list of post withe Php (WordPress )

I would like to create a glossary overview page on wordpress via PHP, which can be used via a shortcode. I would like that always the initial letter is displayed and then the individual topics (posts) which begin with this.
Example:
A
Apple
Apricot
B
Banana
Blackberry
and so on...
To implement this I use the following code:
// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'glossar',
'orderby' => 'title',
'order' => 'ASC',
));
echo '<ul>';
if( $all_posts->have_posts()){
foreach( range( 'A', 'Z' ) as $letter ) {
echo '<div class="group_letter"><div class="letter">' . $letter. '</div>';
while( $all_posts->have_posts() ){
$all_posts->the_post();
$title = get_the_title();
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper( substr( $title, 0, 1 ) );
if( $initial == $letter ){
echo '<li><a class="glossary-listing" href="/glossar/'. $name . '">' . $title . '</a></li>';
}
}
$all_posts->rewind_posts();
}
}
echo '</ul>';
}
add_shortcode( 'glossary', 'glossary' );
So far it works, but now it shows letters for which there are no posts. This is how it looks now
I have tried to do it with an if query, but so far, I am stuck. Can someone help me?
Best regards and thank you!
Sort the array using PHP sort() function then loop through the result
<?PHP
$list=['apples','popsicles','Zinger','donkeys','bananas','joe',
'Locusts','gazelles','Angels','Popsicle','Dongle','jump','cocoa'
];
//convert all elements to same case
//sorting will sort by case
$list =array_map('strtolower', $list);
//sort the array
sort($list);
$last_letter=null;
foreach($list as $item){
$current_letter=substr($item,0,1);
if($last_letter!=$current_letter){
?>
<div style="margin:1rem;padding:1rem;background:#f5f5f5;">
<?=$current_letter?>
</div>
<?php
$last_letter=$current_letter;
}
?>
<div style="margin:1rem;padding:1rem;background:#f5f5f5;">
<?=$item?>
</div>
<?PHP
}
?>
I am sure there is a better solution besides running 26 times through the while loop. Anyway, here is what you are looking for.
// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
[
'posts_per_page' => -1,
'post_type' => 'glossar',
'orderby' => 'title',
'order' => 'ASC',
]
);
echo '<ul>';
if ($all_posts->have_posts()) {
foreach (range('A', 'Z') as $letter) {
$foundPostable = false;
while ($all_posts->have_posts()) {
$all_posts->the_post();
$title = get_the_title();
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper(substr($title, 0, 1));
if ($initial === $letter) {
if ($foundPostable === false) {
$foundPostable = true;
echo '<div class="group_letter"><div class="letter">' . $letter. '</div>';
}
echo '<li><a class="glossary-listing" href="/glossar/'. $name . '">' . $title . '</a></li>';
}
}
$all_posts->rewind_posts();
}
}
echo '</ul>';
}
add_shortcode( 'glossary', 'glossary' );
As for improvement, something like this might work as well.
// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
[
'posts_per_page' => -1,
'post_type' => 'glossar',
'orderby' => 'title',
'order' => 'ASC',
]
);
echo '<ul>';
$startLetter = '';
while ($all_posts->have_posts()) {
$all_posts->the_post();
$title = get_the_title();
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper(substr($title, 0, 1));
if ($initial !== $startLetter) {
$startLetter = $initial
echo '<div class="group_letter"><div class="letter">' . $letter . '</div>';
}
echo '<li><a class="glossary-listing" href="/glossar/'. $name . '">' . $title . '</a></li>';
}
echo '</ul>';
}
add_shortcode('glossary', 'glossary');

Wordpress - Include static image after every X post through wp_query

I have a somewhat complicated design.
I want to use a CPT to output it. This is my CPT in the wp_query:
/**
* Management Team Shortcode
**/
function team_query() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'management-team',
'order' => 'DESC',
);
$posts = get_posts( $args );
if ( !empty( $posts ) ) {
$flag = 0;
foreach ($posts as $counter => $p) {
$counter++;
if ( $flag <= 2 ) {
$flag++;
}
$role = get_field( "role" );
$name = get_field( "team_member_name" );
$bio = get_field( "bio" );
$profile = get_the_post_thumbnail_url( $p->ID, 'full' );
$flip = get_field( "flip_content" );
$html_out = '<article class="team-member">';
// Do stuff with each post here
if ( $flag % 2 == 0 ) {
//add image after second post like
$html_out .= '<img src="http://www.ankitdesigns.com/demo/rawafid/wp-content/themes/rawafid-systems/assets/img/mt-1.jpg" alt="Safety Whistle" />';
}
if ( $counter % 6 == 0 ) {
$flag = 0;
//add image after sixth post like
$html_out .= '<img src="http://www.ankitdesigns.com/demo/rawafid/wp-content/themes/rawafid-systems/assets/img/mt-2.jpg" alt="Safety Whistle" />';
}
$html_out .= '<div class="meta-team"><h6>' . $role . '</h6>' . '<h4>' . $name . '</h4>' . '<p>' . $bio . '</p></div>';
$html_out .= '</article>';
}
} else {
// No results
$html_out = 'No Management Team Members Found.';
}
return $html_out;
}
add_shortcode( 'show_management_team', 'team_query' );
After 2 posts I'd like to add a static img and after 4 posts another static and after 2 more posts another static img, etc.
I'm open to suggestions on a better approach.
I'm trying to think of an alternative method. Maybe using Visual composer to build a grid?
Hey Darren here is the logic, I think that you can modify my functions and implement it in your code.
/**
* Management Team Shortcode
**/
function team_query() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'management-team',
'order' => 'ASC',
);
$posts = get_posts( $args );
if ( !empty( $posts ) ) {
$flag = 0;
foreach ($posts as $counter => $p) {
$counter++;
if ( $flag <= 2 ) {
$flag++;
}
$title = get_the_title($p->ID);
$link = get_the_permalink($p->ID);
$featured_img = get_the_post_thumbnail_url( $p->ID, 'full' );
$html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">';
// Do stuff with each post here
if ( $flag % 2 == 0 ) {
//add image after second post like
// $html .= <img src="css/images/myimage1.png" alt="" />
}
if ( $counter % 6 == 0 ) {
$flag = 0;
//add image after sixth post like
// $html .= <img src="css/images/myimage2.png" alt="" />
}
$html_out .= '<div class="container"><div class="meta-project"><h6>Latest Project</h6>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a></div></div>';
$html_out .= '</article>';
}
} else {
// No results
echo "Nothing to show";
}
return $html_out;
}
Cheers :)

Wordpress get the attachment link for an image

The images that I'm querying from my wordpress media gallery are appearing correctly. But, I also want them to link to my custom attachment page and I can't get it to work
<?php
$query_images_args = array(
'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
);
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= wp_get_attachment_url( $image->ID );
}
foreach ($images as $img) {
$url = get_attachment_link($img->ID);
echo '' . '<img src="' . $img . '" alt="" />' . '';
}
?>
Skip the second foreach loop. You have everything you need in the first one:
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= wp_get_attachment_url( $image->ID );
$url = get_attachment_link($image->ID);
echo '' . '<img src="' . wp_get_attachment_url( $image->ID ) . '" alt="" />' . '';
}
wp_get_attachment_url() returns the URI of the image, see http://codex.wordpress.org/Function_Reference/wp_get_attachment_url; you were trying to get the ID from this non-object.

cycle next page wordpress order by post title

I'm using Cycle jquery for my website.
I've added a php code do get a link at the end of my slideshow to go to the next page.
I want my pages to be sorted alphabetically but it's not working...
I've added "orderby=post_title" but still not working...
anybody can help me with this ?
here is my PHP code :
function dbdb_next_page_link() {
global $post;
if ( isset($post->post_parent) && $post->post_parent > 0 ) {
$children = get_pages('&orderby=menu_order&order=ASC&child_of='.$post->post_parent.'&parent='.$post->post_parent);
}
//print_r($children);
// throw the children ids into an array
foreach( $children as $child ) { $child_id_array[] = $child->ID; }
$next_page_id = relative_value_array($child_id_array, $post->ID, 1);
$output = '';
if( '' != $next_page_id ) {
$output .= ''. get_the_title($next_page_id) . ' ยป';
}
return get_page_link($next_page_id);
}
and a jsfiddle link : http://jsfiddle.net/KvBRV/
....
I have the same problem with a "select type" menu, my pages are not sorted alphabetically, I don't know why, here is my php code :
<div class="styled-select">
<?php
if(!$post->post_parent){
$children = get_pages(array(
'child_of' => $post->ID,
'post_type' => 'page',
'post_status' => 'publish',
'sort_order' => 'ASC',
'sort_column' => 'post_title',
));
}else{
$children = get_pages(array(
'child_of' => $post->post_parent,
'post_type' => 'page',
'post_status' => 'publish',
'sort_order' => 'ASC',
'sort_column' => 'post_title',
));
}
if ($children) {
echo '<select name="" onchange="location = this.options[this.selectedIndex].value;">';
echo '<option>'. 'A - Z' .'</option>';
function getInitials($name){
//split name using spaces
$words=explode(" ",$name);
$inits='';
//loop through array extracting initial letters
foreach($words as $word){
$inits = strtoupper(substr($word,0,1));
break;
}
return $inits;
}
$currval = "";
foreach($children as $child){
//print_r($child);
$permalink = get_permalink($child->ID);
$post_title = strtoupper($child->post_title);
$initial = getInitials($child->post_title);
if($initial!='' && $currval != $initial ) {
$currval = $initial;
echo '<optgroup label="'.$initial.'""></optgroup>';
}
echo '<option value="'.$permalink.'">'.$post_title.'</option>';
}
echo '</select>';
} ?>
<!-- FIN MENU DEROULANT A-Z -->
</div>
and a jsfiddle link : http://jsfiddle.net/Zp3Lt/
thanks a lot for your help !
Mattieu
get_pages doesnt accept any arg called orderby or order instead sort_column and sort_order is used ..
so your code would be
if ( isset($post->post_parent) && $post->post_parent > 0 ) {
$children = get_pages('sort_column=post_title&sort_order=ASC&child_of='.$post->post_parent.'&parent='.$post->post_parent);
}
Refer Here
Get Pages

PHP preg_match_all with Wordpress to echo a image if more than 1 image in post

Basically what I am looking to accomplish is if there is more than 1 img it will echo the below statement so I can basically have a rollover saying click to see more. Another question would be how would I have it link to the post itself if I were to change it to 'a href' and echo out the permalink to post_id.
Any help would be very much appreciated.
function catch_images() {
global $post, $posts;
$first_image = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/ii', $post->post_content, $matches);
$first_image = $matches [2] [0];
if ($output == '2') {
echo '<div class="seemore"><img src="images/magglass.png"></div><div class="seemoretext">See More</div>';
}
}
Well I feel stupid, I should of just put the following:
if ($output > '2') {
Why don't you do something like this to check the number of attached images in your post? This assumes that your images are attached to your post, though.
Well, As said in comment , I did not really understood what you want to link if it has more than 2 images , to the image itself ?? to attachment page ? to another post ?
anyhow, something like this should work - read and execute the comments to suit your needs ..
(the second question I did not understood ...)
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'published', // or NULL
//'post_mime_type' => 'image', // only if you want images alone
'post_parent' => $post->ID
);
$attachments = get_posts($args);
$counter = 0;
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
if (!$counter == 1) {
echo wp_get_attachment_image($attachment->ID);
}
else {
// uncomment the following line if you want a LIST of all following attachments and then delete the marked line
//echo '<a href = ' . wp_get_attachment_url($attachment->ID) . '> see more (image'. $counter .') </a>' ;
}
++$counter;
}
// Delete this line if you have more than 2 images, otherwise it will show the last one only
echo '<a href = ' . wp_get_attachment_url($attachment->ID) . '> see more (image'. $counter .') </a>' ;
}
The following is the answer to my own question, it will echo if there is more than 1 img src tag in a given post.
function catch_images() {
global $post, $posts;
$first_image = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/ii', $post->post_content, $matches);
$first_image = $matches [0] [1];
if ($output > '2') {
echo '<div class="seemore"><div class="seemoreimg"><img src="images/magglass.png"></div><div class="seemoretext">See More</div></div>';
}
}
Thanks everyone for your help!

Categories