Ask your WordPress questions! Pay money and get answers fast! (more info)

Help with some modification WordPress

  • SOLVED

i have a CPT field called 'news_shortcode' with some shortcode like [display-posts tag="collegium-wirtemberg" posts_per_page="3"].

I can get all post-links correctly in my template...

echo apply_filters('the_content', get_post_meta($post->ID, 'news_shortcode', true));


Later on my page i want to show 'the_content'. But what i see that is not the CPT content but the first posts content that was linked above.

the_content(__('Read more','avia_framework').'<span class="more-link-arrow"> &rarr;</span>');

If i move the last code before my "echo apply_filters" i get the correct content...

What must I do to get it work?

Answers (3)

2012-11-15

Dbranes answers:

Hi, here is one idea using the <em>do_shortcode()</em>

$s='[display-posts tag="collegium-wirtemberg" posts_per_page="3"]';
echo do_shortcode($s);

if (have_posts()) : while (have_posts()) : the_post();
the_content();
endwhile; endif;


$s=get_post_meta($post->ID, 'news_shortcode', true);
echo do_shortcode($s);

if (have_posts()) : while (have_posts()) : the_post();
the_content();
endwhile; endif;



more about it here:
http://codex.wordpress.org/Function_Reference/do_shortcode


Martin Grether comments:

thanks, all the same. i see not the CPT content but the first linked posts content. if i use the if-statement around the content i see no content at all.

the shortcode is only for the linked title. it produces a list of news with a tag that is connected to that CPT entry via a tag.


Dbranes comments:

do you get something from

echo $post->post_content;



Martin Grether comments:

yes, as long as it is not wrapped in this if-statement. there is no need for a loop here. but it is all the same. with a shortcode i get the first posts content, without the (wanted) cpt content.


Dbranes comments:

yes, my idea is then to replace:

echo "<div class='entry-content'>";
the_content(__('Read more','avia_framework').'<span class="more-link-arrow"> &rarr;</span>');
echo "</div>";


with

echo "<div class='entry-content'>";
echo $post->post_content;
echo "</div>";


in your code - or if you want the the_content filters on that output

echo "<div class='entry-content'>";
$content= $post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
echo $content;
echo "</div>";






Martin Grether comments:

all the same. all three options produce the same output.


Dbranes comments:

strange, here are three more ideas ;-) to replace these 3 lines

echo "<div class='entry-content'>";
the_content(__('Read more','avia_framework').'<span class="more-link-arrow"> &rarr;</span>');
echo "</div>";


in your code in

http://www.wpquestions.com/user/discourseShow/id/10503/discourse_id/814

with

a)
global $wpdb;
echo "<div class='entry-content'>";
echo $content = $wpdb->get_var( $wpdb->prepare( "SELECT post_content FROM $wpdb->posts WHERE ID=%d AND post_status='publish';",$id ) );
echo "</div>";


b)
$mypost = get_post( $id );
echo "<div class='entry-content'>";
$content= $mypost->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
echo $content;
echo "</div>";


c)

echo "<div class='entry-content'>";
echo get_post_content( $id );;
echo "</div>";



Martin Grether comments:

SUCESS with a.) and b.)
b.) also works without the "str_replace"
c.) is not working at all (AJAX page is not being loaded) also if i skip the mistyped semicolon.

Thanks.


Dbranes comments:

ok that's great news ;-)

you should also consider replacing

$id = $_POST['avia_ajax_request'];


with

$id = (int) $_POST['avia_ajax_request'];

in your code, so the input is only integers, it's a little bit more secure ;-)


Dbranes comments:

ps: I misspelled

echo get_post_content( $id );

I thought I wrote

echo get_the_content( $id );

in part c)

;-)


Martin Grether comments:

echo "<div class='entry-content'>";
echo get_the_content( $id );
echo "</div>";


Again, the linked post content. I stick with a.) .-)

2012-11-15

Jurre Hanema answers:

I think the display-posts shortcode is messing up your loop. Try adding a call to

wp_reset_query();

After the "echo apply_filters..." and see what happens.


Martin Grether comments:

I tried that before:

echo apply_filters('the_content', get_post_meta($post->ID, 'news_shortcode', true));
wp_reset_query();
the_content(__('Read more','avia_framework').'<span class="more-link-arrow"> &rarr;</span>');


It does not work. You can see it here...
http://ws.datema-software.de/vertretungen/

'Collegium Wirtemberg' has not the right content 'Weingut St. Antony' has no linked news and the right content.


Jurre Hanema comments:

In that case strange things happen. You might have to provide more code in order for someone to be able to effectively diagnose the problem.


Martin Grether comments:

it may is related to that cpt being displayed in an ajax call. for now i placed the links AFTER the content. that is not what i wanted but at last it's not wrong.

2012-11-15

Arnav Joy answers:

try this instead

<code>

wp_reset_query();
if(have_posts()):
while(have_posts()):the_post();
the_content(__('Read more','avia_framework').'<span class="more-link-arrow"> &rarr;</span>');
endwhile;endif;


Martin Grether comments:

No output at all.


Arnav Joy comments:

are you getting wrong output or a blanck page ,
try this

wp_reset_query();

if(have_posts()):

while ( have_posts() ) : the_post();

the_content();

endwhile;endif;


Martin Grether comments:

no output of the_content(). the rest is just fine.
your second suggestion just the same.


Arnav Joy comments:

try this

wp_reset_query();



if(have_posts()):



while ( have_posts() ) : the_post();



echo get_the_content();



endwhile;endif;


Martin Grether comments:

no, your if statement has no output.
echo get_the_content(); again brings the content of the first linked post.


Arnav Joy comments:

try this now

<?php

remove_filters('the_content');
wp_reset_query();

if(have_posts()):

while ( have_posts() ) : the_post();

the_content();

endwhile;endif;


?>


Arnav Joy comments:

please do these following things:-

1. remove your full first code of apply_filters and then test your the_content() thing to see if it gives you some output or not.

2. share code for the following function display-posts();


Martin Grether comments:

echo apply_filters('the_content', get_post_meta($post->ID, 'news_shortcode', true));
echo do_shortcode('[display-posts tag="collegium-wirtemberg" posts_per_page="3"]');


Both have the same effect. "the_content" will not show the cpt content but content of the first post that has been pulled via shortcode.

without shortcode OR with shortcode AFTER "the_content" i seen the right cpt content.


Arnav Joy comments:

did you tried my this code

<?php



remove_filters('the_content');

wp_reset_query();



if(have_posts()):



while ( have_posts() ) : the_post();



the_content();



endwhile;endif;





?>


share code for following function

function display-posts()


Martin Grether comments:

your code above is no solution.
I added the plugin-code:
http://www.wpquestions.com/user/discourseShow/id/10503/discourse_id/815