If page parent equals ID (number)? - php

I have an if statement that is checking the ID of the page, using the following:
<?php if ( is_page(10) ) { ?>
How can I do something like if page parent is 10?

try something like this
global $post;
if ($post->post_parent == 10) {
echo "parent's id is 10";
}

$id = wp_get_post_parent_id( get_the_id() );
Now $id has parent page id

Get the current page object, then get its parent ID:
global $wp_query;
$currentPage = get_page($wp_query->get_queried_object_id());
if (is_page($currentPage['post_parent'])) {
}

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;

Check if previous post in the same category

I need something like this:
if (previous_post_is_the_same_category()){
...
} else {
...
}
How can I do this?
There is a function already in Wordpress to get the previous post
https://codex.wordpress.org/Function_Reference/get_previous_post
You can pass a boolean of true as the first parameter to return the previous post which is in the same category, you can then compare the post IDs to see if the previous post is also the previous post in the same category. Something like this...
$previousPost = get_previous_post();
$previousPostInSameCat = get_previous_post(true);
if($previousPost->ID === $previousPostInSameCat->ID){
...
}else{
...
}
That is very vague. You could have a variable that changes according to the category. Guessing you are pulling all your articles (or whatever) from a while f.ex;
$category = NULL;
while($result = mysqli_fetch_array($query)) {
if($result['category'] == $category) {
// Do something
} else {
// Do something else
}
$category = $result['category'];
}

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.

How to go to previous images in Instagram php api?

Im using PHP-Instagram-API to pick out images. Instagram only allows you to make 20 requests at a time and because of this i want to create a pagination. The only thing is, i can only figure out how to do this forward and not backwards. This is how I create my "next" link who successfully prints out the next 20 images:
$media = $current_user->getMedia( isset( $_GET['max_id'] ) ? array( 'max_id' => $_GET['max_id'] ) : null );
if( $media->getNextMaxId() ):
?>
<a href="instagram.php?max_id=<?php echo $media->getNextMaxId() ?>>Next</a>
<?php
endif;
How can i get the previous images? Then i have to select the ID of the last image in the previous page and put it as max_id. But I haven't been able to figure out a way to do this.
I solved it by storing max_id in sessions like this:
if (!isset($_GET['max_id'])) {
$page = NULL;
} else {
$page = $_GET['max_id'];
}
if (!isset($_GET['pg'])) {
$pg = 1;
} else {
$pg = $_GET['pg'];
}
if (isset($_SESSION['max_id'][$pg])) {
$page = $_SESSION['max_id'][$pg];
} else {
$_SESSION['max_id'][$pg] = $page;
}
$previous = $pg - 1;
And then the html like this:
Previous
Next

Losing reference to $_post variable?

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.

Categories