Query per il tipo di post personalizzato?
3 risposta
- voti
-
- 2011-01-06
query_posts( array( 'post_type' => array('post', 'portfolio') ) );
chemostra siai postnormali chei post all'interno deltipo
portfolio
o
query_posts('post_type=portfolio');
soloper
portfolio
.Usa comenormale query WP: leggiil Codex: http://codex.wordpress.org/Function_Reference/query_posts # Usage e http://codex.wordpress.org/Function_Reference/query_posts#Post_.26_Page_Parameters
<?php query_posts(array( 'post_type' => 'portfolio', 'showposts' => 10 ) ); ?> <?php while (have_posts()) : the_post(); ?> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <p><?php echo get_the_excerpt(); ?></p> <?php endwhile;?>
query_posts( array( 'post_type' => array('post', 'portfolio') ) );
which shows both normal posts and posts inside
portfolio
typeor
query_posts('post_type=portfolio');
for only
portfolio
.Use as normal WP Query - read the Codex: http://codex.wordpress.org/Function_Reference/query_posts#Usage and http://codex.wordpress.org/Function_Reference/query_posts#Post_.26_Page_Parameters
<?php query_posts(array( 'post_type' => 'portfolio', 'showposts' => 10 ) ); ?> <?php while (have_posts()) : the_post(); ?> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <p><?php echo get_the_excerpt(); ?></p> <?php endwhile;?>
-
Questa è una risposta abbastanza vecchia,maperessere chiari,non c'èilmodoin cui dovrestifarlo.Quasiinevitabilmenteporterà a 404e una serie di altriproblemi.Perfavoreguarda le risposte di @ kaiser o [questopost sulperchénon dovresti usare `query_posts ()`] (http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-e-pre-get-posts/50762 # 50762)This is a fairly old answer - but to be clear, there is not the way you should being doing this. It will almost inevitably lead to 404s and a host of other problems. Please see @kaiser's answers or [this post on why you shouldn't use `query_posts()`](http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts/50762#50762)
- 7
- 2013-05-28
- Stephen Harris
-
- 2013-05-27
La rispostatardivapoiché la rispostaprincipale utilizza
query_posts()
,che non dovrebbemai essereeseguita.Utilizza unfiltro
Utilizzailfiltro
pre_get_posts
e imposta semplicementeiltipo dipostportfolio
per la queryprincipale. Utilizzai tag condizionali per determinare dove desideriinserire questofiltro.Esempio rapido
<?php defined( 'ABSPATH' ) OR exit; /* Plugin Name: (#6417) "Portfolio" post type in query */ add_filter( 'pre_get_posts', 'wpse_6417_portfolio_posts' ); function wpse_6417_portfolio_posts( $query ) { if ( ! $query->is_main_query() // Here we can check for all Conditional Tags OR ! $query->is_archive() // For e.g.: Every archive will feature both post types ) return $query; $query->set( 'post_type', array( 'post', 'portfolio' ) ); return $query; }
Dichiarazione dinon responsabilità
Il codice sopra è unplugin,mapuò semplicementeessereinseritonelfile
functions.php
deltuotema (che è non consigliato).Late answer as the main answer uses
query_posts()
, which should never be done.Use a filter
Use the
pre_get_posts
filter and just set theportfolio
post type for the main query. Use Conditional Tags to determine where you want to have this filter.Quick Example
<?php defined( 'ABSPATH' ) OR exit; /* Plugin Name: (#6417) "Portfolio" post type in query */ add_filter( 'pre_get_posts', 'wpse_6417_portfolio_posts' ); function wpse_6417_portfolio_posts( $query ) { if ( ! $query->is_main_query() // Here we can check for all Conditional Tags OR ! $query->is_archive() // For e.g.: Every archive will feature both post types ) return $query; $query->set( 'post_type', array( 'post', 'portfolio' ) ); return $query; }
Disclaimer
The above code is a plugin, but can simply get stuffed in the
functions.php
file of your theme (which is not recommended).-
perchénon è consigliabile aggiungerlo allefunzioni?Sicuramente,se l'amministratore del sito cambiailtema,dovrebbe comunque affrontare come visualizzareilportfolio sulla homepage con questonuovotema.Quindi,direi che è altrettanto valido aggiungerlonellefunzionipiuttosto chein unplugin.Oppuremi sfugge qualcosa?why is it not recommended to add it to functions? Surely, if the site admin changes the theme they would need to address how to display the portfolio on the home page with this new theme anyway. So, I would say it is just as valid to add this in functions rather than a plugin. Or am I missing something?
- 0
- 2016-11-29
- Phill Healey
-
@PhillHealey Come hai detto,i dati sarebberoinvisibilie dovresti copiaree incollareil codice.Lemodifiche logichepesanti alle query vengonofornite almegliotramiteplug-in,mentre la visualizzazionee lo stile dovrebberoesseremantenutinei temi.@PhillHealey As you said, the data would be invisible and you would have to copy and paste the code around. Heavy, logic modifications to queries are best served via plugins, while displaying and styling should be kept in themes.
- 0
- 2016-11-29
- kaiser
-
Non se quel codice è specificoperiltema.Not if that code is specific to the theme.
- 0
- 2016-12-03
- Phill Healey
-
@PhillHealey Untipo dipostnon dovrebbe **mai **essere specificoper untema.@PhillHealey A post type should **never** be specific to a theme.
- 0
- 2016-12-04
- kaiser
-
Ok,se vuoifare unpo 'dipanico sugli assoluti,allorabene.Tuttavia,non è corretto affermare chenessun codice specifico diprogettazione deveessereinviato a unplug-in.Ci sonomolte voltein cui ciònon è appropriato.Ok, if you want to get in some tit-for-tat over absolutes then fine. However, It's just not correct to say that none design specific code should be pushed out to a plugin. There are lots of times when that's not appropriate.
- 0
- 2016-12-05
- Phill Healey
-
- 2013-12-11
Aggiungi questo codice altuo file difunzioni deitemi secondari (consigliato)per aggiungere letue singolepagine CPT al cicloprincipale
add_action( 'pre_get_posts', 'add_custom_post_types_to_loop' ); function add_custom_post_types_to_loop( $query ) { if ( is_home() && $query->is_main_query() ) $query->set( 'post_type', array( 'post', 'portfolio' ) ); return $query; }
Fonte http://codex.wordpress.org/Post_Types
Oppure crea unmodello dipagina archive-portfolio.php personalizzato chemostrerà solo letuepagine CPT. Questo deveesserefatto solo senon hai aggiunto unapagina di archivio utilizzando leimpostazioni delplugin.
Esempio: "has_archive"=>true,
Puoi anche controllare quantepagine vengono visualizzatee l'ordinein cui vengono visualizzatenellapagina di archivio utilizzando questo codice:
add_action( 'pre_get_posts', 'cpt_items' ); function cpt_items( $query ) { if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'portfolio' ) ) { $query->set( 'posts_per_page', '8' ); $query->set( 'order', 'ASC' ); } }
Add this code to your child themes functions file (recommended) to add your single CPT pages to your main loop
add_action( 'pre_get_posts', 'add_custom_post_types_to_loop' ); function add_custom_post_types_to_loop( $query ) { if ( is_home() && $query->is_main_query() ) $query->set( 'post_type', array( 'post', 'portfolio' ) ); return $query; }
Source http://codex.wordpress.org/Post_Types
Or create a custom archive-portfolio.php page template which will only display your CPT pages. This only needs to be done if you haven't added a archive page using the plugin settings.
Example: 'has_archive' => true,
You can also control how many pages are displayed and the order in which they're displayed on the archive page using this code:
add_action( 'pre_get_posts', 'cpt_items' ); function cpt_items( $query ) { if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'portfolio' ) ) { $query->set( 'posts_per_page', '8' ); $query->set( 'order', 'ASC' ); } }
Hoinstallatoil plug-in Custom Post Type UI .Dopo l'attivazione di questoplugin ho creato unposttypepersonalizzato chiamato
portfolio
.Ora voglio usarlonellapagina delportfolionelfront-end.Comefaccio a recuperare tuttii post ditipopostpersonalizzatoportfolio
?