Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

plugin create a loop in a design of the theme WordPress

  • SOLVED

I raised the prize to $150 for a question not very hard :-)
and i edited my explanation to be more clear.

i want created a plugin.
I use this code [[LINK href="http://wpquestions.com/uploads/slh_phpsVmFxU.txt"]]http://wpquestions.com/uploads/slh_phpsVmFxU.txt[[/LINK]]
this code created a custom_post_type named "podcast".

with this code, you can see that the edit page have a field "Length"
1- I need insert in the edit page of the posts "podcast" a dropdown menu "Rank" (meta_box) with this values : first, second, 3, 4, 5
You can see an image here : [[LINK href="http://wpquestions.com/uploads/slh_phpzlqFMd.jpg"]]http://wpquestions.com/uploads/slh_phpzlqFMd.jpg[[/LINK]]

2- i need create a page in frontend, with the same design of a page of the active theme. But each post, in this theme, are composed by title, content and value of the dropdown "Rank" (and value of the "length" eventually)
And this posts must be order by "Rank"
You can see an image here : [[LINK href="http://wpquestions.com/uploads/slh_phpzlqFMd.jpg"]]http://wpquestions.com/uploads/slh_phpzlqFMd.jpg[[/LINK]]

if two posts have the same "Rank", this posts are ordered by date.

This is a plugin, so that must work with all themes, of course.

Answers (3)

2011-03-16

AdamGold answers:

Is your page-podcast.php file empty now (the file which displays all the custom posts)? If not, please post it here.
Anyway, this should be the structure of your page-podcast.php:

<?php
/**
* Template Name: Podcast
*/

get_header(); ?>

<?php $loop = new WP_Query( array( 'post_type' => 'podcast', 'posts_per_page' => 10 ) ); ?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

<?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>

<div class="entry-content">
<?php the_content(); ?>
</div>


<?php endwhile; ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>


And for displaying each post, put the following code in your single-podcast.php (if doesn't exists, create it):

<?php
/**
* The Template for displaying all podcasts posts.
*/

get_header(); ?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

<?php
// Display custom post meta
$meta_value = get_post_meta($post->ID, 'your meta value', true);
?>

<?php previous_post_link( '%link', '' . _x( '&larr;', 'Previous post link', 'gateway' ) . ' %title' ); ?>
<?php next_post_link( '%link', '%title ' . _x( '&rarr;', 'Next post link', 'gateway' ) . '' ); ?>

<h1><?php the_title(); ?></h1>

<?php gateway_posted_on(); ?>

<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '' . __( 'Pages:', 'gateway' ), 'after' => '' ) ); ?>

<?php if ( get_the_author_meta( 'description' ) ) : // If a user has filled out their description, show a bio on their entries ?>
<?php echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'gateway_author_bio_avatar_size', 60 ) ); ?>
<h2><?php printf( esc_attr__( 'About %s', 'gateway' ), get_the_author() ); ?></h2>
<?php the_author_meta( 'description' ); ?>
<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>">
<?php printf( __( 'View all posts by %s &rarr;', 'gateway' ), get_the_author() ); ?>
</a>
<?php endif; ?>

<?php gateway_posted_in(); ?>
<?php edit_post_link( __( 'Edit', 'gateway' ), '', '' ); ?>

<?php previous_post_link( '%link', '' . _x( '&larr;', 'Previous post link', 'gateway' ) . ' %title' ); ?>
<?php next_post_link( '%link', '%title ' . _x( '&rarr;', 'Next post link', 'gateway' ) . '' ); ?>

<?php if ( is_active_sidebar( get_post_meta($post->ID, 'gw_custom_sidebar', true) ) && get_post_meta($post->ID, 'gw_custom_sidebar', true) != 'none' ) { ?>
<ul class="xoxo">
<?php dynamic_sidebar( get_post_meta($post->ID, 'gw_custom_sidebar', true) ); ?>
</ul>
<?php } else if( get_post_meta($post->ID, 'gw_custom_sidebar', true) == 'default-wp-sidebar' ) { ?>

<?php get_sidebar(); ?>

<?php } ?>

<?php comments_template( '', true ); ?>

<?php endwhile; // end of the loop. ?>

<?php get_footer(); ?>


Change "yourmetavalue" to the meta value you want to get.
Read more about get_post_meta here:
http://codex.wordpress.org/Function_Reference/get_post_meta


slh comments:

1-there is no file page-podcast.php

2-i don't want to create files in the folder of the theme.
I want a plugin that create all automaticly.


slh comments:

I don't need a page-podcast : i have a redirection to podcast.php ^^

And what do you want that the function <?php gateway_posted_in(); ?> work if my theme is another ?


slh comments:

the code in your response :

<?php $loop = new WP_Query( array( 'post_type' => 'podcast', 'posts_per_page' => 10 ) ); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

is it a serious response ?!


AdamGold comments:

Sorry, didn't realize what you're looking for. Thought you want to make a page for all the custom types, and a page for each. You really need to explain your self better.


slh comments:

Lol AdamGold :-)

You're response is
<?php $loop = new WP_Query( array( 'post_type' => 'podcast', 'posts_per_page' => 10 ) ); ?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>


But what's this code ?

You must code like this in this case :
<?php $loop = new WP_Query( array( 'post_type' => 'podcast', 'posts_per_page' => 10 ) ); ?>
<?php while ($loop->have_posts()) : $loop->the_post(); ?>


Do you need an expert ? (yes, it's a joke :-)


AdamGold comments:

I was in hurry and didn't notice my mistake.
I won't argue with you, just tried to help you..
Good luck with your plugin, Mr.expert.


AdamGold comments:

By the way, if you want to change the_content I suggest you to try this solution:

function yourplugin_the_content() {
// your new the_content
}
add_action('the_content', 'yourplugin_the_content');


slh comments:

Don't be offended :-)
you say i really need to explain me self better (probably right) but you write a wrong code, I found it funny, that's all :-)


AdamGold comments:

That's ok, I am not offended ;) I was in hurry and I wasn't able to debug or even check my code before posting it.
Anyway, have you tried adding the above action like I was suggesting?


slh comments:

no. Where please ?


AdamGold comments:

Under the function getContent() you shall include your desired content (which I guess is podcast.php?):

function getContent() {
$page_content = include( WP_PLUGIN_DIR . "/yourplugin/podcast.php" );
return $page_content;
}


AdamGold comments:

<strong>Edit: Just noticed Denzel was ahead of me.</strong>


AdamGold comments:

In reply to your message to Denzel, try using file_get_contents:

function getContent() {
$content = file_get_contents(WP_PLUGIN_DIR . '/podcast-30/podcast_contents.php', true);
return $content;
}


slh comments:

/aboout-me > error 404


AdamGold comments:

Try echoing WP_PLUGIN_DIR . '/podcast-30/podcast_contents.php':

function getContent() {
echo WP_PLUGIN_DIR . '/podcast-30/podcast_contents.php';
}

So we can see if the 404 is because it can't find the file.


slh comments:

i had make a mistake in the url of my page
that works !


AdamGold comments:

I am not sure if you can order by "first, second, 3, 4" but what I know is you can order by A B C or 1 2 3 like this:

query_posts( $query_string . '&post_type=podcast&orderby=dropdown_menu&meta_key=dropdown_menu');

For this you will need to add a custom field (a meta key) with your values, which called 'dropdown_menu'.
Hope this helps.


AdamGold comments:

Actually, I think I found a solution for your problem.
For the first post, add a custom field named "dropdown_menu" and type "1". For the second, type "2" and so on..
Now, in your podcast_content.php, add the following query_posts:

query_posts( $query_string . '&post_type=podcast&orderby=meta_value_num&meta_key=dropdown_menu');


This should work.


AdamGold comments:

Wow, I am really not focused today. I figured out what you're trying to achieve.
The only way I can think of right now is by editing the page.php (directly, or not directly):

<?php
/**
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the wordpress construct of pages
* and that other 'pages' on your wordpress site will use a
* different template.
*/

get_header(); ?>
<?php $dropdown = wp_get_nav_menu_items('dropdown_menu');

foreach( $dropdown as $value ) {
query_posts( $query_string . '&post_type=podcast&meta_key=dropdown_menu');?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php $menu_value = get_post_meta($post->ID, 'dropdown_menu', true);
if( $menu_value == $value ) {

<?php if ( is_front_page() ) { ?>
<h2><?php the_title(); ?></h2>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>

<?php the_content(); ?>

<?php comments_template( '', true ); ?>

<?php } endwhile; ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

This way the posts will be ordered as your menu.

To add a menu, go to Appearance -> Menu. (In case your theme doesn't support it, I will guide you how to add a support)


AdamGold comments:

I suggest you to use file_get_contents( TEMPLATEPATH . '/page.php'); and then replace the whole code (except from the code between if(have_posts())) with what I gave you above. If you would like that solution, raise your prize and I will guide you how to do that.


slh comments:

Adam, it seems that my English is not as good as I thought :-)

I'll try to do better:

I explain to you : there is a dropdown menu in the edit page of each post.
It's a custom field, simply.
i want a page created by my plugin. This page displays the posts of custom_post_type "podcast".
This posts must be displayed in order of the value of this custom field (dropdown).

If the value is "first", the post is displayed in first position.
If the value is "second", the post is displayed in second position.
If the value is "3", the post is displayed in third position.
etc...

if there is several posts, with the value "first" for example, this posts will be displayed by date.

And for each post, the information are like this :
<div class=value of my dropdown">
<h2>title </h2>
<div>value of my dropdown</div>
<div>content</div>
</div>


AdamGold comments:

I still barely understand. If your values are first, second, 3 and so.. Why don't you just use the order field which is built-in in WordPress? (it is displayed in the publish section of the post)

Hope you understand what I mean :) I will be glad if you could tell me how you're going to use that kind of feature


slh comments:

1- I don't use the "order field" because the value of my dropdoawm are specific (first, not 1)
You can't type "first" in this field i think.

2- there will be another custom fields.


AdamGold comments:

So let's use an example.
Your dropdown menu is: test,othertest,wow

You want the posts to be ordered by the order of the values? I mean, posts with "test" value first, after that posts with "othertest" and so on..
Not just first, second and third.. Have I got it right?

If so, the code I gave you is supposed to be okay. I need to test it first, so tell me if this is what you're trying to achieve. If so, I will try to do it.


slh comments:

yes that's it.


AdamGold comments:

Okay.
Paste the following code in podcast.php:

$new_code = eval("
$dropdown = wp_get_nav_menu_items('dropdown_menu');



foreach( $dropdown as $value ) {

query_posts( $query_string . '&post_type=podcast&meta_key=dropdown_menu');
?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post();
");
$page_design = file_get_contents( TEMPLATEPATH . '/page.php', true );
$page_design = str_replace("if ( have_posts() ) while ( have_posts() ) : the_post();", $newcode, $page_design);
$page_design = str_replace("endwhile;", "} endwhile;", $page_design);


Now add a menu with your items, and add a custom field for each post.

Haven't tested it so let me know the results :)


AdamGold comments:

Don't forget to echo $page_design:
echo $page_design;


slh comments:

cool !

"Now add a menu with your items" > How ? ^^


AdamGold comments:

Go to Appearance -> Menus and type in the name of your menu, then just add links to it. (Add links because there is no other way of adding a normal text)


slh comments:

That's a strange solution. I want that all is automaticly created by the plugin.


AdamGold comments:

If you don't want the user to be able to edit your menu, so just use a defined array.. Really I don't understand why you want a menu if the user won't be able to edit it. You can just use a defined array in your code. Please clarify it to me.


slh comments:

Look at the last post of jevusi
Add-meta-box is the solution i need


AdamGold comments:

add_meta_box is for adding a box in the post admin page.
Let me explain my solution:
You have a custom menu which you create from the admin panel with all of your values.
Now, when you create a new post, you need to add a new custom field named "dropdown_menu" and type in one of your values.
Now, the posts with the first value of the menu will be displayed first, and so on.
Isn't this what you need?


slh comments:

AdamGold : i have respond to denzel, i have edit my question, add attachment etc... i hope that is more clear (is it english ?)
Let me know


AdamGold comments:

Yes it is clear now, but what isn't clear to me is why are you using that dropdown instead of the built-in order box? (Yes, same question again.. I don't understand why you want to re-invent the wheel)


slh comments:

In this field i can type 1 or 2 or 15, that's it ?
And can i type : "mason","john","henry" ? No. So that's not what i want.
Is it clear ?


AdamGold comments:

And how do you want to set these values? Static from the code?


slh comments:

Sorry Adam, i don't understand...

have you seen this image ? [[LINK href="http://wpquestions.com/uploads/slh_phpzlqFMd.jpg"]]http://wpquestions.com/uploads/slh_phpzlqFMd.jpg[[/LINK]]


AdamGold comments:

Do you need me to add this select menu (the meta box) or do you already done this?


slh comments:

YES i need, of course, that's my question :-)
And i need a code to ordre my post in the file podcast_content.php (ordre by value of this dropdown menu)

Is it clear ?


AdamGold comments:

And how do you want to create this select menu? By static values from the code?


slh comments:

Not sur to understand, so tell me if this response is dumb :

i look at the code of jevusi
and i think that i want is like that :

'name' => 'the name of my menu',
'desc' => '',
'id' => $myposttype.'_choix',
'type' => 'select',
'options' => array('first','second','1','2','3','4','5'), <----------the values
'std' => 'choose the value'),


AdamGold comments:

So use his code to create it, and this is the way to use it:




query_posts( $query_string . '&post_type=podcast');

?>

<?php
foreach ($myCustomTypeOptions as $o) {
?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post();
if( get_post_meta( $post->ID, 'menu_name', true ) == $o['name'] ) {
");

$page_design = file_get_contents( TEMPLATEPATH . '/page.php', true );

$page_design = str_replace("if ( have_posts() ) while ( have_posts() ) : the_post();", $newcode, $page_design);

$page_design = str_replace("endwhile;", "} endwhile; }", $page_design);


AdamGold comments:

oops, I've made a mistake. This is the code

$new_code = eval("
query_posts( $query_string . '&post_type=podcast');



?>



<?php

foreach ($myCustomTypeOptions as $o) {

?>



<?php if ( have_posts() ) while ( have_posts() ) : the_post();

if( get_post_meta( $post->ID, 'menu_name', true ) == $o['name'] ) {

");



$page_design = file_get_contents( TEMPLATEPATH . '/page.php', true );



$page_design = str_replace("if ( have_posts() ) while ( have_posts() ) : the_post();", $newcode, $page_design);



$page_design = str_replace("endwhile;", "} endwhile; }", $page_design);


slh comments:

his code ? That's an example code.

I try, but i don't know how insert his code in this code : [[LINK href="http://wpquestions.com/uploads/slh_phpsVmFxU.txt"]]http://wpquestions.com/uploads/slh_phpsVmFxU.txt[[/LINK]]


AdamGold comments:

Use jevusi's code to add the select menu.


slh comments:

I try, but i don't know how insert his code in this code : http://wpquestions.com/uploads/slh_phpsVmFxU.txt


AdamGold comments:

Just insert his code into Podcast30 function. Really, it's getting ridiculous.. I don't think we can build your whole plugin for 50$. Read more about WP and PHP before you build a plugin.


slh comments:

i have insert the code of jevusi, that seems ok, but your code doesn't work...


slh comments:

i paste this code in podcast_contents.php

<?php
$page_design = file_get_contents( TEMPLATEPATH . '/page.php', true );
return $page_design;
?>


the return is :

// calling the header.php
get_header();

// action hook for placing content above #container
thematic_abovecontainer();

?>

// calling the widget area 'page-top'
get_sidebar('page-top');

the_post();

thematic_abovepost();

?>

if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
post_class();
echo '>‘;
} else {
echo ‘class=”‘;
thematic_post_class();
echo ‘”>’;
}

// creating the post header
thematic_postheader();

?>

the_content();

wp_link_pages("\t\t\t\t\t
“.__(‘Pages: ‘, ‘thematic’), “

\n”, ‘number’);

edit_post_link(__(‘Edit’, ‘thematic’),’‘,’‘) ?>

thematic_belowpost();

// calling the comments template
if (THEMATIC_COMPATIBLE_COMMENT_HANDLING) {
if ( get_post_custom_values('comments') ) {
// Add a key/value of "comments" to enable comments on pages!
thematic_comments_template();
}
} else {
thematic_comments_template();
}

// calling the widget area 'page-bottom'
get_sidebar('page-bottom');

?>

// action hook for placing content below #container
thematic_belowcontainer();

// calling the standard sidebar
thematic_sidebar();

// calling footer.php
get_footer();

?>

in this cas the theme is "thematic"


AdamGold comments:

<pre>

$new_code = eval("

query_posts( $query_string . '&post_type=podcast');







?>







<?php



foreach ($myCustomTypeOptions as $o) {



?>







the_post();



if( get_post_meta( $post->ID, 'menu_name', true ) == $o['name'] ) {



");







$page_design = file_get_contents( TEMPLATEPATH . '/page.php', true );







$page_design = str_replace("the_post();", $newcode, $page_design);







$page_design = str_replace("thematic_comments_template();

}", "thematic_comments_template();

}
}", $page_design);


</pre>


AdamGold comments:

Be sure that $myCustomTypeOptions isn't empty.

$new_code = eval("

query_posts( $query_string . '&post_type=podcast');

foreach ($myCustomTypeOptions as $o) {
?>

the_post();



if( get_post_meta( $post->ID, 'menu_name', true ) == $o['name'] ) {



");







$page_design = file_get_contents( TEMPLATEPATH . '/page.php', true );







$page_design = str_replace("the_post();", $newcode, $page_design);







$page_design = str_replace("thematic_comments_template();

}", "thematic_comments_template();

}
}", $page_design);


slh comments:

in your code there is :thematic_comments_template();

But, that's a plugin... the theme could be thematic, or another theme... do you understand ?


AdamGold comments:

I understand, but thematic has a strange page.php and this is the only way to change it.


slh comments:

and if another theme use the same system of thematic but its function is
name-of-theme_comments_template();
instead of
thematic_comments_template();

??


slh comments:

I need a solution that is valid with all themes...


AdamGold comments:

Okay but please try this so at least I can see if it works. If it does, I will try to figure something out to fit all themes.


slh comments:

here is the content of my file podcast_contents.php


<?php $new_code = eval("
query_posts( $query_string . '&post_type=podcast');
foreach ($myCustomTypeOptions as $o) {
?>
the_post();
if( get_post_meta( $post->ID, 'menu_name', true ) == $o['name'] ) {
");
$page_design = file_get_contents( TEMPLATEPATH . '/page.php', true );
$page_design = str_replace("the_post();", $newcode, $page_design);
$page_design = str_replace("thematic_comments_template();
}", "thematic_comments_template();
}
}", $page_design);?>


the error message is
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /wp-content/plugins/podcast-30/podcast_contents.php on line 6

the line 6 is :
if( get_post_meta( $post->ID, 'menu_name', true ) == $o['name'] ) {


AdamGold comments:

Please contact me and I will sort it out. It's really hard to work like that.

ronalister [at] gmail [dot] com


AdamGold comments:

<strong>NOTE: Contact me before paying.</strong>


slh comments:

you can respond here, there is no problem for me :-)


AdamGold comments:

It's really hard to debug the code this way, I need to do it "on the fly".
Anyway, try to add a slash near $o['name']:

if( get_post_meta( $post->ID, 'menu_name', true ) == \$o['name'] ) {


slh comments:

does it work on your server ?


slh comments:


Parse error: syntax error, unexpected '.', expecting ')' in /wp-content/plugins/podcast-30/podcast_contents.php(7) : eval()'d code on line 2

why do you not test before on your server ?


AdamGold comments:

Forget about the code I gave you. I have a better solution that should work.

In jevusi's code, replace:


echo ' <select name="'.$o['id'].'" style="width:100%;" id="'.$o['id'].'"><option value="">-</option>';

foreach($o['options'] as $opt)

{

echo '<option value="'.$opt.'"';

if($opt==$val)

echo 'selected="selected"';

echo '>'.$opt.'</option>';

}


With:


echo ' <select name="'.$o['id'].'" style="width:100%;" id="'.$o['id'].'"><option value="">-</option>';
$i = 0;
foreach($o['options'] as $opt)

{

echo '<option value="'.$i.'"';

if($opt==$val)

echo 'selected="selected"';

echo '>'.$opt.'</option>';
$i++;
}


Now, in podcast_content.php:


query_posts( $query_string . '&post_type=podcast&posts_per_page=10&orderby=meta_value_num&metakey=yourkey');
$page_design = file_get_contents( TEMPLATEPATH . '/page.php', true );

return $page_design;


replace "yourkey" with the name of your meta key. Here's where you define it:

array(

'name' => 'Mon select',


AdamGold comments:

I am sorry, this is where you define it:
'id' => $myposttype.'_choix',


slh comments:

2 problems :

1-> Internal Server Error
2-> i want display the value of the dropdown in each post


AdamGold comments:

It's really hard to work that way, email me your FTP details and I'll just do what you want.. I can't write a code and wait for your response, without even see the results.
I think you're on the wrong page again, no way there's a internal error.. I haven't made any .htaccess changes.


slh comments:

i suggest you test in your server and when the plugin is ok, tell me.
Do you want the files ?


AdamGold comments:

Well, for 150$ I am not going to reach the sky. I offered to help you by connecting to your FTP and do it, if you don't want.. What can I do.
My code will work after some adjustments, I am sure about that.


slh comments:

i replace your code by echo "this is my page" and in my page i look "this is my page".
I replace with your code and i look "Internal Server Error"

Please, try before on your server :-)


slh comments:

i i comment the line of query_posts the content of my page is

// calling the header.php
get_header();

// action hook for placing content above #container
thematic_abovecontainer();

?>

// calling the widget area 'page-top'
get_sidebar('page-top');

the_post();

thematic_abovepost();

?>

if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
post_class();
echo '>‘;
} else {
echo ‘class=”‘;
thematic_post_class();
echo ‘”>’;
}

// creating the post header
thematic_postheader();

?>

the_content();

wp_link_pages("\t\t\t\t\t
“.__(‘Pages: ‘, ‘thematic’), “

\n”, ‘number’);

edit_post_link(__(‘Edit’, ‘thematic’),’‘,’‘) ?>

thematic_belowpost();

// calling the comments template
if (THEMATIC_COMPATIBLE_COMMENT_HANDLING) {
if ( get_post_custom_values('comments') ) {
// Add a key/value of "comments" to enable comments on pages!
thematic_comments_template();
}
} else {
thematic_comments_template();
}

// calling the widget area 'page-bottom'
get_sidebar('page-bottom');

?>

// action hook for placing content below #container
thematic_belowcontainer();

// calling the standard sidebar
thematic_sidebar();

// calling footer.php
get_footer();

?>


slh comments:


// following code will display the contents of the web page www.example.com
$url = “http://www.example.com”;
$contents = file_get_contents($url);
echo $contents;


slh comments:

i need just a loop in the file podcast_contents.php
is it possible ?

i don't need to "link" (file_get_contents) the page.php
because the fakepage use yet the design of a page
i just need a loop...

how can i create a loop in this file ?

i try with this code in podcast_content.php

<?php global $post;
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; else: ?>
Sorry, no posts matched your criteria.
<?php endif; ?>


but the return is "Sorry, no posts matched your criteria."


slh comments:

I use this code

<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'post_type' => 'podcast' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

but
1-it is not a "return", so the contents are printed above the theme headers.
2- i don't know display the value of the dropdown menu and ordered the posts


slh comments:

i have find a solution to display simply a loop in the fakepage
i use this code

function getContent()
{
$content .= '<ul>';
global $post;
$args = array( 'numberposts' => 5, 'post_type' => 'podcast' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
$content .= '<li><a href="' . get_permalink($post->ID) .'">' . $post->post_title . '</a></li>';
endforeach;
$content .= '</ul>';
//$content = include (WP_PLUGIN_DIR . '/podcast-30/podcast_contents.php');
return $content;
}


i must find now how i can ordered the posts by the value of the dropdown menu and after i declare me as winner.


slh comments:

Well, i have find my response :

i use the code of this [[LINK href="http://scott.sherrillmix.com/blog/blogger/creating-a-better-fake-post-with-a-wordpress-plugin/"]]tuto[[/LINK]] to create a fake page

i use [[LINK href="http://wpquestions.com/uploads/slh_phpsVmFxU.txt"]]this code[[/LINK]] to create my custom post type.

i use the code of jevusi to create my dropdown menu.

and i ue the code i have create

function getContent()
{
$content .= '<ul>';
global $post;
$args = array( 'numberposts' => 5, 'post_type' => 'podcast', 'orderby' => 'meta_value_num', 'meta_key' => 'podcast_choix', 'order' => 'asc' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
$dropdownvalue = get_post_meta($post->ID, 'podcast_choix', true);
if ($dropdownvalue==0) $dropdownvalue="first";
elseif ($dropdownvalue==1) $dropdownvalue="second";
elseif ($dropdownvalue==2) $dropdownvalue="3";
$content .= '<li><a href="' . get_permalink($post->ID) .'">' . $post->post_title . '</a>'. $dropdownvalue .'</li>' ;
endforeach;
$content .= '</ul>';
//$content = include (WP_PLUGIN_DIR . '/podcast-30/podcast_contents.php');
return $content;
}

instead of the function getcontent in the code of the fakepage.

And it's ok...


slh comments:

Adam, I forget : many many thanks to you to have spend your time. All your responses were unusable for my need, but thanks.
I'll give you a little money for time :-)

2011-03-16

jevusi answers:

You could create the file podcast.php
this file will be in the folder of your plugin
the content of this file would be :

<?php get_header() ?>


<div>

<?php //here your query_post
if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

<div>
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>

<div>
<?php //here your custom fields value
the_content(); ?>
</div>

</div>

<?php endwhile; ?>

<div class="navigation">
<div class="alignleft"><?php next_posts_link()) ?></div>
<div class="alignright"><?php previous_posts_link() ?></div>
</div>

<?php else : ?>
<?php get_search_form(); ?>

<?php endif; ?>

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>


the header, the footer and the sidebar will be the same of the active theme, but the "content" could be different. I don't know other solution.


slh comments:

Thanks. That's exactly what i have make. But how can i create a page podcast.php with a perfect design (design of the theme)
Is it possible ?


jevusi comments:

that is not a solution but an idea :
you can add this code in podcast.php


<?php
query_posts( $query_string . '&post_type=podcast&posts_per_page=10&orderby=name');//here the query_posts you need to display the specific content of your custom post
$color = get_post_meta($post->ID, 'color', true);//here the value of a custom field in your custom post type
include(TEMPLATEPATH ."/page.php");
?>


in this case, the design is exactly the same of a page. That's perfect.
The displayed content is the content of your custom post type, perfect too.
But you don't customize the loop : probably only <?php the_content(); ?> and <?php the_title() ?> are displayed.
No value of custom field.
And the design of each post is exactly the same of a post in your theme.
Probably would you customize the design of the post to display value of each custom field.


jevusi comments:

to add your dropdown menu (select) use this code

global $myposttype;
global $myCustomTypeOptions;
$myposttype='podcast'; //id de mon custom post

add_action('init', 'moncustomposttype_init');
function moncustomposttype_init()
{
global $myposttype;

register_post_type($myposttype, array(
'label' => 'Custom Posts',
'singular_label' =>'Custom Post',
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array('title', 'editor', 'thumbnail' )
));


/********** pour les checkbox à choix multiples : le plus simple est de déclarer une taxonomie hiérarchique (= catégorie) ****************/
register_taxonomy( 'couleurs', $myposttype, array( 'hierarchical' => true, 'label' => 'couleurs', 'query_var' => true, 'rewrite' => true ) );
}


$myCustomTypeOptions = array (
array(
'name' => 'Mon champ text',
'desc' => 'Description 1',
'id' => $myposttype.'_champtexte',
'type' => 'text',
'std' => ''),
array(
'name' => 'Mon textarea',
'desc' => 'Description 2',
'id' => $myposttype.'_textarea',
'type' => 'textarea',
'std' => 'Youpi'),
array(
'name' => 'Mon select',
'desc' => '',
'id' => $myposttype.'_choix',
'type' => 'select',
'options' => array('choix 1','choix 2','choix 3'),
'std' => 'choix 2'),
array(
'name' => 'Mon checkbox',
'desc' => 'Case à cocher',
'id' => $myposttype.'_case',
'type' => 'checkbox',
'std' => '1')
);

/************** à la fin du formulaire, ajout des champs définis dans $myCustomTypeOptions ***********/
add_action('edit_form_advanced', 'moncustomposttype_form');
add_action('save_post', 'moncustomposttype_save');
function moncustomposttype_form(){

global $myposttype;
global $myCustomTypeOptions;

if((isset($_GET['post_type'])) and ($_GET['post_type']==$myposttype)) /* formulaire d'ajout */
{
echo '<div class="meta-box-sortables ui-sortable">';
/* formulaire vide (ou avec les valeurs par défaut) */
foreach ($myCustomTypeOptions as $o)
{
echo '<div class="postbox">';
echo '<h3 class="hndle"><span>'.$o['name'].'</span></h3><div class="inside"><label class="screen-reader-text" for="'.$o['id'].'">'.$o['name'].'</label>';

//fonction pour afficher le bon html en fonction du type, définie ci-après
echo get_champ($o,$o['std']);

if($o['desc']!='')
echo '<p>'.$o['desc'].'</p>';

echo '</div></div>';
}
echo '</div>';

}
else{
if(!isset($_GET['post_type']))
{
if(isset($_GET['post']))
{
if(get_post_type($_GET['post'])==$myposttype) /* formulaire de modification */
{
$id=$_GET['post'];

/* formulaire prérempli avec les valeurs pré-existantes */
echo '<div class="meta-box-sortables ui-sortable">';
foreach ($myCustomTypeOptions as $o)
{
echo '<div class="postbox">';
echo '<h3 class="hndle"><span>'.$o['name'].'</span></h3><div class="inside"><label class="screen-reader-text" for="'.$o['id'].'">'.$o['name'].'</label>';

echo get_champ($o,get_post_meta($id, $o['id'], true));

if($o['desc']!='')
echo '<p>'.$o['desc'].'</p>';

echo '</div></div>';
}
echo '</div>';
}
}
}
}
}

function get_champ($o,$val)
{
switch ($o['type'])
{
case 'textarea':
echo ' <textarea rows="2" cols="40" name="'.$o['id'].'" id="'.$o['id'].'">'.$val.'</textarea>';
break;
case 'text':
echo ' <input type="text" style="width:100%;" name="'.$o['id'].'" id="'.$o['id'].'" value="'.$val.'"/>';
break;
case 'checkbox':
echo '<input type="checkbox" name="'.$o['id'].'" id="'.$o['id'].'" value="1" ';
if($val==1)
echo 'checked="checked';
echo '/><label for="'.$o['id'].'">'.$o['name'].'</label>';
break;
case 'select':
echo ' <select name="'.$o['id'].'" style="width:100%;" id="'.$o['id'].'"><option value="">-</option>';
foreach($o['options'] as $opt)
{
echo '<option value="'.$opt.'"';
if($opt==$val)
echo 'selected="selected"';
echo '>'.$opt.'</option>';
}
echo'</select>';
break;
}

}
/***************** Lors de la sauvegarde, ajout/modification d'un custom field par champs de $myCustomTypeOptions ***********/
function moncustomposttype_save(){

global $myposttype;
global $myCustomTypeOptions;

if(isset($_POST['post_ID']))
{
$id=$_REQUEST['post_ID'];
if(get_post_type($id)==$myposttype) /* si on édite bien un post du type $myposttype */
{
foreach ($myCustomTypeOptions as $o)
{
if(isset($_POST[$o['id']])) {
update_post_meta($id, $o['id'], $_POST[$o['id']]);
}
elseif($o['type']=='checkbox') {
update_post_meta($id, $o['id'], 0);
}
}
}
}
}


slh comments:

jevusi, i try to insert your code in thsi code http://wpquestions.com/uploads/slh_phpsVmFxU.txt
but that doesn't work

could you insert your code in this code please. And i need the code to display the posts in podcast-content.php

If you can create that, you are the winner :-)


jevusi comments:

that's my code inserted


<?php
/*
Plugin Name: Podcast 3.0
Plugin URI: http://kovshenin.com/wordpress/plugins/podcast-30/
Description: Podcast post types for WordPress 3.0 and above.
Author: Konstantin Kovshenin
Version: 1.0
Author URI: http://kovshenin.com
*/
class Podcast30 {
var $meta_fields = array("p30-length");

function Podcast30()
{

global $myposttype;
global $myCustomTypeOptions;
$myposttype='podcast'; //id de mon custom post
add_action('init', 'moncustomposttype_init');
function moncustomposttype_init()
{
global $myposttype;

register_post_type($myposttype, array(
'label' => 'Custom Posts',
'singular_label' =>'Custom Post',
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array('title', 'editor', 'thumbnail' )
));
/********** pour les checkbox à choix multiples : le plus simple est de déclarer une taxonomie hiérarchique (= catégorie) ****************/
register_taxonomy( 'couleurs', $myposttype, array( 'hierarchical' => true, 'label' => 'couleurs', 'query_var' => true, 'rewrite' => true ) );
}
$myCustomTypeOptions = array (
array(
'name' => 'Mon champ text',
'desc' => 'Description 1',
'id' => $myposttype.'_champtexte',
'type' => 'text',
'std' => ''),
array(
'name' => 'Mon textarea',
'desc' => 'Description 2',
'id' => $myposttype.'_textarea',
'type' => 'textarea',
'std' => 'Youpi'),
array(
'name' => 'Mon select',
'desc' => '',
'id' => $myposttype.'_choix',
'type' => 'select',
'options' => array('choix 1','choix 2','choix 3'),
'std' => 'choix 2'),
array(
'name' => 'Mon checkbox',
'desc' => 'Case à cocher',
'id' => $myposttype.'_case',
'type' => 'checkbox',
'std' => '1')
);
/************** à la fin du formulaire, ajout des champs définis dans $myCustomTypeOptions ***********/
add_action('edit_form_advanced', 'moncustomposttype_form');
add_action('save_post', 'moncustomposttype_save');
function moncustomposttype_form(){
global $myposttype;
global $myCustomTypeOptions;
if((isset($_GET['post_type'])) and ($_GET['post_type']==$myposttype)) /* formulaire d'ajout */
{
echo '<div class="meta-box-sortables ui-sortable">';
/* formulaire vide (ou avec les valeurs par défaut) */
foreach ($myCustomTypeOptions as $o)
{
echo '<div class="postbox">';
echo '<h3 class="hndle"><span>'.$o['name'].'</span></h3><div class="inside"><label class="screen-reader-text" for="'.$o['id'].'">'.$o['name'].'</label>';

//fonction pour afficher le bon html en fonction du type, définie ci-après
echo get_champ($o,$o['std']);

if($o['desc']!='')
echo '<p>'.$o['desc'].'</p>';

echo '</div></div>';
}
echo '</div>';
}
else{
if(!isset($_GET['post_type']))
{
if(isset($_GET['post']))
{
if(get_post_type($_GET['post'])==$myposttype) /* formulaire de modification */
{
$id=$_GET['post'];

/* formulaire prérempli avec les valeurs pré-existantes */
echo '<div class="meta-box-sortables ui-sortable">';
foreach ($myCustomTypeOptions as $o)
{
echo '<div class="postbox">';
echo '<h3 class="hndle"><span>'.$o['name'].'</span></h3><div class="inside"><label class="screen-reader-text" for="'.$o['id'].'">'.$o['name'].'</label>';

echo get_champ($o,get_post_meta($id, $o['id'], true));

if($o['desc']!='')
echo '<p>'.$o['desc'].'</p>';

echo '</div></div>';
}
echo '</div>';
}
}
}
}
}
function get_champ($o,$val)
{
switch ($o['type'])
{
case 'textarea':
echo ' <textarea rows="2" cols="40" name="'.$o['id'].'" id="'.$o['id'].'">'.$val.'</textarea>';
break;
case 'text':
echo ' <input type="text" style="width:100%;" name="'.$o['id'].'" id="'.$o['id'].'" value="'.$val.'"/>';
break;
case 'checkbox':
echo '<input type="checkbox" name="'.$o['id'].'" id="'.$o['id'].'" value="1" ';
if($val==1)
echo 'checked="checked';
echo '/><label for="'.$o['id'].'">'.$o['name'].'</label>';
break;
case 'select':
echo ' <select name="'.$o['id'].'" style="width:100%;" id="'.$o['id'].'"><option value="">-</option>';
foreach($o['options'] as $opt)
{
echo '<option value="'.$opt.'"';
if($opt==$val)
echo 'selected="selected"';
echo '>'.$opt.'</option>';
}
echo'</select>';
break;
}
}
/***************** Lors de la sauvegarde, ajout/modification d'un custom field par champs de $myCustomTypeOptions ***********/
function moncustomposttype_save(){
global $myposttype;
global $myCustomTypeOptions;

if(isset($_POST['post_ID']))
{
$id=$_REQUEST['post_ID'];
if(get_post_type($id)==$myposttype) /* si on édite bien un post du type $myposttype */
{
foreach ($myCustomTypeOptions as $o)
{
if(isset($_POST[$o['id']])) {
update_post_meta($id, $o['id'], $_POST[$o['id']]);
}
elseif($o['type']=='checkbox') {
update_post_meta($id, $o['id'], 0);
}
}
}
}
}

// Register custom post types
register_post_type('podcast', array(
'label' => __('Podcasts'),
'singular_label' => __('Podcast'),
'public' => true,
'show_ui' => true, // UI in admin panel
'_builtin' => false, // It's a custom post type, not built in
'_edit_link' => 'post.php?post=%d',
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array("slug" => "podcast"), // Permalinks
'query_var' => "podcast", // This goes to the WP_Query schema
'supports' => array('title','author', 'excerpt', 'editor' /*,'custom-fields'*/) // Let's use custom fields for debugging purposes only
));

add_filter("manage_edit-podcast_columns", array(&$this, "edit_columns"));
add_action("manage_posts_custom_column", array(&$this, "custom_columns"));

// Register custom taxonomy
register_taxonomy("speaker", array("podcast"), array("hierarchical" => true, "label" => "Speakers", "singular_label" => "Speaker", "rewrite" => true));
// Admin interface init
add_action("admin_init", array(&$this, "admin_init"));
add_action("template_redirect", array(&$this, 'template_redirect'));

// Insert post hook
add_action("wp_insert_post", array(&$this, "wp_insert_post"), 10, 2);
}

function edit_columns($columns)
{
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Podcast Title",
"p30_description" => "Description",
"p30_length" => "Length",
"p30_speakers" => "Speakers",
);

return $columns;
}

function custom_columns($column)
{
global $post;
switch ($column)
{
case "p30_description":
the_excerpt();
break;
case "p30_length":
$custom = get_post_custom();
echo $custom["p30-length"][0];
break;
case "p30_speakers":
$speakers = get_the_terms(0, "speaker");
if($speakers){
$speakers_html = array();
foreach ($speakers as $speaker)
array_push($speakers_html, '<a href="' . get_term_link($speaker->slug, "speaker") . '">' . $speaker->name . '</a>');

echo implode($speakers_html, ", ");
}
break;
}
}

// Template selection
function template_redirect()
{
global $wp;
if ($wp->query_vars["post_type"] == "podcast")
{
include(dirname (__FILE__) ."/podcast.php");
die();
}
}

// When a post is inserted or updated
function wp_insert_post($post_id, $post = null)
{
if ($post->post_type == "podcast")
{
// Loop through the POST data
foreach ($this->meta_fields as $key)
{
$value = @$_POST[$key];
if (empty($value))
{
delete_post_meta($post_id, $key);
continue;
}
// If value is a string it should be unique
if (!is_array($value))
{
// Update meta
if (!update_post_meta($post_id, $key, $value))
{
// Or add the meta data
add_post_meta($post_id, $key, $value);
}
}
else
{
// If passed along is an array, we should remove all previous data
delete_post_meta($post_id, $key);

// Loop through the array adding new values to the post meta as different entries with the same name
foreach ($value as $entry)
add_post_meta($post_id, $key, $entry);
}
}
}
}

function admin_init()
{
// Custom meta boxes for the edit podcast screen
add_meta_box("p30-meta", "Podcast Options", array(&$this, "meta_options"), "podcast", "side", "low");
}

// Admin post meta contents
function meta_options()
{
global $post;
$custom = get_post_custom($post->ID);
$length = $custom["p30-length"][0];
?>
<label>Length:</label><input name="p30-length" value="<?php echo $length; ?>" />
<?php
}
}
// Initiate the plugin
add_action("init", "Podcast30Init");
function Podcast30Init() { global $p30; $p30 = new Podcast30(); }


slh comments:

Jevusi, many thanks for this code.
You are alone to really create a code.
And a code that work and will be useful for my plugin
Congratulations.
I give the lion's share

2011-03-16

Denzel Chia answers:

Hi,

You may want to take a look at this tutorial, for creating a "fake page" in WordPress.
http://scott.sherrillmix.com/blog/blogger/creating-a-better-fake-post-with-a-wordpress-plugin/
This tutorial allows you to create a "fake page" that does not require database entry and fits into the content, using the perfect design of the theme. It mimics a "real" wordpress page.

I had used it in a plugin for my client called Real Estate Q&A.
You can test it here
http://blog.realtybaron.com/realanswers/questions/?type=metro&value=Atlanta,%20GA

Thanks.
Denzel


slh comments:

Denzel,

i have try this. That's an interessant idea, but how can i customize the post_content ?
I want a query_post to order the posts of my custom_post_type by value of my dropdown (see question)
and i must display the value of several custom fields


Denzel Chia comments:

Hi,

With reference to the "fake post" plugin,

under createPost() , add the post type
$post->post_type = 'podcast';

under getContent() , add your include of podcast contents

$content = include (WP_PLUGIN_DIR . '/your_plugin_folder_name/podcast_contents.php');
return $content;

Remember all your podcast contents must be returned instead of echo, or the contents will be printed above the theme headers.

Lastly, I don't understand what you want for menu.
There is no on the fly creating of menus for WordPress.

Thanks.
Denzel


slh comments:

i replace
$post->post_author = 1;
by
$post->post_author = 1;
$post->post_type = 'podcast';


i replace this code

function getContent()
{
return '<p>Hi there! You are viewing my fake post!</p>';
}


by
function getContent()
{
$content = include (WP_PLUGIN_DIR . '/podcast-30/podcast_contents.php');
return $content;
}

in podcast_contents.php i write "<?php ?>test<?php ?>"

i go to http://mysite/about-me but that's an error 404
(before this page displayed "Hi there! You are viewing my fake post!")


Denzel Chia comments:

Hi

in podcast_content.php

it should be something like that


<?php
$test = 'Testing PodCast';
return $test;
?>


Thanks.


slh comments:

doesn't work, denzel, error 404 one more time


slh comments:

sorry, i make a mistake
the url must be ?page_id=about-me
no /about-me
that works


slh comments:

i explain to you what's the dropdown menu

I need to add a dropdown menu with this values : "first", "second", "3", "4", "5" (like that)
That's probably a custom field

and i would to order the post in my page "about-me" by the value of this dropdown menu
the posts with the value "first" in first
the posts with the value "seconde" in second
the posts with the value "3" in third
etc...

what can i add this menu in the administration ?
i suppose i woudl add a query_posts in podcast_content.php isn't it ?
What is this query_posts please ?


slh comments:

if you don't understand what i write ( i'm not English :-) like AdamGold

I'll try to do better:

I explain to you : there is a dropdown menu in the edit page of each post.
It's a custom field, simply.
i want a page created by my plugin. This page displays the posts of custom_post_type "podcast".
This posts must be displayed in order of the value of this custom field (dropdown).

If the value is "first", the post is displayed in first position.
If the value is "second", the post is displayed in second position.
If the value is "3", the post is displayed in third position.
etc...

if there is several posts, with the value "first" for example, this posts will be displayed by date.

And for each post, the information are like this :
<div class=value of my dropdown">
<h2>title </h2>
<div>value of my dropdown</div>
<div>content</div>
</div>


Denzel Chia comments:

<em><strong>I am not going to answer anymore.</strong></em>

<em><strong>You are wasting everybody's time.</strong></em>

<em><strong>You don't even know the difference between a custom meta box or menu or HTML drop down list!?!?</strong></em>

You keep saying you want a dropdown menu then suddenly it becomes a custom field?!!?!

<em><strong>Please get your facts straight! </strong></em>

You should say you want a custom meta box with a drop down list,
not a dropdown menu. Menu in WordPress refers to website menu, admin menu, etc, not a custom meta box with a HTML dropdown list of values!?!?!

<em><strong>You could argue that you are poor in English, but this has nothing to do with English!</strong></em>
<em><strong>This has to do with your knowledge of WordPress jargon!</strong></em>
So I suggest you read more about WordPress, read more on PHP, do your homework well,
before attempting to do a Plugin. There is no shortcut in gaining knowledge!

<em><strong>Your last explanation should be your initial question!
This will save people a lot of time!</strong></em>

<em><strong>Well, the question is expiring and you can get your refund now.</strong></em>


slh comments:

Denzel,

i'm sorry. In french, a dropdown menu is this thing (see attachment).

I don't know if that is a meta box, or a menu or a custom field. You are expert, not me :-)

It seems that for you a "dropdown menu" is systematically a custom menu (appearence>menu). I'm sorry, i didn't know that.

I need a dropdown menu like that in my edit page.

And my posts must be arranged in the order of this value.

Said me if it's a good jargon, and a good english :-))

And what's your fee for all the question.