I'm working with the function have_posts from WordPress codex. What I'm trying to do is:
while ($content_query->have_posts()) {
$content_query->the_post();
if(strpos(the_title(),'Garajes Gran')===false){
global $post;
include($item_template);
}
}
So I get a full list of names with Garajes Gran names included and not the list without the name Garajes Gran, What I'm doing wrong?. Also instead of look for the first position where the String Garajes Gran it's, it prints all names, so why if(strpos(the_title(),'Garajes Gran')===false){ doesn't do what it means?.
Thanks in advance.
Try the below code used 'get_the_title()' instead of 'the_title()'.
if(strpos(get_the_title( get_the_ID() ),'Garajes Gran')===false){
global $post;
include($item_template);
}
I hope this is working for you.
Related
I have a function in php as shown below:
function hello_world( $post ) {
echo '<pre>'; print_r($post); echo '</pre>'; // Line A
echo '<pre>'; print_r($post->post_meta); echo '</pre>'; // Line B
return $post->post_meta;
}
I have added Line A and Line B in the function above for debugging purposes. In the function hello_world above, Line A returns list of all posts whereeas Line B doesn't return anything.
I am wondering what changes I need to make in the wordpress or php code above so that Line B returns list of posts.
Post_meta doesn’t contain a list of posts, it has extra information that you, a plugin or your theme are adding to a post using add_post_meta.
To get the post meta, you use the get_post_meta function.
To get all values you can do this:
$allpostmeta = get_post_meta ($post->ID);
You can also get a specific field like this:
$mymetavalue = get_post_meta ($post->ID, ‘your_meta_key’);
I have a dropdown select field to select a custom posttype within the woocommerce product edit screen and need to output the ID of this selected post.
This code works on my frontend but having issues to get it working with other plugins (here wplister).If i hardcode the selected post ID all works fine.
function psg_table_frontend_shortcode($atts, $content = null) {
global $psg_global;
/* START creating HTML Object */
ob_start();
?>
<p>
<?php
echo "The ID of the selected sizing Guide (cpt) inside the Product : " .$psg_global['psg-selected-sizing-guide-on-productpage'];
?>
</p>
$output = ob_get_clean();
/* END creating HTML Object */
return $output;
}
add_shortcode('psg_frontend', 'psg_table_frontend_shortcode');
Spoke to the support of wplister but they said it is probably an issue with the reduxframwork and the global variable.
Any idea?
if you are in a function, who knows if your psg_global is even in scope yet... so try this:
$psg_global= get_option( 'psg_global'); // or whatever your opt name is
$productpage = $psg_global['psg-selected-sizing-guide-on-productpage'];
echo $productpage;
also, might want to use 'psg_selected_sizing_guide_on_productpage' with underscores instead of 'psg-selected-sizing-guide-on-productpage' because that way you can do this:
extract($psg_global)
and all your vars would be in scope.
for example:
echo $psg_selected_sizing_guide_on_productpage
I would like to use a php variable to input the value as the shortcode name for wordpress.
Basically I have an option where people can create an options and give it a shortcode name.
Something like the following:
Is this possible?
$shortcode_name = "name_here";
function $shortcode_name ($atts) {
// code here
}
add_shortcode ($shortcode_name , $shortcode_name);
Thanks
In PHP 5.3+ you have closures.
$shortcode_name = 'awesome';
${"{$shortcode_name}_fn"} = function($atts) {
print('Awesome shortcode');
};
add_shortcode($shortcode_name, ${"{$shortcode_name}_fn"});
You can also have variable variable names, in this case, the variable that contains the function is named: awesome_fn.
I want to develop a plugin to add the ability to do something with shortcode. I want it to function like this:
[shortcode]Content[/shortcode]
Here is the code I use:
function quote( $atts, $content = null ) {
return '<div class="right text">"'.$content.'"</div>';
}
add_shortcode("quote", "quote");
The $content variable, which returns the value of the shortcode, in this case Content, cannot be used outside of the function. I want to use it on some other part of the PHP code, but I can't get it to work. I am not experienced with PHP, so if you have any solution, please try to be as clear as possible.
Thanks.
You'd have to declare it as a global variable otherwise it's scope (where you can access it) is limited to the function you're using it in.
function quote( $atts, $content = null ) {
global $content;
return '<div class="right text">"'.$content.'"</div>';
}
add_shortcode("quote", "quote");
echo "Using content somewhere else $content";
FYI though, it can lead to potential problems. $content, for example, is a pretty common variable and could conflict if the same variable is being used elsewhere. You'd be better off giving it a unique name like: global $my_global_content = $content. Then use $my_global_$content in the other areas of your code.
I tried to solve something but struggling the whole morning.
I have my WordPress posts already in the state that they can have a parent post.
done by:
add_action('init', 'add_hierarchy_to_posts');
function add_hierarchy_to_posts() {
add_post_type_support( 'post', 'page-attributes' );
$obj = get_post_type_object( 'post' );
$obj->hierarchical =true;
remove_theme_support( 'post-formats' );
}
I need my post numerized in chapters so thats
1. Introduction
2. Subject is great
2.1 why great
2.2 why subject
2.2.1 better
2.2.2 worse
2.2.2.1 in best case
3. End
The problem is now, how to count subchapters.
The function is here so far, and it produces at least1.02.03.03.13.2
global $chapter,$subchapter,$subchapter1,$subchapter2,$depper;
function is_child_of($postID) {
global $post;
$ico=$post->post_parent;
return $ico;
}
function find_out_chapter_number($id){
global $chapter,$subchapter,$subchapter1,$subchapter2,$subchapter3,$depper;
if(is_child_of($id)==0){
$chapter++;
$subchapter=0;
}
else if(is_child_of($id)==$depper){
$subchapter++;
}
$depper=$id;
$ico=is_child_of($id);
//$id. " - ".$ico." - ".
return $chapter.".".$subchapter.".".$subchapter1.".".$subchapter2;
}
The $depper variable stores the post id before.
I need it deeper and I thought its maybe something with recursion but I don't know.
The function is called from the wordpress loop with$kapitel = find_out_chapter_number(get_the_ID()); and echoed before the_title();
Please try this plugin... this will help.
Page List plugin
if you still facing the same problem. please leave your comment. i will give you another solution.