How to pass PHP template through shortcode - wordpress

I want to create an overview of my latest blog posts and display this information on a certain part of my homepage. My WordPress theme already has a PHP template in the desired format, which means I would not have to create the actual design/style myself.
As I want this information to be shown in one particular part, I was thinking about creating a shortcode which inserts the information then. I have created shortcodes in WP before, however they always populated simple information.
I am running into the problem now that I do not quite know how I can populate a PHP template via a shortcdode. Before the shortcode function was like similar to this one:
function display_my_shortcode($atts, $content, $tag){
//if called from the 'my_primary_shortcode' shortcode
if($tag == 'my_primary_shortcode'){
return 'This is the primary shortcode';
}
//if called from the 'my_secondary_shortcode' shortcode
else if($tag == 'my_secondary_shortcode'){
return 'This is the secondary shortcode';
}
//default
else{
return 'This is something else';
}
}
add_shortcode('my_primary_shortcode','display_my_shortcode');
add_shortcode('my_secondary_shortcode','display_my_shortcode');
My problem is, that the output of the template is still with php - the template I want to export would look like this:
<div class="blog_holder blog_small_image">
<?php $counter = 0; ?>
<?php if(have_posts()) : while ( have_posts() ) : the_post(); ?>
<?php
if(get_the_category(get_the_ID()) != 'test123')
{
continue;
}
else
{
if($counter < 6){
get_template_part('blog_small_image', 'loop');
$counter++;
}
}
?>
<?php endwhile; ?>
<?php if($qode_options_theme13['pagination'] != "0") : ?>
<?php pagination($wp_query->max_num_pages, $blog_page_range, $paged); ?>
<?php endif; ?>
<?php else: //If no posts are present ?>
<div class="entry">
<p><?php _e('No posts were found.', 'qode'); ?></p>
</div>
<?php endif; ?>
</div>
It should create a list of 5 blog posts of the category 'test123'. How would I include this output into a shortcode though. what would be the "return" value?
Thank you guys so much!

Related

Wordpress: function to replace content for custom post status

I created custom post status "blocked" via register_post_status (like this register_post_status not showing post_status in status dropdown).
Now I want to replace content on posts with this status via functions.php.
I tried something like this
function block_by_status ($post_object) {
global $wp_query;
if( 'blocked' == $wp_query->post->post_status ) {
return 'This post is blocked';
}
}
add_action('the_post','block_by_status');
But it doesn't work, please somebody help?
So for the content you can do something as simple as:
add_filter('the_content', 'block_by_status');
and to hide the title you can use:
add_filter( 'the_title', 'block_by_status', 10, 2 );
If you want to go further than that, I suggest editing the template in the theme folder.
For pages you need to edit page.php and for posts you need to edit posts.php or singular.php, depends on the theme you are using.
So you can do something like this:
In the functions.php file:
function block_by_status ($post_object) {
global $wp_query;
if( 'blocked' == $wp_query->post->post_status ) {
return true;
}
}
and then in the files I referenced above (Depends on your theme):
<?php if (!block_by_status()) { ?>
<?php while (have_posts()) : the_post(); ?>
<h1><?php the_title() ?></h1>
<?php the_content() ?>
<?php get_sidebar() ?>
<?php endwhile; ?>
<?php } else {
echo 'This post is blocked';
} ?>
So you use your function as a boolean and just hide what you dont want users to see.

Wordpress custom post type to cart not update it

I’m working on one client site, the previous developer uses WP custom post type build a product collection page, kind like woocommerce, but only can add to cart to request a quote to the company, no transaction on the cart application.
I have trouble with I can add to cart is fine, but when I view the cart page. the item won’t show up unless I refresh the page. same problem with delete item in the cart.
There’s a cookie set in the code, which I not familiar with, thinking might be the issue.
I thought I move the to the top will fix some issue, but the cart won't work. I got some unexpected end error on WP debug tool, so I tried to use PHP checker to see how many } I miss and add it back, and still not working.
Thank you so much for the help!
—–-- More Info ———-
Wordpress version: 4.9.7
PHP server: 5.6
Theme development date: unknown (Twenty thirteen theme modify version, never update)
—–-- code ———-
<?php
if(get_query_var('remove') > 0){
$r = get_query_var('remove');
$d = get_query_var('d');
if (isset($_COOKIE['cart'])) {
$cart = json_decode(base64_decode($_COOKIE['cart']), true);
unset($cart[$r][$d]);
if(empty($cart[$r])){
unset($cart[$r]);
}
if(empty($cart)){
setcookie("cart", base64_encode(json_encode($cart)), time() - 3600, '/');
} else {
setcookie("cart", base64_encode(json_encode($cart)), time() + 2592000, '/');
}
header('Location: '.get_the_permalink());
die();
} else {
header('Location: '.get_the_permalink());
die();
}
}
?>
<?php get_header();?>
<?php if(isset($ok) && $ok == 1){?>
<p class='after_cart_text'>Thank you for the opportunity to quote your project! We will endeavour to respond within 24 hours.</p>
<?php } else { ?>
<?php if(isset($ok) && $ok == -1){?>
<div class="alert alert-danger" role="alert">Please check the the captcha form.</div>
<?php } ?>
<div class="baners_categories">
<?php
$rows = get_field('banery_categories', 'options');
if($rows){
shuffle( $rows );
$image = $rows[0]['picture_categories'];
?>
<img src="<?php echo $image; ?>" />
<?php
}
?>
</div>
</fieldset>
<fieldset>
<!-- Form Name -->
<p class="legend_pe"><legend>Product Information</legend></p>
<?php if(isset($_COOKIE['cart']) && !empty($_COOKIE['cart']) && $post->post_name == 'cart'){
$cart = json_decode(base64_decode($_COOKIE['cart']), true);
//var_dump($cart);
$i = 0;
if (is_array($cart)) {
foreach($cart as $k => $row){
foreach($row as $k2 => $item){
//var_dump($item);
?>
<!-- form section code I comment it out -->
<?php }?>
<?php }?>
<div class="col-sm-4" style="margin-bottom:15px;">
<p><strong>Your cart is currently empty.</strong></p>
</div>
<?php }?>
<?php }?>
<?php }?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Thank you so much for any helps or tips!
I add this <?php wp_reset_postdata(); ?> end of the while loop and work now. More info here:https://codex.wordpress.org/Function_Reference/wp_reset_postdata

Using wp_get_post_tags instead of the_tags

I'm working in one theme, and i trying to sumit to wordpress theme page, but i have the follow massages:
REQUIRED: This theme doesn't seem to display tags. Modify it to display tags in appropriate locations.
So i found that i don't have the_tags function but i have my own code with the wp_get_post_tags because i make my own html, so i dont know how to fix this problem.
This is my function
function basico_create_link($array_object_terms, $type) {
$link = '';
foreach ($array_object_terms as $object_terms) {
$link.='<a href="';
if ($type == 'category') {
$link.=get_tag_link($object_terms->term_id);
} else if ($type == 'tag') {
$link.= get_category_link($object_terms->term_id);
}
$link.='" > ' . $object_terms->name . '</a>,';
}
return substr($link, 0, -1);
}
And i used this way
basico_create_link(wp_get_post_tags(get_the_ID(), array('fields' => 'all')), 'tag');
Function the_tags() should be somewhere within The Loop. You can put it in your theme's index.php, although I myself would rather opt for single.php (since the tags are needed in blogposts).
Therefore, I start the loop, add some other content as needed and then I put the functions needed to get a blogpost properly displayed. In general, it could go like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article>
<!--some additional html/php content as needed -->
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php the_category(' '); ?> || <?php the_tags(); ?>
<!--et cetera-->

How to Get Current Custom Post Type Associated Taxonomy Term

I have a single.php file like below
<?php get_header(); ?>
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
echo '<div">';
the_content();
echo '</div>';
} // end while
} // end if
?>
<?php get_footer(); ?>
now I need to get Current Custom Post Type Associated Taxonomy Term as link on the top of the page so if users click on the link the page navigate to taxonomy.php.
Can you please let me know how to do this?
Thanks
You can use get_the_term_list within your loop to get the associated taxonomy term links:
global $post
$terms = get_the_term_list($post->ID, 'your_taxonomy');
echo $terms;
This was a good solution for me as I only ever had a single taxonomy term associated to each post.

Wordpress custom field if else for showing data

In my theme I have custom field 'ext_url'. Now I want to get post url If I don't add 'ext_url' custom field.
I tried
<?php if(get_post_meta($post->ID, 'ext_url', true)): ?>
<?php else : ?>
<?php the_permalink(); ?>
<?php endif; ?>
When I don't add custom field, it is showing post URL. But, when I add custom field it is showing blank. Any Solutions?
Update: I've found a code
<?php
$url = get_post_meta($post->ID, 'ext_url', true);
if ($url) {
echo "<p><a href='$url'>External URL</a></p>";
}
?>
but, it is working for showing custom field. How can i add else function here? Sorry, I am not experienced in PHP.
If you want it to display the data from the custom field, you should add a line to echo the results.
<?php
$ext_url = get_post_meta( $post->ID, 'ext_url', true );
if ( $ext_url ) {
echo $ext_url;
} else {
the_permalink();
}
?>

Resources