I need some help to see where I am going wrong.
I am trying to add a page ID to this original function:
<?php if( $post->ID != '91' )
{
get_sidebar();
} ?>
>
to also exclude the ID 1267.
I am trying this, with no success.
<?php
$pageIDs_to_exclude=array("91","1267");
if( $post->ID != $pageIDs_to_exclude )
{
get_sidebar();
}
?>
Surely there must be a better way of doing this? Or what am I missing?
Thnaks for any help
/Anders
$pageIDs_to_exclude = array("91","1267");
// in_array will return false if it doesn't find $post->ID within the $pageIDs_to_exclude array
if( ! in_array($post->ID, $pageIDS_to_exclude) )
{
get_sidebar();
}
You are trying to directly compare $post->ID to $pageIDs_to_exclude, an array. As $post->ID is not an array (it is a string), this is not possible. Instead, see if $post->ID is in $pageIDs_to_exclude.
if (!in_array($post->ID, $pageIDs_to_exclude)) {
get_sidebar();
}
in_array() is a function that will return true if the object is found in the array.
You can use in_array of php. It will return true or false.
$pageIDs_to_exclude=array("91","1267");
if(!in_array($post->ID,$pageIDs_to_exclude))
{
get_sidebar();
}
Use the PHP function in_array() (http://php.net/manual/en/function.in-array.php) to search for a value in an array:
<?php
$page_ids = array("91", "1271");
if(!in_array($post->ID, $page_ids))
{
get_sidebar();
}
?>
Related
I'd want to edit the content of one of my posts using this way, but it's not working.
Does this filter affect the php output or the raw php file?
add_filter( 'the_content', 'multiple_string_replacements');
function multiple_string_replacements ( $contentt ) {
if ( is_single('15467') && in_the_loop() && is_main_query() ) {
$condition = true;
if($condition){
$urlsdothejob = "link.com";
$text = array(
"$variable1" => "$urlsdothejob",
"$variable2" => "$urlsdothejob",
);
$contentt = str_ireplace(array_keys( $text ), $text, $contentt);
};
}
return $contentt;
}
If you know the conditions that you're looking for, you should be able to write an IF statement to match.
if ($constantvalue == "anothervalue") {
// make local changes
}
Can you give an example of what you're trying to do? It's hard to understand the request with such little information.
Try like this hope its working for you.
while(have_posts()):
$ID = get_the_ID();
$data1 = get_post_meta($ID,'meta_key',true);
$data2 = get_post_meta($ID,'meta_key',true);
if($data1 == $data2) {
update_post_meta($ID,'meta_key','meta_value');
}
endwhile;
Which one is the best practice to use the argument values?
Below is the php code I wrote inside a Wordpress theme.
function kpn_page_banner($args) {
if (!$args['title']) {
$args['title'] = the_title();
}
!$args['title'] && $args['title'] = the_title();
$title = $args['title'] ? $args['title'] : the_title();
// ...
}
Is not recommended to access array keys without checking its existence. If you do in that way you can get some Notices.
if(!isset($args['title']) || ( isset($args['title']) && trim($args['title']) === '')) {
$args['title'] = the_title();
}
Also, don't ever try to reduce the size of an if statement by excluding some conditions that are so important, like isset is in your case.
Check out more http://php.net/manual/ro/function.isset.php
I need to run a function (show a modal) if a page DOES have a particular field value AND is not a certain page. It also needs to work if the page DOES NOT HAVE THE FIELD VALUE and it is not the certain page. They work individually like this:
//if page has field value:
<?php
$values = get_field( 'recast_video' );
if ( ($values) ) {
get_template_part('template-parts/popIn', 'none');
}
?>
//if it is this page, do not show:
<?php
if ( (!is_page('trading-education-webinars') ) ){
get_template_part('template-parts/popIn', 'none');
}
?>
How do I combine the two so that it shows (get_template_part) if the field has the value and is not the specified page
//this fails
<?php
$values = get_field( 'recast_video' );
if ( ($values) || (!is_page('trading-education-webinars') ) ){
get_template_part('template-parts/popIn', 'none');
}
?>
First thing to do is look for the common condition. No matter what, you don't want it to be a certain page, right? So first check that it's not that page:
$result = (!is_page('trading-education-webinars')) ? : ;
It also needs to work if the page DOES NOT HAVE THE FIELD VALUE and it
is not the certain page.
Then why check for the field value at all? No need, as long as it's not the page we checked for.
$result = (!is_page('trading-education-webinars')) ? get_template_part('template-parts/popIn', 'none') : return false;
You can change return false; to whatever you want to happen if the page IS 'trading-education-webinars'
EDIT: Clarifying the conditional:
Page X never gets served "content" (modal)
If NOT page X and has $values, show content
If NOT page X and NOT has $values, show some other content
$values = get_field( 'recast_video' );
$x = get_template_part('template-parts/popIn', 'none');
$y = 'some other content';
$return = (!is_page('trading-education-webinars')) ? (($values) ? $x; : $y ) : return false );
Use the && (AND) operator:
<?php
$values = get_field( 'recast_video' );
if ( ($values && !is_page('trading-education-webinars')) || (!$values && is_page('trading-education-webinars')) ){
get_template_part('template-parts/popIn', 'none');
}
?>
|| operator is true when at least one or both statements are true
When I submit a form with wrong id's, the url is set like this :
url-test.com/connexion/?login-failed
I want to trigger login-failed to make conditions.
Here's my code, but it does not work
$login_failed = $_GET['login-failed'];
if($login_failed) {
$error = 'Oups failed';
}
//My form
<?php wp_login_form(); ?>
<p><?php if(isset($error)) {echo $error;} ?></p>
Thanks for your help !
if(isset($login_failed)) {
$error = 'Oups failed';
}
You have to check that it's isset.
Your code isn't working, because you're store the value of $_GET['login-failed], but actually there's no value. If you change ?login-failed to ?login-failed=1, it should works.
Otherwise, you should check if variable isset, e.g:
$login_failed = isset( $_GET['login-failed'] ) ? true : false;
In the code below, the echo at the top returns true, but the echo at the bottom returns nothing. Apparently the code in between is causing me to lose a reference to the $_post variable?
<?php
echo "in category: ".in_category('is-sidebar'); //RETURNS TRUE
if (!get_option('my_hide_recent'))
{
$cat=get_cat_ID('top-menu');
$catHidden=get_cat_ID('hidden');
$myquery = new WP_Query();
$myquery->query(array(
'cat' => "-$cat,-$catHidden",
'post_not_in' => get_option('sticky_posts')
));
$myrecentpostscount = $myquery->found_posts;
if ($myrecentpostscount > 0)
{ ?>
<div class="menu"><h4><?php if ($my_sidebar_heading_recent !=="") { echo $my_sidebar_heading_recent; } else { echo "Recent Posts";} ?></h4><ul>
<?php
global $post;
$current_page_recent = get_post( $current_page );
$myrecentposts = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$catHidden",'showposts' => $my_recent_count));
foreach($myrecentposts as $idxrecent=>$post) {
if($post->ID == $current_page_recent->ID)
{
$home_menu_recent = ' class="current_page_item';
}
else
{
$home_menu_recent = ' class="page_item';
}
$myclassrecent = ($idxrecent == count($myrecentposts) - 1 ? $home_menu_recent.' last"' : $home_menu_recent.'"');
?>
<li<?php echo $myclassrecent ?>><?php the_title(); ?></li>
<?php
} ; if (($myrecentpostscount > $my_recent_count) && $my_recent_count > -1){ ?><li>View all</li><?php } ?></ul></div>
<?php
}
}
global $sitemap;
echo "in category: ".in_category('is-sidebar'); //RETURNS NOTHING
Variables in PHP are case-sensitive. This means that $_POST (a predefined variable) is not the same as $_post.
If you really did mean $_post, it's a terrible variable name, as it may confuse things later on.
Your foreach $myrecentposts declares a new variable $post. Use a different name for $post there.
The special variable that contains the current post is called $post, not $_post. But since that's the default value for in_category() anyway, you don't need to pass it that second parameter.
But you need to add a call to setup_postdata($post) inside that foreach loop to, well, setup the post data. Without it the "magic" functions like the_title() will just keep returning the post data for the original post. Note that that variable must be called $post.