Genesis and Custom Meta Box - php

I have been using WPAlchemy for my custom meta boxes. I can usually get the meat box values to display using something like <?php $custom_mb->the_value('summary'); ?> but I am having trouble displaying the data in a genesis child theme. If use the above example I can get the post to display but it is at the very top of the page, even above the header. So I attempted to hook into the genesis_post_content hook using this
add_action('genesis_post_content', 'meta_content');
function meta_content() {
echo "Hello World";
}
I can echo Hello World this way but receive errors when trying the first example in the function. Any help would be greatly appreciated.

I do not know Genesis and I do not use WPAlchemy ( I avoid "frameworks" like fire ) but my logic dictates that if you can see the value at the top of the page like you described ( above header ) than the method the_value() is doing a direct echo, where you would need a return value.
The internal wordpress core logic dictates that whenever you have an echo function ( e.g. the_title() ) you would potentially have an equivalent return function ( e.g. get_the_title() ) and it would get the same function name with an added get_ prefix.
If the same wordpress logic is applied to those "frameworks" , or in this case to the WPAlchemy class , so instead of
$custom_mb->the_value('summary'); // if this is direct echo
you should be able to do :
$custom_mb->get_the_value('summary'); // then this should be return
Note that I did not tested it ( not using those "frameworks" , remember ?? ) but if it is indeed the case with WPAlchemy , than you would not need to invoke the genesis filter ( which by itself seems a bit wrong becasue the_content filter should be about the_content and not about meta_data but not knowing genesis I can not really say )

Related

Put php logic function inside wordpress Shortcode

I have this code that displays an content to to users who have an paid subscribtion:
echo do_shortcode('[ihc-hide-content ihc_mb_who="5,7"] MY CONTENT HERE [/ihc-hide-content])
But I want to display inside this shortcode where it says MY CONTENT HERE, an logical function, like this:
if ( $a = $b ) { echo ($c); }
There is possibile to do this in some way?
P.S. I have made another shortcode, and put him inside like this:
do_shortcode('[ihc-hide-content ihc_mb_who="5,7"].do_shortcode('[my_shortcode]').[/ihc-hide-content])
But the 1st shortcode is not hide the content of 2nd shortcode.
Any help will be apreciated, ans sorry for my bad english!
If you're using an Enclosing Shortcode you should just be able to add the shortcode as a string to your do_shortcode() function. do_shortcode() will search the content that's passed to it and parse out any and all shortcodes that it's able to. Depending on how the ihc-hide-content shortcode is set up, it may not work quite properly, but you can get around that as well.
First and foremost, I would try just passing a single shortcode string to it:
do_shortcode( '[ihc-hide-content ihc_mb_who="5,7"][my_shortcode][/ihc-hide-content]' );
If that doesn't work (in all honesty, it should) - you can try and generate the output from my_shortcode into an Output Buffer like so:
// Turn on Output Buffering
ob_start();
// Output your shortcode
do_shortcode( '[my_shortcode]' );
// Turn off Output Buffering, and grab the content as a variable
$my_shortcode_output = ob_get_clean();
// Pass that complete output to the other shortcode
do_shortcode( '[ihc-hide-content ihc_mb_who="5,7"]'. $my_shortcode_output .'[/ihc-hide-content]' );

Execute a WordPress shortcode in Genesis breadcrumbs

I've created a shortcode [year] to show the current year dynamically. I enabled it in the post title by using this filter:
add_filter( 'the_title', 'do_shortcode' );
Shortcode executes successfully in the post title but displays as it is in the breadcrumbs. I'm using Genesis Framework so is there any way to make it work in the Genesis breadcrumbs too?
Using the_title filter like you have done, should actually work just fine (I just tested and confirmed it's working on a local site of mine).
As a matter of fact it can sometimes be a problematic filter because people expect it to filter just the page title, but in reality it filters the_title whenever it's pulled from the database, including the Page Title, Breadcrumbs, Menu Item Name, etc.
That said, if it's not working for some reason, Genesis has a handy Genesis_Breadcrumb class. Most notably it has the genesis_breadcrumb_args filter that lets you remove the "You are here:", etc. Provided the theme you're using is implementing Genesis Breadcrumbs and not some custom ones, you actually have access to the lesser utilized genesis_build_crumbs filter.
The build_crumbs() method returns an array of Page/Post Names that you can modify using a simple foreach loop or array_map.
Take the following function:
function so52299149_breadcrumbs( $crumbs ) {
$new_crumbs = array_map( function($val){
return do_shortcode( $val );
}, $crumbs);
return $new_crumbs;
}
add_filter( 'genesis_build_crumbs', 'so52299149_breadcrumbs' );
What it will do is run each element in the $crumbs array through the do_shortcode() function, giving you the desired output. Again, the way you have it, add_filter( 'the_title', 'do_shortcode' ); should be working - so check how the Child Theme you're using is implementing breadcrumbs, especially if the function above doesn't work - if that's the case there's likely something else at play.

WordPress shortcode fails in Custom theme

I'm creating a theme and i want the user to be able to use shortcodes.
Right now it outputs [the_shortcode] and I think I know why but don't know how to fix it.
I load the content of the page not in the conventional way.
Preferably the way is to load the_content() function. But the way my template is designed it loads content based upon placement in the hierarchy of pages.
So a parent has a different look then a child.
To do this I load the content with a foreach loop and echo out $grandchild->post_title. The page being a grandchild of a parent.
Now the way to fix this, according to the internet, is to use the apply_filters() function.
The function expects two parameters and I have no clue on how to fill them:
function apply_filters( $tag, $value )
This is my function for the shortcode:
function output_function(){
return 'Knees weak, arms are heavy';
}
add_shortcode('output', 'output_function');
The shortcode is placed in a page post like this: ['output']
Any thoughts on how to output page content through the filter?
What you want is the_content
$content = 'some string that has a [output] shortcode';
echo apply_filters('the_content', $content);
This filter will make sure all $content is parsed like the WordPress editor.
The same like the_content().

Wordpress template Echo a php string

I'm working on a page template and I'm having trouble working out how I can echo a string of php. Basically I have set up a custom field inside Wordpress for the page called Slider Code, and it asks the user to paste in the slider code given to them so that the appropriate slider is displayed on the page. The slider code given looks like this: [soliloquy id="82"]
The custom field plugin gives me this code to use in the template to echo out whatever is in that field, which looks like this: <?php echo CFS()->get('slider_code'); ?>
In WordPress there is a php function called do_shortcode which enables me to use that code, so it would look like this: <?php do_shortcode('[soliloquy id="82"]'); ?> and the slider would be displayed. So is there any way I can merge that <?php echo CFS()->get('slider_code'); ?> string with the do_Shortcode string? If not, does anyone have any alternative solutions to this issue? I can only think of having to make separate page templates for each individual slider.
EDIT: I have just discovered that the do_shortcode function does not actually work at all with the soliloquy shortcode. It does give me the option of using this code: if ( function_exists( 'soliloquy' ) ) { soliloquy( '82' ); }. However, when I paste this code into the custom field as is, the <?php echo CFS()->get('slider_code'); ?> tag just echos out that text. If I wrap the code in the custom field in <?php and ?> it just comments the code out inside my inspector. How do I get around this?
Check this out.
$slider = CFS()->get('slider_code');
call_user_func('do_shortcode', $slider);
I don't understand why you can't just run do_shortcode(CFS()->get('slider_code')).
Ok. Then try this one. Paste this code in the template file.
<?php
if ( function_exists( 'soliloquy' ) ) {
soliloquy( CFS()->get('slider_code') );
}
?>
And for slider_code input just enter '82'. If you are unable to see what is the template file you have to paste the above given code, then try to see WordPress template conventions.

Wordpress Conditional if is_single

Im trying to use a conditional statement on my single.php page.
What I want to do is, if it is of the Custom Post Type current-products, use a specific single -product.php template page, if not (ie a standard blog post) use the default single.php page.
I think Im using the right statement, but don't know what to do afterwards (to get the template to be used):
if ( is_single( 'current-products' == get_post_type() ) {
// If the post type is "Current Products" use this template...
// Please help me out on this bit
} elseif ( is_single() ) {
// If not, use the standard one...
// please help me out on this bit
}
I think that's right...?
WordPress automatically uses a different template page for different post types, if your post type is called products the files should be named single-products.php. You can read more about it from there Codex
In the same way single posts and their archives can be displayed using the single.php and archive.php template files, respectively,
single posts of a custom post type will use single-{post_type}.php
and their archives will use archive-{post_type}.php
where {post_type} is the $post_type argument of the register_post_type() function.
You can use is_singular() function, according to WordPress documentation: Ref Link
This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
True when viewing a post of the Custom Post Type book.
is_singular('book');
This will accomplish your task.
I believe if you want to use for posts try this:
is_singular('post');
Thanks

Categories