I'm struggling to compare two php variables to display or not display some text depending if the variables match or not. This is what I have:
<?php $link = the_permalink();?>
<?php $portfolioloop = new WP_Query( array( 'post_type' => 'news' ) ); ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
<?php while(the_repeater_field('featured_companies')): ?>
<?php $company = the_sub_field('featured_company'); ?>
<?php if ($link == $company) { ?>
show news articles
<?php } else { ?>
don't show news articles
<?php } ?>
<?php endwhile; ?>
<?php endwhile; // end of the loop. ?>
I want to compare $link and $company and if they match then do the stuff within the if. Where am I going wrong?
I'm using the http://www.advancedcustomfields.com plugin in Wordpress if that helps.
UPDATE:
Firstly forgot to mention that the two variables are urls. At the moment it's echoing out 2 urls that are the same on the page I want, but it's also echoing out "show news articles" when the 2 urls don't match.
Underneath the twitter profile - http://www.mediwales.com/v3/members/mediwales/ shows the same two urls. But when you goto this page http://www.mediwales.com/v3/members/3m/ it shows two different urls yet shows "show news articles".
You have to be careful with Wordpress's native functions:
the_permalink() echoes out the permalink (see documentation examples)
get_permalink() returns it as a variable (see documentation examples)
So you need to be using:
$title = get_permalink();
Just solved it:
<h2>Latest News</h2>
<?php $link = get_the_title(); ?>
<?php $portfolioloop = new WP_Query( array( 'post_type' => 'news' ) ); ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
<?php $post_link = get_post_permalink(); ?>
<?php if (get_field('featured_companies') != "") { ?>
<?php foreach(get_field('featured_companies') as $post): ?>
<?php $company = get_the_title($post_object->ID); ?>
<?php if ($company == $link) { ?>
News item 1
<?php } ?>
<?php endforeach;?>
<?php } ?>
<?php endwhile; ?>
Related
I'm working on a website for a band where you can add gigs and add the songs played on that specific gig.
So I've created two custom post types:
- gig
- song
I got a custom field "Songs" of type "Relationship". This field is shown on the Custom Post Type. This way I can add songs to a specific gig. This works perfectly.
But I want to show some statistics on the homepage of that website: I want to count how many times a specific song is played and show the top 10. So I guess that I have to loop over the gig custom post type and count the relation with 'songs'.
I thought this would do the trick:
<?php
$args = array(
'post_type' => 'gig'
);
?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
print_r(get_field('songs'))
//$song_count = count(get_field('songs'));
//echo $song_count . " ";
the_title();
?><br />
<?php endwhile; ?>
<?php else: ?>
<!-- No gigs available -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
You can find the result of the print_r over here: http://snippi.com/s/njzg3uu
For example: the song "A memory" is on 2 gigs. That why you can find it twice in the array. The song "Wasted" can be found only once, because it's on 1 gig.
You can use this code to create an array of all the songs:
<?php
$args = array(
'post_type' => 'gig'
);
$countArray = []; //create an array where you can put all the song id's and the number of times played
?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$posts = get_field('songs');
if( $posts ):
foreach( $posts as $post):
setup_postdata($post);
//if the song id already exists -> count + 1
if (array_key_exists($post->ID, $countArray)){
$countArray[$post->ID]++;
}
else { // otherwise the song is played 1 time
$countArray[$post->ID] = 1;
}
endforeach;
wp_reset_postdata();
endif;
?>
<?php endwhile; ?>
The code above will create an array of post ID's of songs and the number of times it is used in the post_type "gig".
Now you can use the array $countArray and do whatever you want with it.
In your example you want to sort it, so you have to do arsort($countArray); This way the array is sorted by it's value (the number of times played) from high to low.
Then you have to loop through the array:
foreach ($countArray as $key => $value) {
?>
<?php echo get_post_permalink($key); //=the permalink of the song ?>
<?php echo get_the_title($key); //= the title of the song ?>
<?php echo $value; //number of times play in a gig ?>
<?php
}
So the full code is:
<?php
$args = array(
'post_type' => 'gig'
);
$countArray = [];
?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$posts = get_field('songs');
if( $posts ):
foreach( $posts as $post):
setup_postdata($post);
if (array_key_exists($post->ID, $countArray)){
$countArray[$post->ID]++;
}
else {
$countArray[$post->ID] = 1;
}
endforeach;
wp_reset_postdata();
endif;
?>
<?php endwhile; ?>
<?php
arsort($countArray);
foreach ($countArray as $key => $value) {
?>
<?php echo get_post_permalink($key); //=the permalink of the song ?>
<?php echo get_the_title($key); //= the title of the song ?>
<?php echo $value; //number of times play in a gig ?>
<?php
}
?>
<?php else: ?>
<!-- No gigs available -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
You Could do like this in simple and short way:
$args = array(
'post_type' => 'gig'
);
$gigs = get_posts($args);
$songsarr = array();
foreach($gigs as $gig) {
$posts = get_field('songs', $gig->ID);
array_push($songsarr,$posts[0]);
}
//echo "<pre>;
//print_r($songsarr);
$countsongs = array_count_values($songsarr);
echo 'No. of Duplicate Items: '.count($countsongs).'<br><br>';
// print_r($countsongs);
foreach($countsongs as $songID => $songname){
echo get_the_title( $songID );
echo $songname;
}
I have Tried by making Two Custom Post Type(gig, songs) and I got count numbers of song this way that you can show in home page and also you can give condition in last foreach loop if song more than one etc..
Hope this will help you!
Hope help:
<?php
$args = array(
'post_type' => 'song'
);
?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$song_count = count(get_field('songs', get_the_ID())); <-- add
echo $song_count . " ";
the_title();
?><br />
<?php endwhile; ?>
<?php else: ?>
<!-- No gigs available -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
My understanding of your question is you are seeking to generate a top 10 list of songs that have the most associated gigs. The best way to approach this would be to generate a set that would map a unique identifier and a count value of how many times that songs has been seen.
Here is an example:
<?php
// Get all the posts
$gigs = get_posts([
'post_type' => 'gigs',
'numberposts' => -1
]);
// We will use this array to key a running tally of
$set = [];
// If the key doesn't exist yet on the array, then we will initialize it, otherwise, increment the count
function add_set_element(&$set, $key) {
if (!isset($set[$key])) {
$set[$key] = 1;
} else {
$set[$key]++;
}
}
function iterate_songs($songs, &$set){
/** #var WP_Post $song */
foreach($songs as $song) {
$key = $song->post_title;// This can be what ever unique identifier you want to get from $song object, such as ID or title
add_set_element($set, $key);
}
}
foreach($gigs as $gig) {
setup_postdata($gig);
$songs = get_the_field('songs');
iterate_songs($songs, $set);
}
Afterwards you can sort and manipulate the $set variable however you feel to get the data you want from it.
Let me know if I misinterpreted your questions, and I can provide another answer.
I created a custom loop for exclusion of sticky post from the flow. So in that loop no sticky post came, but the problem is when I went to page/2/ or page/3/ same posts are repeating.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$rest = new WP_Query(array(
'post__not_in' => get_option('sticky_posts'),
'paged' => $paged,
));
?>
<?php if($rest->have_posts()) { ?>
<?php while($rest->have_posts()) { ?>
<?php $rest->the_post(); ?>
<?php get_template_part('template/main/main-content'); ?>
<?php } ?>
<?php } else { ?>
<p> no post available </p>
<?php } ?>
<?php the_posts_pagination(); ?>
<?php wp_reset_query(); ?>
Use this parameter in your argument : 'posts_per_page'. It will call the next records according to the page number you pass.
I tried many attempts and finally I cracked the code.
I just add
wp_reset_query();
in the second line from top.... just after opening tag of php and it worked.
I'm making website about movies. Taxonomy is using to for a cast. For example:
Cool yeah? :D But i want to show the description on this page. I'm talking about this:
How to make it? Here is a code of taxonomy.php:
<?php get_header(); ?>
<div class="module">
<?php get_template_part('inc/parts/sidebar'); ?>
<div class="content">
<header><h1><?php printf( __( '%s', 'mundothemes' ), '' . single_tag_title( '', false ) . '' ); ?></h1></header>
<div class="<?php if(is_tax('dtquality')) { echo 'slider'; } else { echo 'items'; } ?>">
<?php if (have_posts()) :while (have_posts()) : the_post(); ?>
<?php if(is_tax('dtquality')) { get_template_part('inc/parts/item_b'); } else { get_template_part('inc/parts/item'); } ?>
<?php endwhile; endif; ?>
</div>
<?php if (function_exists("pagination")) { pagination($additional_loop->max_num_pages); } ?>
</div>
</div>
<?php get_footer(); ?>
That should work with <?php echo term_description(); ?>
see also https://codex.wordpress.org/Function_Reference/term_description, where you can also read about the two optional parameters $term_idand $taxonomy
You can use the method term_description
echo term_description($term_id, "your-taxonomy");
Every taxonomy has a unique id, like "course_teachers". so we grap it like :
get_the_terms($post->ID , 'course_teachers');
In this example we are graping the first term of ower taxonomy and store it inside a variable. like :
$course_teacher = get_the_terms($post->ID , 'course_teachers')[0];
now we need to grap the description :
$teacher_description = term_description( $course_teacher, 'course_teachers' );
Finally, print the description on the web page :
echo $teacher_description;
Note: if you get an error like (variable $post not exist). don't worry, just add code below on top of your code :
global $post;
If you are using a "loop". you need to write the $teacher_description variable inside the loop.
Grap other data's :
-> taxonomy name
$teacher_name = $course_teacher->name;
-> taxonomy archive URL
$teacher_archive_url = get_term_link( $course_teacher, 'course_teachers' );
I'm not a backend developer and it's was all of my knowledge. I hope this will help someone.
123 is term id.
$data = get_term(123)->description;
print_R($data);
Display category description:
<?php
the_archive_description( '<div class="taxonomy-description">', '</div>' );
?>
https://developer.wordpress.org/reference/functions/the_archive_description/
I'm running a multi site and I want to pull post with certain categories from two blogs. Therefore I am running a loop for each of the blog to pull the posts as one of the category is in blog 1 while the other category is in blog 2.
<?php $value = array(); ?>
<?php
// Get the values from $_POST
$original_blog_id = get_current_blog_id(); // get current blog
$bids = array(1,2); // all the blog_id's to loop through EDIT
foreach($bids as $bid):
switch_to_blog($bid);
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'Music',
'field' => 'slug',
'terms' => array('artist', 'club')
),
)
);
$the_query = new WP_Query( $args );
?>
<?php $postids = array(); ?>
<?php if ( $the_query->have_posts() ) { ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $postids[]=get_the_ID(); ?>
<?php endwhile; ?>
<?php $value[] = $postids; ?>
<?php
} else {
// no posts found
echo 'Nothing found.';
}
?>
<?php endforeach; ?>
<?php
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($value));
$list = iterator_to_array($it,false);
$posts = new WP_Query(array(
'post__in' => $list,
'post_type' => 'post',
));
?>
<?php
foreach ($posts as $post) {
setup_postdata($post);
?>
<?php echo the_title(); ?>
<?php
}
wp_reset_postdata(); ?>
<?php switch_to_blog($original_blog_id); ?>
The reason why I'm getting the IDs inside an array:
<?php $postids[]=get_the_ID(); ?>
because I want to fetch random post's. If at this point instead of the above statement I get the title and content of the posts then it will show in sequential order. Something like this:
BLOG1: POST1,POST2, POST : BLOG2: POST1, POST2, POST3
But I want Posts in random order like this:
BLOG1: POST1, BLOG2: POST3, BLOG1: POST2, BLOG2: POST1: BLOG2: POST2
So everything is working fine, I am able to get the posts IDs even outside the foreach loop but the problem is:
I am not able to get post content from those IDs. It only gives me posts from blog 2 because the current blog is 2. But it doesn't show anything from blog1 even though the postID is in the list array.
Can anyone please help?
The solution can be the following, just add the code into your themes footer.php files, although this can be put anywhere within your WordPress theme, example:
<?php
if (!function_exists('display_posts_from_blogs')) {
function display_posts_from_blogs($blog_id) {
global $switched;
switch_to_blog($blog_id); //switched to blog id 2, for example
// Get latest Post
$latest_posts = get_posts('category=-3&numberposts=6&orderby=post_name&order=DSC');
$cnt =0;
?>
<ul>
<?php foreach($latest_posts as $post) : setup_postdata($post);?>
<li>
<?php echo $post->post_title; ?>
</li>
<?php endforeach ; ?>
<?php restore_current_blog(); //switched back to main site
} //end function
}
?>
I'm using the "bones" theme on WordPress. On my blog page I'm trying to have it with 9 blog posts displayed in 3 columns, but on the first page have 10 blog posts with the first (most recent) one being enlarged to span all 3 columns.
What is the best way to display 10 posts on the first page and 9 thereafter without messing up the pagination?
Here's my code: (I removed all the HTML and what-not because I assume it's not needed)
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<?php $c++;
if( !$paged && $c == 1){
//code for the first post
} else { // THE REST: begin the code for the remainder of the posts ?>
<?php }
endif; ?>
<?php endwhile; ?>
I haven't really experimented with it as I don't have the page function set up - but try this
<?php
$post = $posts[0]; $c=0;
$c++; if( !$paged && $c == 1){
$query1 = new WP_Query( array ('posts_per_page' => 1 ) );
if ($query1-> have_posts()) : while ($query1-> have_posts()) : $query1-> the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; wp_reset_query();}
else{
$query2 = new WP_Query( array ('posts_per_page' => 9, 'offset' => 1 ) );
if ($query2-> have_posts()) : while ($query2-> have_posts()) : $query2-> the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; wp_reset_query(); }?>