I am creating a query using shortcodes ultimate lightbox. But the only way this will work within a regular php page, is to save the data as string. So what I need to do is to create my query but somehow get my results within a string.
Here is what works before I use any kind of php query:
<?php
$my_tabs = "<ul class='easybuttons'>
<li>[su_lightbox type='inline' src='#lightbox1']AT&T[/su_lightbox]</li>
<li>[su_lightbox type='inline' src='#lightbox2']Sprint[/su_lightbox]</li>
<li>[su_lightbox type='inline' src='#lightbox3']T-Mobile[/su_lightbox]</li>
</ul>";
echo do_shortcode( $my_tabs );
?>
but I need the ATT, Sprint, T-Mobile to be dynamic. Keep in mind the shortcode will only work if it within a string.
So how can I do a while loop within this string? I tried using an operator but did not work.
$args = array('post_type' => 'services', 'category_name' => $childid, 'order_by' => 'the_title', 'order' => 'ASC');
query_posts($args);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$my_tabs .= '<ul class="easybuttons">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox1"]' . get_the_title() . '</li>';
}
$my_tabs .= '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();
echo do_shortcode( $my_tabs );
?>
UPDATE:
I tried using this code but it does work. Nothing comes through. I don't get any errors but no shortcode is displayed.
<?php
$args = array('post_type' => 'services', 'category_name' => $childid, 'order_by' => 'the_title', 'order' => 'ASC');
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$lid = 1;
$my_tabs .= '<ul class="easybuttons">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox' . $lid . '"]' . get_the_title() . '</li>';
$lid++;
}
$my_tabs .= '</ul>';
}
echo do_shortcode( $my_tabs );
wp_reset_postdata();
You need to initialise the variable $my_tabs somewhere, for instance outside the if block, and increment the lightbox id. You don't need to call query_posts(). order_by should be title, not the_title. Make sure $childid is definitely a string of the category slug, not the name, if in doubt, take out that parameter altogether to see if you get anything as I imagine this is most likely to be the main issue.
$args = array('post_type' => 'services', 'category_name' => $childid, 'order_by' => 'title', 'order' => 'ASC');
// The Query
$the_query = new WP_Query( $args );
$my_tabs = '';
// The Loop
if ( $the_query->have_posts() ) {
$lid = 1;
$my_tabs .= '<ul class="easybuttons">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox' . $lid . '"]' . get_the_title() . '</li>';
$lid++;
}
$my_tabs .= '</ul>';
}
Related
I’m using bellow function for related post shortcode and I have a question about it. It’s supposed to display posts with related tag (I don't why it shows just random posts) but I want to use something like [rps tag=google] and the function just return a post has a tag as “google”, I mean nothing related to the current post tags. How can I do that?
This is the code:
add_shortcode('rps', 'fphp_get_related_posts');
function fphp_get_related_posts() {
$reset_post = $post;
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
$post_tag_ids = array();
foreach($post_tags as $post_tag) $post_tag_ids[] = $post_tag->term_id;
$args=array(
'tag__in' => $post_tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page' => 1,
'orderby' => 'rand',
);
$related_query = new wp_query( $args );
if (intval($related_query->post_count) === 0) return '';
$html = '<div class="rps"><ul><h3>Also read:</h3>';
$related_query->the_post();
$html .= '<li style="width:250px">';
$html .= '<div class="relatedthumb"><a rel="external" href="'. get_the_permalink(). '">';
$html .= get_the_title() . '</a>';
$html .= '</div></li>';
}
$post = $reset_post;
wp_reset_query();
$html .= '</ul></div>';
return $html;
}
If you want to show given tag post, then no need to use wp_get_post_tags(), because its return multi tags. try this code,
function fphp_get_related_posts( $atts ) {
$atts = shortcode_atts( [
'tag' => '',
], $atts );
$args = [ 'posts_per_page' => 1, 'tag' => $atts['tag'], 'orderby' => 'rand' ];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
ob_start(); ?>
<!-- your html here -->
<?php endwhile; endif; wp_reset_postdata();
return ob_get_clean();
}
add_shortcode( 'rps','fphp_get_related_posts' );
trying to put together a wp site for a martial arts centre, and they need a list of their instructors.
i try to get all the children of the instructor page appear in list by their page order, and that works fine, except it only shows the first 10 pages, and there is currently 13 instructor pages...
this is the code i used:
<section id="instruktorer">
<div class="indent">
<?php
$query = new WP_Query( 'pagename=instruktorer' );
$services_id = $query->queried_object->ID;
/* Restore original Post Data */
wp_reset_postdata();
$args = array(
'post_type' => 'page',
'post_parent' => $services_id,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$services_query = new WP_Query( $args );
// The Loop
if ( $services_query->have_posts() ) {
echo '<ul class="instruktorer">';
while ( $services_query->have_posts() ) {
$services_query->the_post();
echo '<li class="clear">';
echo '<a href="' . get_permalink() . '" figure class="instruktorer-thumb">';
the_post_thumbnail('instructor-pic');
echo '</a>';
echo '</figure>';
echo '<div class="caption">' . get_post( get_post_thumbnail_id() )->post_excerpt . '</div>';
echo '</li>';
}
echo '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
</div><!-- .indent -->
</section>
Change your query to this:
$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_parent' => $services_id,
'orderby' => 'menu_order',
'order' => 'ASC'
);
The below query works if I manually assign the meta value like so 'meta_value' => '4-5', but I need it to return posts for each of the $cartArr values. How can something like that be done?
<ul id="order-box" class="list-group">';
print_r($cartArr); //Array ( [0] => 3-6 [1] => 4-5 )
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_key ';
}
add_filter('posts_groupby', 'query_group_by_filter');
$the_query = new WP_Query(array(
'post_type' => 'clasa',
'post_status' => 'publish',
'meta_key' => 'twin_id',
'meta_value' => '4-5'
)
);
remove_filter('posts_groupby', 'query_group_by_filter');
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li class="list-group-item" id="remli-'.$post->ID.'">' . get_the_title() . '<a class="right remove" id="remove-'.get_post_meta($post->ID, 'twin_id', true).'" href="#">x</a>';
}
} else {
// no posts found
}
/* Restore original Post Data */
//wp_reset_postdata();
echo '</ul>';
Need to add a foreach loop. like foreach($cartArr as $cartArrr) which is mentioned in bellow line. If working please vote me.
<ul id="order-box" class="list-group">';
<?php
print_r($cartArr); //Array ( [0] => 3-6 [1] => 4-5 )
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_key ';
}
add_filter('posts_groupby', 'query_group_by_filter');
foreach($cartArr as $cartArrr) {
$the_query = new WP_Query(array(
'post_type' => 'clasa',
'post_status' => 'publish',
'meta_key' => 'twin_id',
'meta_value' => $cartArrr
)
);
remove_filter('posts_groupby', 'query_group_by_filter');
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li class="list-group-item" id="remli-'.$post->ID.'">' . get_the_title() . '<a class="right remove" id="remove-'.get_post_meta($post->ID, 'twin_id', true).'" href="#">x</a>';
}
} else {
// no posts found
}
/* Restore original Post Data */
//wp_reset_postdata();
}
echo '</ul>';
I have spent hours on this - hoping someone who knows a bit of PHP can solve it painlessly.
I have a Wordpress query loop which needs to be concatenated together. It kind of works, but I have requested 6 posts, and all 6 are the same!
Here's the code snippet which returns the duplicate posts:
////* New tab *//////
$args = array( 'numberposts' => 6, 'orderby' => 'post_date' );
$postslist = get_posts( $args );
$content .= '<div class="tab" id="new"><ul>';
foreach ($postslist as $post) : setup_postdata($post);
$content .= '<li><a href="' . get_permalink() . '"</a>' . get_the_title() . '</a></li>';
endforeach;
$content .= '</ul></div>';
return $content; // prints all the contents stringed together
This code produces this list:
Any help appreciated...at all! Thanks.
It looks like your code example is taken from inside a function.
a) Please try this:
// ... cut ...
global $post;
$args = array( 'posts_per_page' => 6, 'orderby' => 'post_date' );
$postslist = get_posts( $args );
$content .= '<div class="tab" id="new"><ul>';
foreach ( $postslist as $post ) : setup_postdata( $post );
$content .= sprintf( '<li>%s</li>',
get_permalink(),
get_the_title() );
endforeach;
$content .= '</ul></div>';
wp_reset_postdata();
return $content; // prints all the contents stringed together
where I added the global $post declaration so setup_postdata( $post ) can modify the global $post object.
b) Or try this:
// ... cut ...
$args = array( 'posts_per_page' => 6, 'orderby' => 'post_date' );
$postslist = get_posts( $args );
$content .= '<div class="tab" id="new"><ul>';
foreach ( $postslist as $post ) :
$content .= sprintf( '<li>%s</li>',
get_permalink( $post->ID ),
get_the_title( $post->ID ) );
endforeach;
$content .= '</ul></div>';
return $content; // prints all the contents stringed together
where I used the post id as an input argument for get_permalink() and get_the_title().
c) You can also use the raw post title: $post->post_title or if you want to take it through the the_title filter with:
echo apply_filters( 'the_title', $post->post_title );
So to wrap it up:
Notice that get_the_title() is based on get_post(), and if you don't use an input argument, then it will try to use the global $post object:
if ( empty( $post ) && isset( $GLOBALS['post'] ) )
$post = $GLOBALS['post'];
Therefore you always get the same global post object title (usually from the main query) if you don't modify the global object within the secondary loop.
Hope this helps.
i found this great snippet of code on http://codex.wordpress.org/Template_Tags/get_posts and I edited it a little to suit me. It works.
<p>
<?php
$args = array( 'posts_per_page' => 0, 'offset'=> 0, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?>
</br>
<?php endforeach;
wp_reset_postdata();?>
</p>
I would like to transform it into a shortcode with one changeable variable - category id. But, well, since I don't know php, all I came up is:
function tytuly_postow($atts) {
extract(shortcode_atts(array(
'id' => 1,
), $atts));
$args = array( 'posts_per_page' => 0, 'offset'=> 0, 'category' => $id );
$q ="<p>
<?php
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?>
</br>
<?php endforeach;
wp_reset_postdata();?>
</p>";
}
add_shortcode('tytuly','lista_postow');
Of course, it doesn't work. I use Karma theme and added it in shortcodes.php file. Please, help :)
The code you have is certainly on the right track. Here's how I'd do it:
function tytuly_postow( $atts ) {
extract( shortcode_atts( array(
'id' => 1,
), $atts ) );
$posts = get_posts( array(
'posts_per_page' => -1,
'post_status' => 'publish',
'cat' => $id,
) );
$output = '';
if ( $posts ) {
$output .= '<p>';
foreach ( $posts as $post ) {
$output .= apply_filters( 'the_title', $post->post_title ) . '<br />';
}
$output .= '</p>';
}
return $output;
}
add_shortcode( 'tytuly', 'tytuly_postow' );
Offset by default is 0 so you don't need to add it as an argument. I've set posts_per_page to -1. This means get all.
Since you're just listing off titles it may make sense to order them by title. At the moment they'll be ordered by date descending.
I'm setting output to a blank string and returning output at the end. This means no matter what happens the shortcode will always return something, even if that something happens to be an empty string.
Then I'm checking if posts were found. If that's the case I'm adding a paragraph tag before and after the foreach. This was to match your original code.
Finally it loops through each post and uses the_title filter on the post titles then ends each title with a break tag.
By taking a look at the Shortcode API and add_shortcode Function Reference, you can check that the parameters of function add_shortcode are $tag and $func.
So, to start fixing your code, you need to fix it:
//[tutuly]
function tytuly_postow($atts) {
...
}
add_shortcode('tytuly', 'tytuly_postow');
By doing this, you will have a working structure for your shortcode. Now, your second mistake is that your function needs to return a value, thats what your shortcode will be transformed into. To do this, you need to use return.
Edit: Plus, your logic to retrive posts names was wrong, so I fixed it:
So, your fixed code will look like:
//[tutuly id="1"]
function tytuly_postow($atts) {
extract(shortcode_atts(array(
'id' => 1,
), $atts));
$args = array(
'posts_per_page' => -1,
'category' => $id,
'post_status' => 'publish'
);
$myposts = get_posts($args);
if ($myposts) {
$q = '<p>';
foreach ($myposts as $post) {
$q .= apply_filters('the_title', $post->post_title) . '<br />';
}
$q .= '</p>';
} else {
$q = '';
}
return $q;
}
add_shortcode('tytuly','tytuly_postow');
posts_per_page is now set to -1 to retrieve all posts,
post_status is used to retrieve only posts that are published.
If you want to retrieve links for your posts, instead of just the name, you can change this line:
$q .= apply_filters('the_title', $post->post_title) . '<br />';
to
$q .= '<a href=' . get_permalink($post->ID) . '>' . apply_filters('the_title', $post->post_title) . '</a><br />';
Notice that if you don't have enough programming skills, you can always use the awesome GenerateWP Shorcodes Generator.