How can I add Wordpress plugin hook to index.php? - php

I'm using a plugin called Category Thumbnail list. I normally would place the hook [categorythumbnaillist 100] on my Wordpress pages. How can I add this to an actual php page?
http://wordpress.org/extend/plugins/categoy-thumbnail-list/
Plugin code:
$categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
if ($categoryThumbnailList_Order == '') {
$categoryThumbnailList_Order = 'date';
}
$categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );
if ($categoryThumbnailList_OrderType == '') {
$categoryThumbnailList_OrderType = 'DESC';
}
$categoryThumbnailList_Path = get_option('siteurl')."/wp-content/plugins/categoy-thumbnail-list/";
define("categoryThumbnailList_REGEXP", "/\[categorythumbnaillist ([[:print:]]+)\]/");
define("categoryThumbnailList_TARGET", "###CATTHMBLST###");
function categoryThumbnailList_callback($listCatId) {
global $post;
global $categoryThumbnailList_Order;
global $categoryThumbnailList_OrderType;
$tmp_post = $post;
$myposts = get_posts('numberposts=-1&&category='.$listCatId[1].'&&orderby='.$categoryThumbnailList_OrderType.'&&order='.$categoryThumbnailList_Order);
$output = '<div class="categoryThumbnailList">';
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail() ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
}
endforeach;
/*
$output .= '</div>';
$output .= '<div class="categoryThumbnailList_clearer"></div>';
return ($output);
$output = '';
$post = $tmp_post;
setup_postdata($post);
*/
$output .= '</div>';
$output .= '<div class="categoryThumbnailList_clearer"></div>';
$post = $tmp_post;
wp_reset_postdata();
return ($output);
$output = '';
}
function categoryThumbnailList($content) {
return (preg_replace_callback(categoryThumbnailList_REGEXP, 'categoryThumbnailList_callback', $content));
}
function categoryThumbnailList_css() {
global $categoryThumbnailList_Path;
echo "
<style type=\"text/css\">
#import url(\"".$categoryThumbnailList_Path."categoy-thumbnail-list.css\");
</style>
";
}
add_action('wp_head', 'categoryThumbnailList_css');
add_filter('the_content', 'categoryThumbnailList',1);
?>

i think you want to add short-code in template file. place this line in your index.php
<?php echo do_shortcode( '[categorythumbnaillist 100]' ); ?>

Related

Wordpress shortcode issue

i have this code for shortcode
function custom_query_shortcode( $atts ) {
// EXAMPLE USAGE:
// [postcat name="Lorem"]
// Defaults
$atts = shortcode_atts(array(
'name' => ''
), $atts, 'postcat');
// Reset and setup variables
$output = '';
$title = '';
$link = '';
$featImg = '';
// the loop
$argsSC = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page'=> -1,
'category_name' => $atts['name']
);
$querySC = new WP_Query( $argsSC );
if ( $querySC->have_posts() ) {
$output .= '<div class="row" style="margin-top: 30px;">';
while ( $querySC->have_posts() ) {
$querySC->the_post();
$title = get_the_title(get_the_ID());
$link = get_permalink(get_the_ID());
$featImg = ( has_post_thumbnail() ? get_the_post_thumbnail_url(get_the_ID(), 'large') : get_template_directory_uri() . '/imgs/No_Image_Available.gif' );
$id = get_the_ID(get_the_ID());
$chartL = get_field( 'chart' );
$storyL = get_field( 'story' );
// output all findings - CUSTOMIZE TO YOUR LIKING
//$output .= "<div><img src='$featImg'><a href='$link'>$title</a></div>";
$output .= '<article id="post-'.$id.'" class="csseco-tssf-t col-12 col-md-6">';
$output .= ' <div class="standard-featured bg-img-el" style="background-image: url('.$featImg.')">';
$output .= ' <a class="standard-featured-link" href="'.$link.'"></a>';
$output .= ' <div class="cat-tssf">';
if ( !empty($chartL['url']) ) {
$output .= ' Charts';
}
if ( !empty($storyL['url']) ) {
$output .= ' The Story';
}
$output .= ' </div>';
$output .= ' <h3 class="ttl-tssf">';
$output .= ' <span class="dtable">';
$output .= ' <span class="dtable-cell">';
$output .= ' <a href="'.$link.'" rel="bookmark">';
$output .= $title;
$output .= ' </a>';
$output .= ' </span>';
$output .= ' </span>';
$output .= ' </h3>';
$output .= ' </div>';
$output .= '</article>';
}
$output .= '</div>';
} else {
$output .= "nothing found.";
}
return $output;
wp_reset_query();
wp_reset_postdata();
}
add_shortcode("postcat", "custom_query_shortcode");
In the backend, when i press "Add new category" to make a new category(duh!) does not work. The category its added but the page does not refresh. I'm pretty sure it's something with this code, but i really dont know what... if i dont include this file in functions.php categories and tags works perfectly...
Please help and thanks in advance!
Can't comment, so I'll post it as an answer.
Please look at:
return $output;
wp_reset_query();
wp_reset_postdata();
The code after the return will not be executed I believe. So change it to:
wp_reset_query();
wp_reset_postdata();
return $output;
Maybe this will solve your issue?

Why wp_video_shortcode inside shortcode echoes when calling the wrapping shortcode?

I have a shortcode, which returns html created like this:
public function get_video_grid($the_query) {
$html = '';
if ( $the_query->have_posts() ) {
$count = 0;
while ( $the_query->have_posts()) : $the_query->the_post();
$attachment = get_attached_media( 'video' );
$post_ID = get_the_ID();
global $wpdb;
$video_url = wp_get_attachment_url($post_ID);
$video_attr = array(
'src' => $video_url
);
$item = $wpdb->get_row(
"
SELECT video_start_time
FROM " . $wpdb->prefix . "lc
WHERE attachment_id = $post_ID
",
ARRAY_A
);
$time = $item['video_start_time'];
$html .= '<div class="video-thumb-wrapper">';
$html .= '<h4 class="grid-title">' . get_the_title() . '</h4>';
$html .= '<span class="grid-timestamp">' . gmdate('d.m.Y H:i', strtotime($time)) . '</span>';
$html .= '<div class="aspect-ratio">';
$html .= wp_video_shortcode( $video_attr );
$html .= '</div>';
$html .= '</div>';
$count++;
endwhile;
}
return $html;
} // get_video_grid
However, if I am using the shortcode either in article or in do_shortcode(), the video elements are rendered outside of the containers on some pages. On some WP installations this works just fine. The version of WP does not seem to affect.
If I try to store the html string to variable like this:
<?php
/**
* Template Name: Modular Page
*/
get_header(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post();
if ( post_password_required() ) {
echo get_the_password_form();
} else {
lobo_modular_content( $post );
}
endwhile;
$content = do_shortcode("[lilicast_list]");
get_footer(); ?>
the videos are still rendered. Notable here is that nothing should be echoed at this point, yet still I have the elements in my html. As far as I know, all I am asking is the wp to return me a string.
Why is the wp_video_shortcode() echoed before anything else? Why the behaviour is not consistent with just adding the shortcode inside of the post?

Wordpress - Include static image after every X post through wp_query

I have a somewhat complicated design.
I want to use a CPT to output it. This is my CPT in the wp_query:
/**
* Management Team Shortcode
**/
function team_query() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'management-team',
'order' => 'DESC',
);
$posts = get_posts( $args );
if ( !empty( $posts ) ) {
$flag = 0;
foreach ($posts as $counter => $p) {
$counter++;
if ( $flag <= 2 ) {
$flag++;
}
$role = get_field( "role" );
$name = get_field( "team_member_name" );
$bio = get_field( "bio" );
$profile = get_the_post_thumbnail_url( $p->ID, 'full' );
$flip = get_field( "flip_content" );
$html_out = '<article class="team-member">';
// Do stuff with each post here
if ( $flag % 2 == 0 ) {
//add image after second post like
$html_out .= '<img src="http://www.ankitdesigns.com/demo/rawafid/wp-content/themes/rawafid-systems/assets/img/mt-1.jpg" alt="Safety Whistle" />';
}
if ( $counter % 6 == 0 ) {
$flag = 0;
//add image after sixth post like
$html_out .= '<img src="http://www.ankitdesigns.com/demo/rawafid/wp-content/themes/rawafid-systems/assets/img/mt-2.jpg" alt="Safety Whistle" />';
}
$html_out .= '<div class="meta-team"><h6>' . $role . '</h6>' . '<h4>' . $name . '</h4>' . '<p>' . $bio . '</p></div>';
$html_out .= '</article>';
}
} else {
// No results
$html_out = 'No Management Team Members Found.';
}
return $html_out;
}
add_shortcode( 'show_management_team', 'team_query' );
After 2 posts I'd like to add a static img and after 4 posts another static and after 2 more posts another static img, etc.
I'm open to suggestions on a better approach.
I'm trying to think of an alternative method. Maybe using Visual composer to build a grid?
Hey Darren here is the logic, I think that you can modify my functions and implement it in your code.
/**
* Management Team Shortcode
**/
function team_query() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'management-team',
'order' => 'ASC',
);
$posts = get_posts( $args );
if ( !empty( $posts ) ) {
$flag = 0;
foreach ($posts as $counter => $p) {
$counter++;
if ( $flag <= 2 ) {
$flag++;
}
$title = get_the_title($p->ID);
$link = get_the_permalink($p->ID);
$featured_img = get_the_post_thumbnail_url( $p->ID, 'full' );
$html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">';
// Do stuff with each post here
if ( $flag % 2 == 0 ) {
//add image after second post like
// $html .= <img src="css/images/myimage1.png" alt="" />
}
if ( $counter % 6 == 0 ) {
$flag = 0;
//add image after sixth post like
// $html .= <img src="css/images/myimage2.png" alt="" />
}
$html_out .= '<div class="container"><div class="meta-project"><h6>Latest Project</h6>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a></div></div>';
$html_out .= '</article>';
}
} else {
// No results
echo "Nothing to show";
}
return $html_out;
}
Cheers :)

WordPress: add shortcode into a concatenate variable

I'm trying to make this work but it's not working. What's wrong with this piece of code?
add_filter( 'the_content', 'addslider_hook' );
function addslider_hook ( $content ) {
if ( is_page('outsourced') ) {
$slide = '[rev_slider outsourced]';
$hook_slide = do_shortcode( $slide );
$content .= "<div class='page-slider'>";
$content .= $hook_slide;
$content .= "</div>";
}
return $content;
}
You have a extra } at the bottom of the code.You can try this
if ( is_page('outsourced') ) {
$content .= "<div class='page-slider'>";
$content .= do_shortcode('[rev_slider outsourced]');
$content .= "</div>";
echo $content;
}

Wordpress outputting tags in a loop

I'm trying to output all the tags associated with a custom post type via shortcode and it only appears to be bringing in 1 tag inside the $output.
outside the $output the code is fine.
code is:
function display_stores() {
$args = array( 'post_type' => 'stores', 'posts_per_page' => 5 );
$success = new WP_Query( $args );
$output = '';
while( $success->have_posts() ) {
$success->the_post();
$tags = get_the_tags($post_ID);
foreach($tags as $tag) {
return '<li>'. $tag->name . '</li>' ;
}
$output .= sprintf( "<div class='story-content left'>" );
$output .= sprintf( "<h2>%s</h2>", get_the_title() );
$output .= sprintf( '%s</div>', get_the_content() );
$output .= sprintf( "Button");
$output .= sprintf( "<div class='story-tags right'>" );
$output .= sprintf( "<h4>Areas</h4><ul class='ul-arrows'>" );
$output .= sprintf( $tags );
$output .= sprintf( "</ul></div><hr>" );
}
wp_reset_query();
return $output;
}
add_shortcode( 'display_stores', 'display_stores' );
foreach($tags as $tag) {
return '<li>'. $tag->name . '</li>' ;
}
The first time this is ran it will exit the function and return the li. I imagine you meant to add it to output.
$tagHTML = '';
foreach($tags as $tag) {
$tagHTML .= '<li>'. $tag->name . '</li>' ;
}
//Later
$output .= $tagHTML;

Categories