Losing reference to $_post variable? - php

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.

Related

create a function and modify a post on the wordpress site based on the post id

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;

How to get a single variable attached to the URI?

I don't seem to able to acquire a GET variable that is attached to the URI.
The codes are => at the controller
...
parse_str($_SERVER['QUERY_STRING'], $_GET);
....
$data['ti'] = $this->input->get('hamleno');
this->load->view('anasayfa', $data);
The codes are => at the view the link is
...
<div class="row"><?php for ($b=0; $b<=(count($hml)-1); $b++) { ?><?php echo $hml[$b]." "; ?> <?php } ?></div>
The link is working. I have added the
$config['uri_protocol'] = "PATH_INFO";
to the config.php file.
However I am not able to get the $ti variable
if ($ti){
$t=$ti;
}else{
$t = $this->input->post('t');
if (!$t) $t = 0;
if( $this->input->post('ileri') ) {
$t=$t+1;
if($t>($uz-1)){
$t=$uz-1;
}
} // Forward button was pressed;
if( $this->input->post('geri') ) {
$t=$t-1;
if($t<0){
$t=0;
}
} // Back button was pressed;
}
I'm not familiar with codeigniter, but I've always passed GET variables in this manner:
URL = www.site.com/folder/webpage.php?myvariable=myvalue
I would retrieve that value this way:
$x = $_GET['myvariable'];
or with codeigniter: (I think)
$x = $this->input->get('myvariable');
Tailored to your example, I would personally de-obfuscate your loop code just a little and instead of switching from PHP to HTML and back in one line, I would simply echo both from PHP like this:
(I also don't exactly understand the url you're using, so here is my approximate)
<?php
for ($b=0; $b<=(count($hml)-1); $b++)
{
echo '',$hml[$b],' ';
}
?>
I found out how Codeigniter solves the problem =>
$get = $this->uri->uri_to_assoc();
if(isset($get['hamleno'])){
$data['ti'] = $get['hamleno'];
}
This sends the $b assigned to $hamleno together with all the other stuff in $data.
Thank you all for your kind comments

Set variable name and value through Function

Hallo I am trying to create a function with two arguments.
Argument ONE should set a variable name and Argument TWO sets the name of the custom field which is the value of the variable.
I need a function for this, because the variable should get its value from a custom field of different pages depending on
- if { it's parent has the ID 77, get its own field }
- else { if it's parent doesn't have the ID 77, get its parent field }
This is what I tried, but does not work yet:
function variable_is_field($variable, $field) {
global $post;
if($post->post_parent == 77) // if page parent ID=77
$variable = get_field($field);
else
$variable = get_field($field, $post->post_parent;);
}
variable_is_field("$my-variable1", "my-custom-field1");
echo $my-variable1;
Any idea what's wrong with the code?
i can propose 2 solutions
if you need it only on this scope and one time like comment suggested so the code will be:
function variable_is_field($field) {
global $post;
$variable = null;
if($post && $post->post_parent == 77) // if page parent ID=77
$variable = get_field($field);
else
$variable = get_field($field, $post->post_parent;);
return $variable ;
}
$my-variable1 = variable_is_field("my-custom-field1");
echo $my-variable1;
else if you need it globally you can try somenthing like dis
global $my-variable1;
function variable_is_field($field) {
global $post;
global $my-variable1;
if($post && $post->post_parent == 77) // if page parent ID=77
$my-variable1= get_field($field);
else
$my-variable1 = get_field($field, $post->post_parent;);
}
variable_is_field("my-custom-field1");
echo $my-variable1;
Hope that this can help.

Multiple IF php with 2 else statements

I am trying to use the following code to 1st check if a user has a certain member level, then if they have a blog on the wp network. If they pass both those checks then a link is echoed, if they dont pass the first if check then another link is echoed. Also though, I am trying to check if they pass the first if but fail the second one then a different link is echoed. Here's the code I have now -
<?php
if(pmpro_hasMembershipLevel(array(2,4))) {
if(current_user_can( 'edit_posts' )) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
} else {
echo '<li>Register your Site</li>';
}
}
}
}
} else {
echo '<li>UPGRADE</li>';
}
?>
The code above echoes the register link when its suppose to but when the user has a blog, the register link shouldnt show but now it shows next to my site link. Any ideas?
EDIT
Free user sees a UPGRADE link
Premium Users without site see a REGISTER Link ( the membership array of 2,4 are the levels they have to be either one of )
Premium members with a site will see the MY SITE link.
EDIT
I was able to use the print_r and on the page where it's suppose to echo the register link -- Array ( [1] => stdClass Object ( [userblog_id] => 1 [blogname] => mysite.com [domain] => mysite.com [path] => / [site_id] => 1 [siteurl] => https://mysite.com [archived] => 0 [spam] => 0 [deleted] => 0 ) )
Looking at the Wordpress MU documentation, I would guess that the get_blogs_of_user always returns an array, so checking on the value of $blogs exists is always going to return true. In the following code, I suggest replacing the simple check on the existence of a value with a check to determine if the returned value is an array and, if so, whether it has elements or not:
<?php
if (pmpro_hasMembershipLevel(array(2,4))) {
if (current_user_can( 'edit_posts' )) :
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
/*Check if we got an array back and, if so,
check if it has elements*/
if ( is_array($blogs) && ( count($blogs) > 0 ) ) {
foreach ( $blogs as $blog ) :
if($blog->userblog_id != 1) {
echo '<li><a href="http://' . $blog->domain
. $blog->path
.'wp-admin/">My Site</a></li>';
}
endforeach; // end foreach loop
} else {
echo 'Register your Site';
} // end if $blogs
endif; // endif current_user_can
} else {
?>
<div>UPGRADE</div>
<?php
}
?>
Try this :
<?php if(pmpro_hasMembershipLevel(array(2,4))) {
if(current_user_can( 'edit_posts' )) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
}
}
} else {
echo 'Register your Site';
}
}
} else { ?>
<div>UPGRADE</div>
<?php } ?>
Give this one a shot. Even if it doesn't work in its current state, it should be easier to see the logic and figure out whats not working properly.
EDIT: Shamelessly stole #JustinPearce's method of checking if the user has a blog from his answer
<?php
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
// print_r($blogs);
$has_membership_level = pmpro_hasMembershipLevel(array(2,4));
$has_blog = ( current_user_can('edit_posts') && is_array($blogs) && count($blogs) > 0 );
$registerLink = 'Register your Site';
$upgradeLink = '<div>UPGRADE</div>';
function echoBlogLinks($blogs) {
echo '<ul>';
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
}
}
echo '</ul>';
}
if ($has_membership_level) {
if ($has_blog) {
echoBlogLinks($blogs);
} else {
echo $registerLink;
}
} else {
echo $upgradeLink;
}

Return an array value from included file in a function

Ok, stupid question, I guess...
I'm trying to do this:
File: pt.php
<?php $langlist = array ( "Car" => "Carro", "Big Car" => "Carro grande") ?>
File: index.php
<?php
$lang = 'pt';
if ($lang != 'en') include('locale/' . $lang . '.php');
function __($langstring){
if ($lang != 'en'){
echo $langlist[$langstring];
} else {
echo $langstring;
}
}
?>
But this doesn't work (Notice: Undefined variable: lang and langlist).
What am I doing wrong?
P.S.: I know using echo instead of return inside a function ins't correct, but I don't want do be using echo __(); every time I need to use this function...
$lang and $langlist are global variables, but they cannot be seen from within the function. Simply add the following as the first line of the function to gain access to them:
global $lang, $langlist;
Alternatively, you could access them as $GLOBALS['lang'] and $GLOBALS['langlist'] without using the global declaration.
Your syntax is wrong:
<?php $langlist = array { "Car" => "Carro", "Big Car" => "Carro grande"} ?>
should be
<?php $langlist = array("Car" => "Carro", "Big Car" => "Carro grande") ?>

Categories