I use the Wordpress is_page conditional tag frequently, and typically I am defining a single ID, or an array of ID's to include, or exclude from displaying some custom code.
What I'm trying to figure out, is whether I can define a range of ID's to feed in as an argument.
Here is a simple example of using the is_page condition:
<?php if(is_page( 5 ): ?>
<p>This is some text</p>
<?php endif; ?>
Here is a simple example of using the id_page condition with an array:
<?php if(is_page( array( 5,6,7 ) ): ?>
<p>This is some text</p>
<?php endif; ?>
What I'd like to do, is have this where I can pass through an argument, and define that as a range of ID's such as 5-7:
$arg = 5-7;
<?php if(is_page( $arg ): ?>
<p>This is some text</p>
<?php endif; ?>
Where I'm defining the arg, I think it's a matter of maybe setting a boundary, defining an array, and placing that in a foreach statement, but I'm still trying to wrap my head around it all.
According to http://codex.wordpress.org/Function_Reference/is_page, you can't use it as a range. However, I'd use this function instead
$isPage = false;
foreach(range(5, 7) as $page)
{
if(is_page($page))
$isPage = true;
}
Related
I am using HTML5Blank as a starting theme and it comes with this function which returns 40 chars excerpt:
<?php html5wp_excerpt('html5wp_custom_post'); ?>
My main blog page is more complex, so I am using array to store values into it and echo them where I need them:
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $post_titles[$counter] = get_the_title($post->ID); ?>
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post_id); ?>
<?php $post_permalinks[$counter] = get_the_permalink($post->ID); ?>
<?php $post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid')); ?>
<?php $counter++; ?>
<?php endwhile; ?>
All other fields work and I can echo them, but I don't have any idea how to make excerpt work because it isn't echoing anywthing with:
<?php echo $post_excerpts[0]; ?>
First of all I notice, that you are using a variable called $post_id, which is not defined as far as I can see. You need to add a $post_id = $post->ID; before or alternatively you can use instead:
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post->ID); ?>
Maybe this already solves your problem. But to make sure, I will take it further.
I took a look at the functions.php of the HTML5-Blank-Theme: https://github.com/html5blank/html5blank/blob/master/src/functions.php
// Create 40 Word Callback for Custom Post Excerpts, call using html5wp_excerpt('html5wp_custom_post');
function html5wp_custom_post($length)
{
return 40;
}
So the function only returns the value of 40. I guess you can simply use the html5wp_excerpt() like this:
html5wp_excerpt(40);
Maybe there is something wrong with the html5wp_custom_post, so you can get rid of it to test this. And I also think, why use an addtional function, if it only returns a number... you can easily set it in the function call.
I don't know if this functions accepts an post ID as a parameter. So maybe it can only be used inside of a single.php page. I can't find a documentation about this, maybe you can do some research and find it out.
Here is another way to achieve it:
You can use the get_the_title() function that will accept the post id. Unfortunately, the get_the_excerpt() does not accept it.
So we first need to get the post object and then apply a filter to get the excerpt of the post. Do this by putting this code inside your while loop:
<?php $current_post = get_post($post->ID); ?>
You now have the current post as an object. In the next line, we apply the filter and save the result to the array at the right index position:
<?php $post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt); ?>
I wonder why there are so many php opening and closing tags, so you could make your code more readable:
<?php while ($the_query->have_posts()) : $the_query->the_post();
$current_post = get_post($post->ID);
$post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_titles[$counter] = get_the_title($post->ID);
$post_permalinks[$counter] = get_the_permalink($post->ID);
$post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid'));
$counter++;
endwhile; ?>
Just as an additional info, you could also use only the filters, should work with your post object:
$post_titles[$counter] = apply_filters('the_title',$current_post->post_title);
EDIT:
You can trim your excerpt to a certain length with mb_strimwidth (read more: https://www.php.net/manual/en/function.mb-strimwidth.php) :
$current_post = get_post($post->ID);
$trim_excerpt = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_excerpts[$counter] = mb_strimwidth($trim_excerpt, 0, 40, '...');
EDIT 2:
Maybe you should check if you are getting your post object. The fact that you are always seeing the same excerpt, maybe means you get the excerpt of the current page (not the post in your query).
$current_id = get_the_id();
$current_post = get_post($current_id);
I'm quite new to PHP, but have no idea how to word this question.
I want to return a coloured circle if the colour has been selected elsewhere.
So if silver has been ticked, show a silver circle.
If silver and gold have been ticked, show both.
There must be an easier way to code the below, instead of having to create lots of seperate if statements and call a variable each time?
elseif's wouldn't work here from my understanding.
Forgive me for my naivety!
The code I've written below:
<?php
// for iphone
$colours_available = get_field('iphone_colours_available');
// check
if( $colours_available && in_array('silver', $colours_available) ): ?>
<div class="colour-circle-title"><p>Silver</p></div>
<?php endif; ?>
<?php
// vars
$colours_available = get_field('iphone_colours_available');
// check
if( $colours_available && in_array('gold', $colours_available) ): ?>
<div class="colour-circle-title"><p>Gold</p></div>
<?php endif; ?>
Thanks so much for any help!
As mentioned in comments, you can iterate through your $colours_available array, and then use ucwords() to camel-case your p-tag's text:
<?php $colours_available = get_field('iphone_colours_available'); ?>
<?php foreach($colours_available as $color) :; ?>
<div class="colour-circle-title"><p><?php echo ucwords($color); ?></p></div>
<?php endforeach; ?>
Note: If you don't need to use the $colours_availble array elsewhere, you could eliminate that line and replace it wtih get_field('iphone_colours_available') in the loop. Also, this solution does not address sorting, which your previous solution does. For that, I would recommend looking at PHP's sorting functions, specifically uasort, uksort, and usort
I am trying to achieve an outcome that combines two plugins in WordPress.
Basically, I am using Easing Slider Pro and Advanced Custom Fields. When the website owner edits a page, I want them to be able to add a slideshow by simply entering the slideshow ID into an Advanced Custom Field called 'slider'.
This is how one would normally add the PHP to display a slideshow:
<?php if ( function_exists('easingsliderpro') ) { easingsliderpro( 5 ); } ?>
The 5 is an example of a slideshow ID that can be changed.
Here is the PHP for the advanced custom field:
<?php if( get_field('slider') ): ?><?php the_field('slider'); ?><?php endif; ?>
Both of these work fine by themselves. But I want a way to combine these two pieces of code so that in the page editor the website manager only has to enter the ID of the slideshow. I don't know a lot about PHP and I am often confused by it, but this was my initial attempt:
<?php if( get_field('slider') ): ?>
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) { easingsliderpro( <?php the_field('slider'); ?> ); } ?></div>
<?php endif; ?>
It didn't work, I am assuming because you're not allowed to have PHP code within PHP code. Is there any workaround that anyone knows that could make this achievable?
Many thanks.
Am I crazy? Can't you just:
AHA!
I think I see the confusion: the_field echoes the value out, so it gets passed to easingsliderpro() as just true, and displays the value.
You need to use a function that returns the value, so you can pass it to the next function.
In this case, it's get_field():
<?php if( get_field('slider') ): ?>
<div id="sliderframe">
<?php
if ( function_exists('easingsliderpro') ) :
easingsliderpro( get_field('slider') );
endif;
?>
</div>
<?php endif; ?>
See more in the documentation:
http://www.advancedcustomfields.com/resources/functions/get_field/
You shouldn't put php open close tags within a php open/close tag.
For your code above, this is valid:
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) {
easingsliderpro(the_field('slider'));
} ?></div>
I wrote this PHP code to put on a wordpress page template:
<?php
query_posts('showposts=10&cat=7');
while (have_posts()) : the_post();
?>
<li class="img-slider">
<?php the_content(); ?>
</li>
<?php endwhile; ?>
When I view the page I don't see any result and the right bar of the browser continue to reduce itself. I have understand that the code create an infinite loop.
Where I mistake?
Thanks
Firstly, you should not use query_posts. It's too invasive for simple loops, and messes with the entire WP_Query. Also showposts should be posts_per_page.
Secondly, it's hard to gauge what this issue is without more context. Perhaps pastebin your entire page, and edit it into your question. My guess is a loop within a loop, and should stop at like a 100 posts. (10 X 10) but if it's reset anywhere else if could very well go infinite!
Use this code instead to create loops:
$custom_query = new WP_Query( 'posts_per_page=10' );
if($custom_query->have_posts()) :
while ( $custom_query->have_posts() ) : $custom_query->the_post();
//global $post; // for stuff like $post->post_name
// Post stuff here
// the_title();
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
Look at the WordPress codex for more details. http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
You should use an if statement in your loop:
<?php
query_posts('showposts=10&cat=7');
if ( have_posts() ): while ( have_posts() ) : the_post();
?>
<li class="img-slider">
<?php the_content(); ?>
</li>
<?php endwhile; endif; ?>
Without further information, I would say that in each loop of the while statement, the function is returning the first row of the data? So each time the while loop executes you are calling the function again, which is returning the same row over and over again without actually iterating through the result set.
In this case I want to make the code work only if there are more than 5 replies.
<?php if ( bbp_topic_reply_count() > 5 ) : ?>
<?php query_posts('gdsr_sort=thumbs&post_type=bbp_reply&posts_per_page=2&post_parent='.$post->ID); ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php bbp_reply_author_link( array( 'type' => 'avatar' ) ); ?>
<?php bbp_reply_author_link( array( 'type' => 'name' ) ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endif; ?>
The replies are effectively being shown in the code below:
<h4><?php bbp_topic_reply_count(); ?></h4>
But it seems like its not working in the if statement.
Any suggestions?
Try using:
if ( bbp_get_topic_reply_count() > 5)
As with many templatey functions in various PHP libraries, there are two variants of this function. One, bbp_topic_reply_count(), automatically echoes the count, rather than returning it. The other, bbp_get_topic_reply_count() actually returns the value to you rather than echoing it.
May I suggest using
if (bbp_get_topic_reply_count() > 5):
The reason for this is that the function bbp_topic_reply_count() does not return the count value, instead it outputs this value. So when you compare the return value of bbp_topic_reply_count it is null and this produces the following statement
if (0 > 5) :
Which of course is always false.
I don't really know the coding conventions of WordPress but I'm sure you don't have to open and close the php tag <?php in every line.
bbp_topic_reply_count() isn't returning the reply count. It is only echoing it. This means you can't use it as a comparison because the function doesn't return a number to compare against. I am not familiar with the bbpress functions, but you will have to find an alternative.